pi-charset 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/CHANGELOG.zh-CN.md +18 -0
- package/dist/config.js +943 -0
- package/dist/edit/apply-patch-core.js +274 -0
- package/dist/edit/diagnostics.js +46 -0
- package/dist/edit/hashline.js +400 -0
- package/dist/edit/patch.js +634 -0
- package/dist/edit/paths.js +48 -0
- package/dist/edit/plan.js +90 -0
- package/dist/encoding/classify.js +430 -0
- package/dist/encoding/common-scripts.js +58 -0
- package/dist/encoding/converter.js +311 -0
- package/dist/encoding/fast-codec.js +476 -0
- package/dist/encoding/file-stamp.js +32 -0
- package/dist/encoding/line-endings.js +118 -0
- package/dist/encoding/mime.js +88 -0
- package/dist/encoding/stream-transcode.js +287 -0
- package/dist/grep-core.js +730 -0
- package/dist/journal.js +494 -0
- package/dist/maintenance.js +333 -0
- package/dist/mcp/server.js +866 -0
- package/dist/notify.js +60 -0
- package/dist/operations.js +1021 -0
- package/dist/path-key.js +10 -0
- package/dist/read-view.js +927 -0
- package/dist/resolve.js +325 -0
- package/dist/version.js +2 -0
- package/docs/MCP-AND-MULTI-AGENT.md +27 -19
- package/docs/zh-CN/MCP-AND-MULTI-AGENT.md +27 -19
- package/package.json +14 -2
- package/src/edit/apply-patch-core.ts +32 -3
- package/src/grep-core.ts +50 -8
- package/src/grep.ts +9 -1
- package/src/index.ts +8 -5
- package/src/journal.ts +167 -42
- package/src/mcp/server.ts +435 -163
- package/src/operations.ts +197 -68
- package/src/read-view.ts +111 -8
- package/src/shell-guard.ts +38 -1
- package/src/version.ts +1 -1
- package/tools/pi-charset-cli.mjs +453 -167
- package/tools/pi-charset-mcp.mjs +78 -41
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
// Compiled zero-allocation round-trip DFA, encodability bitset, flat BMP encoder, ICU TextDecoder fast path,
|
|
2
|
+
// and exact raw byte-splicing derived directly from iconv-lite's DBCSCodec tables.
|
|
3
|
+
// Any multi-character sequence (e.g. Big5-HKSCS), rare 4-byte entry, or ICU-divergent pair falls back to
|
|
4
|
+
// iconv-lite so every byte and code point stays 100% bit-for-bit identical to iconv.decode / iconv.encode.
|
|
5
|
+
import iconv from "iconv-lite";
|
|
6
|
+
const UNASSIGNED = -1;
|
|
7
|
+
const GB18030_CODE = -2;
|
|
8
|
+
const SEQ_START = -10;
|
|
9
|
+
const NODE_START = -1000;
|
|
10
|
+
// DFA state values inside each 256-entry node table:
|
|
11
|
+
// 0 = invalid / non-round-tripping
|
|
12
|
+
// 1 = valid leaf (returns to root, ICU-identical)
|
|
13
|
+
// 2 = GB18030 4-byte leaf (verified via gb18030 range tables; forces iconv decode)
|
|
14
|
+
// 3 = sequence entry (must fall back to full iconv round-trip)
|
|
15
|
+
// 4 = valid leaf whose ICU TextDecoder mapping differs from iconv-lite (valid round-trip, forces iconv decode)
|
|
16
|
+
// <0 = transition to node index (-val)
|
|
17
|
+
const DFA_INVALID = 0;
|
|
18
|
+
const DFA_VALID = 1;
|
|
19
|
+
const DFA_GB18030 = 2;
|
|
20
|
+
const DFA_FALLBACK = 3;
|
|
21
|
+
const DFA_VALID_ICU_DIFF = 4;
|
|
22
|
+
// Exhaustively verified 18 2-byte pairs where Node/WHATWG ICU TextDecoder("gbk"/"gb18030") differs from iconv-lite
|
|
23
|
+
// (PUA vertical punctuation in 0xA6D9..0xA6F3 and 0xFE59..0xFEA0). Verified by test/invariants.test.ts.
|
|
24
|
+
const GB_ICU_DIFF_PAIRS = new Set([
|
|
25
|
+
0xa6d9, 0xa6da, 0xa6db, 0xa6dc, 0xa6dd, 0xa6de, 0xa6df, 0xa6ec, 0xa6ed, 0xa6f3,
|
|
26
|
+
0xfe59, 0xfe61, 0xfe66, 0xfe67, 0xfe6d, 0xfe7e, 0xfe90, 0xfea0,
|
|
27
|
+
]);
|
|
28
|
+
const ICU_DECODER_NAMES = {
|
|
29
|
+
GBK: "gbk",
|
|
30
|
+
GB2312: "gbk",
|
|
31
|
+
GB18030: "gb18030",
|
|
32
|
+
"EUC-JP": "euc-jp",
|
|
33
|
+
};
|
|
34
|
+
const compiledCache = new Map();
|
|
35
|
+
// Records buffers already proven by fastExactRoundTrip to contain zero ICU-divergent pairs or 4-byte sequences,
|
|
36
|
+
// so a subsequent decodeToUtf8 on the same buffer can invoke C++ ICU TextDecoder with zero extra scanning.
|
|
37
|
+
const icuCleanBuffers = new WeakMap();
|
|
38
|
+
function findIdx(table, val) {
|
|
39
|
+
if (table[0] > val)
|
|
40
|
+
return -1;
|
|
41
|
+
let l = 0;
|
|
42
|
+
let r = table.length;
|
|
43
|
+
while (l < r - 1) {
|
|
44
|
+
const mid = l + ((r - l + 1) >> 1);
|
|
45
|
+
if (table[mid] <= val)
|
|
46
|
+
l = mid;
|
|
47
|
+
else
|
|
48
|
+
r = mid;
|
|
49
|
+
}
|
|
50
|
+
return l;
|
|
51
|
+
}
|
|
52
|
+
function decodeMbCode(decodeTables, mbCode) {
|
|
53
|
+
let bytes;
|
|
54
|
+
if (mbCode < 0x100)
|
|
55
|
+
bytes = [mbCode];
|
|
56
|
+
else if (mbCode < 0x10000)
|
|
57
|
+
bytes = [mbCode >> 8, mbCode & 0xff];
|
|
58
|
+
else if (mbCode < 0x1000000)
|
|
59
|
+
bytes = [mbCode >> 16, (mbCode >> 8) & 0xff, mbCode & 0xff];
|
|
60
|
+
else
|
|
61
|
+
bytes = [mbCode >>> 24, (mbCode >>> 16) & 0xff, (mbCode >>> 8) & 0xff, mbCode & 0xff];
|
|
62
|
+
let nodeIdx = 0;
|
|
63
|
+
for (let i = 0; i < bytes.length - 1; i++) {
|
|
64
|
+
const v = decodeTables[nodeIdx][bytes[i]];
|
|
65
|
+
if (v > NODE_START)
|
|
66
|
+
return UNASSIGNED;
|
|
67
|
+
nodeIdx = NODE_START - v;
|
|
68
|
+
}
|
|
69
|
+
return decodeTables[nodeIdx][bytes[bytes.length - 1]];
|
|
70
|
+
}
|
|
71
|
+
function gb18030PtrValid(ptr, gb, encodeTable) {
|
|
72
|
+
const idx = findIdx(gb.gbChars, ptr);
|
|
73
|
+
if (idx < 0)
|
|
74
|
+
return false;
|
|
75
|
+
const uCode = gb.uChars[idx] + ptr - gb.gbChars[idx];
|
|
76
|
+
if (uCode === 0xfffd || uCode > 0x10ffff || (uCode >= 0xd800 && uCode <= 0xdfff))
|
|
77
|
+
return false;
|
|
78
|
+
const bucket = encodeTable[uCode >> 8];
|
|
79
|
+
if (bucket !== undefined && bucket[uCode & 0xff] !== UNASSIGNED)
|
|
80
|
+
return false;
|
|
81
|
+
const eIdx = findIdx(gb.uChars, uCode);
|
|
82
|
+
if (eIdx < 0)
|
|
83
|
+
return false;
|
|
84
|
+
return gb.gbChars[eIdx] + (uCode - gb.uChars[eIdx]) === ptr;
|
|
85
|
+
}
|
|
86
|
+
function gb18030CodePointEncodable(uCode, gb, encodeTable) {
|
|
87
|
+
if (uCode > 0x10ffff || (uCode >= 0xd800 && uCode <= 0xdfff))
|
|
88
|
+
return false;
|
|
89
|
+
const bucket = encodeTable[uCode >> 8];
|
|
90
|
+
if (bucket !== undefined && bucket[uCode & 0xff] !== UNASSIGNED)
|
|
91
|
+
return false;
|
|
92
|
+
const eIdx = findIdx(gb.uChars, uCode);
|
|
93
|
+
if (eIdx < 0)
|
|
94
|
+
return false;
|
|
95
|
+
const ptr = gb.gbChars[eIdx] + (uCode - gb.uChars[eIdx]);
|
|
96
|
+
if (ptr < 0 || ptr >= 126 * 10 * 126 * 10)
|
|
97
|
+
return false;
|
|
98
|
+
const dIdx = findIdx(gb.gbChars, ptr);
|
|
99
|
+
if (dIdx < 0)
|
|
100
|
+
return false;
|
|
101
|
+
return gb.uChars[dIdx] + (ptr - gb.gbChars[dIdx]) === uCode;
|
|
102
|
+
}
|
|
103
|
+
function compileDbcs(encoding) {
|
|
104
|
+
const cached = compiledCache.get(encoding);
|
|
105
|
+
if (cached !== undefined)
|
|
106
|
+
return cached;
|
|
107
|
+
let codec;
|
|
108
|
+
try {
|
|
109
|
+
codec = iconv.getCodec(encoding);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
compiledCache.set(encoding, null);
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
if (!codec || !Array.isArray(codec.decodeTables) || !Array.isArray(codec.encodeTable)) {
|
|
116
|
+
compiledCache.set(encoding, null);
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
const up = encoding.toUpperCase();
|
|
120
|
+
const isGbIcu = up === "GBK" || up === "GB2312" || up === "GB18030";
|
|
121
|
+
const icuName = ICU_DECODER_NAMES[up];
|
|
122
|
+
let icuDecoder = null;
|
|
123
|
+
if (icuName) {
|
|
124
|
+
try {
|
|
125
|
+
icuDecoder = new TextDecoder(icuName);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
icuDecoder = null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const decodeTables = codec.decodeTables;
|
|
132
|
+
const encodeTable = codec.encodeTable;
|
|
133
|
+
const gb18030 = codec.gb18030 ?? null;
|
|
134
|
+
const hasEncodeSeq = (codec.encodeTableSeq?.length ?? 0) > 0;
|
|
135
|
+
const dfa = decodeTables.map(() => new Int16Array(256));
|
|
136
|
+
const visited = new Uint8Array(decodeTables.length);
|
|
137
|
+
const fill = (nodeIdx, prefix) => {
|
|
138
|
+
if (visited[nodeIdx])
|
|
139
|
+
return;
|
|
140
|
+
visited[nodeIdx] = 1;
|
|
141
|
+
const src = decodeTables[nodeIdx];
|
|
142
|
+
const dst = dfa[nodeIdx];
|
|
143
|
+
for (let b = 0; b < 256; b++) {
|
|
144
|
+
const val = src[b];
|
|
145
|
+
const mbCode = ((prefix << 8) | b) >>> 0;
|
|
146
|
+
if (val >= 0) {
|
|
147
|
+
if (val === 0xfffd || (val >= 0xd800 && val <= 0xdfff) || val > 0x10ffff) {
|
|
148
|
+
dst[b] = DFA_INVALID;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const bucket = encodeTable[val >> 8];
|
|
152
|
+
const reEnc = bucket !== undefined ? bucket[val & 0xff] : UNASSIGNED;
|
|
153
|
+
if (reEnc <= SEQ_START)
|
|
154
|
+
dst[b] = DFA_FALLBACK;
|
|
155
|
+
else if (reEnc !== mbCode)
|
|
156
|
+
dst[b] = DFA_INVALID;
|
|
157
|
+
else if (isGbIcu && GB_ICU_DIFF_PAIRS.has(mbCode))
|
|
158
|
+
dst[b] = DFA_VALID_ICU_DIFF;
|
|
159
|
+
else if (up === "EUC-JP" && mbCode >= 0x10000)
|
|
160
|
+
dst[b] = DFA_VALID_ICU_DIFF;
|
|
161
|
+
else
|
|
162
|
+
dst[b] = DFA_VALID;
|
|
163
|
+
}
|
|
164
|
+
else if (val <= NODE_START) {
|
|
165
|
+
const nextIdx = NODE_START - val;
|
|
166
|
+
dst[b] = -nextIdx;
|
|
167
|
+
fill(nextIdx, mbCode);
|
|
168
|
+
}
|
|
169
|
+
else if (val === GB18030_CODE) {
|
|
170
|
+
dst[b] = DFA_GB18030;
|
|
171
|
+
}
|
|
172
|
+
else if (val <= SEQ_START) {
|
|
173
|
+
dst[b] = DFA_FALLBACK;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
dst[b] = DFA_INVALID;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
fill(0, 0);
|
|
181
|
+
let asciiFast = true;
|
|
182
|
+
for (let b = 0; b < 128; b++) {
|
|
183
|
+
if (dfa[0][b] !== DFA_VALID) {
|
|
184
|
+
asciiFast = false;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const bmpEncodable = new Uint8Array(8192);
|
|
189
|
+
let asciiIdentity = true;
|
|
190
|
+
for (let cp = 0; cp < 0x10000; cp++) {
|
|
191
|
+
if (cp >= 0xd800 && cp <= 0xdfff)
|
|
192
|
+
continue;
|
|
193
|
+
const bucket = encodeTable[cp >> 8];
|
|
194
|
+
const mb = bucket !== undefined ? bucket[cp & 0xff] : UNASSIGNED;
|
|
195
|
+
let ok = false;
|
|
196
|
+
if (mb >= 0 && (gb18030 || mb < 0x1000000)) {
|
|
197
|
+
ok = decodeMbCode(decodeTables, mb) === cp;
|
|
198
|
+
}
|
|
199
|
+
else if (mb === UNASSIGNED && gb18030) {
|
|
200
|
+
ok = gb18030CodePointEncodable(cp, gb18030, encodeTable);
|
|
201
|
+
}
|
|
202
|
+
if (ok)
|
|
203
|
+
bmpEncodable[cp >> 3] |= 1 << (cp & 7);
|
|
204
|
+
if (cp < 128 && mb !== cp)
|
|
205
|
+
asciiIdentity = false;
|
|
206
|
+
}
|
|
207
|
+
let asciiEncodable = true;
|
|
208
|
+
for (let cp = 0; cp < 128; cp++) {
|
|
209
|
+
if ((bmpEncodable[cp >> 3] & (1 << (cp & 7))) === 0) {
|
|
210
|
+
asciiEncodable = false;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const compiled = {
|
|
215
|
+
dfa,
|
|
216
|
+
asciiFast,
|
|
217
|
+
gb18030,
|
|
218
|
+
encodeTable,
|
|
219
|
+
bmpEncodable,
|
|
220
|
+
hasEncodeSeq,
|
|
221
|
+
asciiEncodable,
|
|
222
|
+
bmpToMb: null,
|
|
223
|
+
asciiIdentity,
|
|
224
|
+
icuDecoder,
|
|
225
|
+
};
|
|
226
|
+
compiledCache.set(encoding, compiled);
|
|
227
|
+
return compiled;
|
|
228
|
+
}
|
|
229
|
+
function getBmpEncodeTable(c) {
|
|
230
|
+
if (c.bmpToMb)
|
|
231
|
+
return c.bmpToMb;
|
|
232
|
+
const table = new Uint16Array(65536);
|
|
233
|
+
const encTable = c.encodeTable;
|
|
234
|
+
for (let cp = 1; cp < 0x10000; cp++) {
|
|
235
|
+
if (cp >= 0xd800 && cp <= 0xdfff)
|
|
236
|
+
continue;
|
|
237
|
+
const bucket = encTable[cp >> 8];
|
|
238
|
+
if (!bucket)
|
|
239
|
+
continue;
|
|
240
|
+
const mb = bucket[cp & 0xff];
|
|
241
|
+
if (mb > 0 && mb < 0x10000)
|
|
242
|
+
table[cp] = mb;
|
|
243
|
+
}
|
|
244
|
+
c.bmpToMb = table;
|
|
245
|
+
return table;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Zero-allocation exact round-trip check for DBCS codecs.
|
|
249
|
+
* Also marks buffers free of ICU-divergent pairs / 4-byte GB18030 sequences so subsequent decodes can use C++ ICU.
|
|
250
|
+
*/
|
|
251
|
+
export function fastExactRoundTrip(buf, encoding) {
|
|
252
|
+
const c = compileDbcs(encoding);
|
|
253
|
+
if (!c)
|
|
254
|
+
return null;
|
|
255
|
+
const dfa = c.dfa;
|
|
256
|
+
const root = dfa[0];
|
|
257
|
+
let node = root;
|
|
258
|
+
const len = buf.length;
|
|
259
|
+
const asciiFast = c.asciiFast;
|
|
260
|
+
let icuSafe = c.icuDecoder !== null;
|
|
261
|
+
for (let i = 0; i < len; i++) {
|
|
262
|
+
const b = buf[i];
|
|
263
|
+
if (asciiFast && node === root && b < 0x80)
|
|
264
|
+
continue;
|
|
265
|
+
const v = node[b];
|
|
266
|
+
if (v === DFA_VALID) {
|
|
267
|
+
node = root;
|
|
268
|
+
}
|
|
269
|
+
else if (v < 0) {
|
|
270
|
+
node = dfa[-v];
|
|
271
|
+
}
|
|
272
|
+
else if (v === DFA_VALID_ICU_DIFF) {
|
|
273
|
+
icuSafe = false;
|
|
274
|
+
node = root;
|
|
275
|
+
}
|
|
276
|
+
else if (v === DFA_GB18030 && c.gb18030 && i >= 3) {
|
|
277
|
+
icuSafe = false;
|
|
278
|
+
const ptr = (buf[i - 3] - 0x81) * 12600 + (buf[i - 2] - 0x30) * 1260 + (buf[i - 1] - 0x81) * 10 + (b - 0x30);
|
|
279
|
+
if (!gb18030PtrValid(ptr, c.gb18030, c.encodeTable))
|
|
280
|
+
return false;
|
|
281
|
+
node = root;
|
|
282
|
+
}
|
|
283
|
+
else if (v === DFA_FALLBACK) {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (node !== root)
|
|
291
|
+
return false;
|
|
292
|
+
if (icuSafe) {
|
|
293
|
+
let set = icuCleanBuffers.get(buf);
|
|
294
|
+
if (!set) {
|
|
295
|
+
set = new Set();
|
|
296
|
+
icuCleanBuffers.set(buf, set);
|
|
297
|
+
}
|
|
298
|
+
set.add(encoding.toUpperCase());
|
|
299
|
+
}
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
/** Fast native C++ ICU decode when `buf` was already proven ICU-identical for `encoding`; null otherwise. */
|
|
303
|
+
export function fastDecodeDbcs(buf, encoding) {
|
|
304
|
+
const up = encoding.toUpperCase();
|
|
305
|
+
if (!icuCleanBuffers.get(buf)?.has(up))
|
|
306
|
+
return null;
|
|
307
|
+
const c = compileDbcs(encoding);
|
|
308
|
+
if (!c?.icuDecoder)
|
|
309
|
+
return null;
|
|
310
|
+
return c.icuDecoder.decode(buf);
|
|
311
|
+
}
|
|
312
|
+
const NON_ASCII_STR = /[^\x00-\x7f]/;
|
|
313
|
+
/**
|
|
314
|
+
* Flat Uint16Array DBCS encoder (~2.7x faster than iconv.encode and 50% less peak allocation; ~40x on ASCII).
|
|
315
|
+
* Returns null if any character needs multi-codepoint sequences, 3/4-byte encoding, or fallback.
|
|
316
|
+
*/
|
|
317
|
+
export function fastEncodeDbcs(text, encoding) {
|
|
318
|
+
const c = compileDbcs(encoding);
|
|
319
|
+
if (!c || c.hasEncodeSeq || !c.asciiIdentity)
|
|
320
|
+
return null;
|
|
321
|
+
if (!NON_ASCII_STR.test(text))
|
|
322
|
+
return Buffer.from(text, "latin1");
|
|
323
|
+
const bmp = getBmpEncodeTable(c);
|
|
324
|
+
const len = text.length;
|
|
325
|
+
const out = Buffer.allocUnsafe(len * 2);
|
|
326
|
+
let j = 0;
|
|
327
|
+
for (let i = 0; i < len; i++) {
|
|
328
|
+
const u = text.charCodeAt(i);
|
|
329
|
+
if (u < 0x80) {
|
|
330
|
+
out[j++] = u;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
const mb = bmp[u];
|
|
334
|
+
if (mb >= 0x100) {
|
|
335
|
+
out[j++] = mb >>> 8;
|
|
336
|
+
out[j++] = mb & 0xff;
|
|
337
|
+
}
|
|
338
|
+
else if (mb > 0) {
|
|
339
|
+
out[j++] = mb;
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return out.subarray(0, j);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Exact raw byte-splicing for edits on large files: copies untouched byte regions directly from `raw`
|
|
350
|
+
* via C++ memcpy and encodes only the modified ranges. Returns null when fallback to full encode is needed.
|
|
351
|
+
*/
|
|
352
|
+
export function spliceEditedBytes(raw, bomLength, before, ranges, encoding, encodeChunk) {
|
|
353
|
+
if (ranges.length === 0)
|
|
354
|
+
return raw;
|
|
355
|
+
const up = encoding.toUpperCase();
|
|
356
|
+
const body = raw.subarray(bomLength);
|
|
357
|
+
// Measure exact byte length of before[from..to) in `encoding` without allocating encoded Buffers.
|
|
358
|
+
let measure;
|
|
359
|
+
if (up === "UTF-8") {
|
|
360
|
+
measure = (from, to) => Buffer.byteLength(before.slice(from, to), "utf8");
|
|
361
|
+
}
|
|
362
|
+
else if (up === "UTF-16LE" || up === "UTF-16BE") {
|
|
363
|
+
measure = (from, to) => (to - from) * 2;
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
const c = compileDbcs(encoding);
|
|
367
|
+
if (!c || c.hasEncodeSeq || !c.asciiIdentity)
|
|
368
|
+
return null;
|
|
369
|
+
const bmp = getBmpEncodeTable(c);
|
|
370
|
+
const isGb18030 = c.gb18030 !== null;
|
|
371
|
+
measure = (from, to) => {
|
|
372
|
+
let bytes = 0;
|
|
373
|
+
for (let i = from; i < to; i++) {
|
|
374
|
+
const u = before.charCodeAt(i);
|
|
375
|
+
if (u < 0x80) {
|
|
376
|
+
bytes++;
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
const mb = bmp[u];
|
|
380
|
+
if (mb >= 0x100)
|
|
381
|
+
bytes += 2;
|
|
382
|
+
else if (mb > 0)
|
|
383
|
+
bytes++;
|
|
384
|
+
else if (isGb18030) {
|
|
385
|
+
if (u >= 0xd800 && u <= 0xdbff)
|
|
386
|
+
i++;
|
|
387
|
+
bytes += 4;
|
|
388
|
+
}
|
|
389
|
+
else
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return bytes;
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
const chunks = [];
|
|
397
|
+
if (bomLength > 0)
|
|
398
|
+
chunks.push(raw.subarray(0, bomLength));
|
|
399
|
+
let charPos = 0;
|
|
400
|
+
let bytePos = 0;
|
|
401
|
+
for (const r of ranges) {
|
|
402
|
+
const keepLen = measure(charPos, r.start);
|
|
403
|
+
const skipLen = measure(r.start, r.end);
|
|
404
|
+
if (keepLen === null || skipLen === null)
|
|
405
|
+
return null;
|
|
406
|
+
if (keepLen > 0)
|
|
407
|
+
chunks.push(body.subarray(bytePos, bytePos + keepLen));
|
|
408
|
+
bytePos += keepLen + skipLen;
|
|
409
|
+
if (r.text.length > 0)
|
|
410
|
+
chunks.push(encodeChunk(r.text, encoding));
|
|
411
|
+
charPos = r.end;
|
|
412
|
+
}
|
|
413
|
+
const tailLen = measure(charPos, before.length);
|
|
414
|
+
if (tailLen === null || bytePos + tailLen !== body.length)
|
|
415
|
+
return null;
|
|
416
|
+
if (tailLen > 0)
|
|
417
|
+
chunks.push(body.subarray(bytePos));
|
|
418
|
+
return Buffer.concat(chunks);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Zero-allocation check whether all characters in a well-formed string can be losslessly round-tripped in encoding.
|
|
422
|
+
* Returns boolean when decided definitively, or null if fallback to iconv is needed.
|
|
423
|
+
*/
|
|
424
|
+
export function fastCanEncodeAll(text, encoding) {
|
|
425
|
+
const up = encoding.toUpperCase();
|
|
426
|
+
if (up === "UTF-8" || up === "UTF-16LE" || up === "UTF-16BE" || up === "UTF-32LE" || up === "UTF-32BE") {
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
const c = compileDbcs(encoding);
|
|
430
|
+
if (!c || c.hasEncodeSeq)
|
|
431
|
+
return null;
|
|
432
|
+
if (c.asciiEncodable && !NON_ASCII_STR.test(text))
|
|
433
|
+
return true;
|
|
434
|
+
const bits = c.bmpEncodable;
|
|
435
|
+
const asciiOk = c.asciiEncodable;
|
|
436
|
+
const len = text.length;
|
|
437
|
+
for (let i = 0; i < len; i++) {
|
|
438
|
+
const u = text.charCodeAt(i);
|
|
439
|
+
if (asciiOk && u < 0x80)
|
|
440
|
+
continue;
|
|
441
|
+
if (u >= 0xd800 && u <= 0xdbff) {
|
|
442
|
+
const lo = text.charCodeAt(++i);
|
|
443
|
+
const cp = 0x10000 + ((u - 0xd800) << 10) + (lo - 0xdc00);
|
|
444
|
+
if (c.gb18030) {
|
|
445
|
+
if (!gb18030CodePointEncodable(cp, c.gb18030, c.encodeTable))
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
const bucket = c.encodeTable[cp >> 8];
|
|
450
|
+
if (!bucket || bucket[cp & 0xff] === UNASSIGNED)
|
|
451
|
+
return false;
|
|
452
|
+
return null;
|
|
453
|
+
}
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
if ((bits[u >> 3] & (1 << (u & 7))) === 0) {
|
|
457
|
+
// Rare 4-byte entry in a non-GB18030 table (e.g. GBK U+E7C7) falls back to iconv for exact parity.
|
|
458
|
+
if (!c.gb18030) {
|
|
459
|
+
const bucket = c.encodeTable[u >> 8];
|
|
460
|
+
if (bucket !== undefined && bucket[u & 0xff] >= 0x1000000)
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
return false;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return true;
|
|
467
|
+
}
|
|
468
|
+
/** Whether encoding has multi-codepoint combining sequences that can cross edit boundaries. */
|
|
469
|
+
export function hasCombiningSequences(encoding) {
|
|
470
|
+
const up = encoding.toUpperCase();
|
|
471
|
+
if (up === "UTF-8" || up === "UTF-16LE" || up === "UTF-16BE" || up === "UTF-32LE" || up === "UTF-32BE") {
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
const c = compileDbcs(encoding);
|
|
475
|
+
return c ? c.hasEncodeSeq : true;
|
|
476
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Unified stat-based file evidence shared across classify, config, sibling inference, decoded-read, and grep caches.
|
|
2
|
+
// Comparing (dev, ino, size, mtimeMs, ctimeMs, plus nanosecond timestamps when available) catches same-millisecond rewrites and atomic inode replacements.
|
|
3
|
+
export function stampFromStat(st) {
|
|
4
|
+
return {
|
|
5
|
+
dev: Number(st.dev ?? 0),
|
|
6
|
+
ino: Number(st.ino ?? 0),
|
|
7
|
+
size: Number(st.size),
|
|
8
|
+
mtimeMs: Number(st.mtimeMs),
|
|
9
|
+
ctimeMs: Number(st.ctimeMs ?? 0),
|
|
10
|
+
...(st.mtimeNs !== undefined ? { mtimeNs: st.mtimeNs } : {}),
|
|
11
|
+
...(st.ctimeNs !== undefined ? { ctimeNs: st.ctimeNs } : {}),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function sameFileStamp(a, b) {
|
|
15
|
+
if (a.size !== b.size ||
|
|
16
|
+
a.mtimeMs !== b.mtimeMs ||
|
|
17
|
+
a.ctimeMs !== b.ctimeMs ||
|
|
18
|
+
a.ino !== b.ino ||
|
|
19
|
+
a.dev !== b.dev) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
if (a.mtimeNs !== undefined && b.mtimeNs !== undefined && a.mtimeNs !== b.mtimeNs)
|
|
23
|
+
return false;
|
|
24
|
+
if (a.ctimeNs !== undefined && b.ctimeNs !== undefined && a.ctimeNs !== b.ctimeNs)
|
|
25
|
+
return false;
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
export function fileStampKey(st) {
|
|
29
|
+
const m = st.mtimeNs !== undefined ? st.mtimeNs.toString() : String(st.mtimeMs);
|
|
30
|
+
const c = st.ctimeNs !== undefined ? st.ctimeNs.toString() : String(st.ctimeMs);
|
|
31
|
+
return `${st.dev}:${st.ino}:${st.size}:${m}:${c}`;
|
|
32
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import iconv from "iconv-lite";
|
|
2
|
+
/** Detect line-ending kind from decoded text string directly using SIMD-accelerated indexOf with early MIXED exit. */
|
|
3
|
+
export function detectLineEndingKindFromText(text) {
|
|
4
|
+
let hasCrlf = false;
|
|
5
|
+
let hasLf = false;
|
|
6
|
+
for (let p = text.indexOf("\n"); p !== -1; p = text.indexOf("\n", p + 1)) {
|
|
7
|
+
if (p > 0 && text.charCodeAt(p - 1) === 13)
|
|
8
|
+
hasCrlf = true;
|
|
9
|
+
else
|
|
10
|
+
hasLf = true;
|
|
11
|
+
if (hasCrlf && hasLf)
|
|
12
|
+
return "MIXED";
|
|
13
|
+
}
|
|
14
|
+
return hasCrlf ? "CRLF" : hasLf ? "LF" : "NONE";
|
|
15
|
+
}
|
|
16
|
+
/** C++-backed UTF-16LE/BE EOL scan: uses native V8 UTF-16 memcpy + SIMD indexOf("\n") instead of a JS byte loop. */
|
|
17
|
+
function detectUtf16LineEndingKind(buf, littleEndian) {
|
|
18
|
+
let start = 0;
|
|
19
|
+
if (buf.length >= 2) {
|
|
20
|
+
if (littleEndian && buf[0] === 0xff && buf[1] === 0xfe)
|
|
21
|
+
start = 2;
|
|
22
|
+
else if (!littleEndian && buf[0] === 0xfe && buf[1] === 0xff)
|
|
23
|
+
start = 2;
|
|
24
|
+
}
|
|
25
|
+
const end = buf.length - ((buf.length - start) & 1);
|
|
26
|
+
if (end <= start)
|
|
27
|
+
return "NONE";
|
|
28
|
+
if (littleEndian) {
|
|
29
|
+
return detectLineEndingKindFromText(buf.toString("utf16le", start, end));
|
|
30
|
+
}
|
|
31
|
+
const copy = Buffer.from(buf.subarray(start, end));
|
|
32
|
+
copy.swap16();
|
|
33
|
+
return detectLineEndingKindFromText(copy.toString("utf16le"));
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Detect line ending kind from buffer or text.
|
|
37
|
+
* UTF-16LE/BE buffers scan aligned 16-bit code units directly (zero decode allocation) so codepoints like U+010A never trigger false newlines.
|
|
38
|
+
*/
|
|
39
|
+
export function detectLineEndingKind(input, encoding) {
|
|
40
|
+
if (typeof input === "string")
|
|
41
|
+
return detectLineEndingKindFromText(input);
|
|
42
|
+
if (encoding) {
|
|
43
|
+
const upper = encoding.toUpperCase();
|
|
44
|
+
if (upper === "UTF-16LE")
|
|
45
|
+
return detectUtf16LineEndingKind(input, true);
|
|
46
|
+
if (upper === "UTF-16BE")
|
|
47
|
+
return detectUtf16LineEndingKind(input, false);
|
|
48
|
+
if (upper.startsWith("UTF-16") || upper.startsWith("UTF-32")) {
|
|
49
|
+
return detectLineEndingKindFromText(iconv.decode(input, encoding));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (input.length >= 2) {
|
|
53
|
+
if (input[0] === 0xff && input[1] === 0xfe && !(input.length >= 4 && input[2] === 0 && input[3] === 0)) {
|
|
54
|
+
return detectUtf16LineEndingKind(input, true);
|
|
55
|
+
}
|
|
56
|
+
if (input[0] === 0xfe && input[1] === 0xff) {
|
|
57
|
+
return detectUtf16LineEndingKind(input, false);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
let hasCrlf = false;
|
|
61
|
+
let hasLf = false;
|
|
62
|
+
for (let p = input.indexOf(0x0a); p !== -1; p = input.indexOf(0x0a, p + 1)) {
|
|
63
|
+
if (p > 0 && input[p - 1] === 0x0d)
|
|
64
|
+
hasCrlf = true;
|
|
65
|
+
else
|
|
66
|
+
hasLf = true;
|
|
67
|
+
if (hasCrlf && hasLf)
|
|
68
|
+
return "MIXED";
|
|
69
|
+
}
|
|
70
|
+
return hasCrlf ? "CRLF" : hasLf ? "LF" : "NONE";
|
|
71
|
+
}
|
|
72
|
+
/** Count CRLF vs LF occurrences and return the majority line-ending style (tie breaks to LF on POSIX, CRLF on Windows). */
|
|
73
|
+
export function dominantLineEndingStyle(input, encoding) {
|
|
74
|
+
let crlf = 0;
|
|
75
|
+
let lf = 0;
|
|
76
|
+
if (typeof input === "string") {
|
|
77
|
+
for (let p = input.indexOf("\n"); p !== -1; p = input.indexOf("\n", p + 1)) {
|
|
78
|
+
if (p > 0 && input.charCodeAt(p - 1) === 13)
|
|
79
|
+
crlf++;
|
|
80
|
+
else
|
|
81
|
+
lf++;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const upper = encoding?.toUpperCase() ?? "";
|
|
86
|
+
const isUtf16Le = upper === "UTF-16LE" || (input.length >= 2 && input[0] === 0xff && input[1] === 0xfe && !(input.length >= 4 && input[2] === 0 && input[3] === 0));
|
|
87
|
+
const isUtf16Be = upper === "UTF-16BE" || (input.length >= 2 && input[0] === 0xfe && input[1] === 0xff);
|
|
88
|
+
if (isUtf16Le || isUtf16Be) {
|
|
89
|
+
const text = isUtf16Le ? input.toString("utf16le") : Buffer.from(input).swap16().toString("utf16le");
|
|
90
|
+
return dominantLineEndingStyle(text);
|
|
91
|
+
}
|
|
92
|
+
for (let p = input.indexOf(0x0a); p !== -1; p = input.indexOf(0x0a, p + 1)) {
|
|
93
|
+
if (p > 0 && input[p - 1] === 0x0d)
|
|
94
|
+
crlf++;
|
|
95
|
+
else
|
|
96
|
+
lf++;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (crlf === lf)
|
|
100
|
+
return process.platform === "win32" ? "CRLF" : "LF";
|
|
101
|
+
return crlf > lf ? "CRLF" : "LF";
|
|
102
|
+
}
|
|
103
|
+
export function restoreLineEndings(text, style) {
|
|
104
|
+
if (style === "LF") {
|
|
105
|
+
return text.indexOf("\r\n") === -1 ? text : text.replace(/\r\n/g, "\n");
|
|
106
|
+
}
|
|
107
|
+
// Fast no-op check for CRLF: skip both regex passes when every \n is already preceded by \r.
|
|
108
|
+
let needsRewrite = false;
|
|
109
|
+
for (let p = text.indexOf("\n"); p !== -1; p = text.indexOf("\n", p + 1)) {
|
|
110
|
+
if (p === 0 || text.charCodeAt(p - 1) !== 13) {
|
|
111
|
+
needsRewrite = true;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (!needsRewrite)
|
|
116
|
+
return text;
|
|
117
|
+
return text.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n");
|
|
118
|
+
}
|