-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompression.diff
More file actions
836 lines (796 loc) · 25.9 KB
/
Copy pathcompression.diff
File metadata and controls
836 lines (796 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
--- input/input.ts
+++ output/output.ts
@@ -23,17 +23,7 @@
* @param value string to which formatting is applied
* @param args replacements for {n}-entries
*/
-export function format(value: string, ...args: any[]): string {
- if (args.length === 0) {
- return value;
- }
- return value.replace(_formatRegexp, function (match, group) {
- const idx = parseInt(group, 10);
- return isNaN(idx) || idx < 0 || idx >= args.length ?
- match :
- args[idx];
- });
-}
+export function format(value: string, ...args: any[]): string { … 11 line(s) … ⟦tj:870557e346c33eff08954ba172e68750⟧ }
const _format2Regexp = /{([^}]+)}/g;
@@ -54,33 +44,13 @@
* In other words, computes `$val`, such that `attr` in `<div attr="$val" />` has the runtime value `value`.
* This prevents XSS injection.
*/
-export function htmlAttributeEncodeValue(value: string): string {
- return value.replace(/[<>"'&]/g, ch => {
- switch (ch) {
- case '<': return '<';
- case '>': return '>';
- case '"': return '"';
- case '\'': return ''';
- case '&': return '&';
- }
- return ch;
- });
-}
+export function htmlAttributeEncodeValue(value: string): string { … 12 line(s) … ⟦tj:0982072a15a096150d4ded8118db0920⟧ }
/**
* Converts HTML characters inside the string to use entities instead. Makes the string safe from
* being used e.g. in HTMLElement.innerHTML.
*/
-export function escape(html: string): string {
- return html.replace(/[<>&]/g, function (match) {
- switch (match) {
- case '<': return '<';
- case '>': return '>';
- case '&': return '&';
- default: return match;
- }
- });
-}
+export function escape(html: string): string { … 10 line(s) … ⟦tj:a9a10288a6bb3b81c3a4ad90a0f0604c⟧ }
/**
* Escapes regular expression characters in a given string
@@ -110,17 +80,8 @@
return `${value.substr(0, maxLength)}${suffix}`;
}
-export function truncateMiddle(value: string, maxLength: number, suffix = '…'): string {
- if (value.length <= maxLength) {
- return value;
- }
+export function truncateMiddle(value: string, maxLength: number, suffix = '…'): string { … 10 line(s) … ⟦tj:1f9459ca7d5677d8c301bcb149a91b2b⟧ }
- const prefixLength = Math.ceil(maxLength / 2) - suffix.length / 2;
- const suffixLength = Math.floor(maxLength / 2) - suffix.length / 2;
-
- return `${value.substr(0, prefixLength)}${suffix}${value.substr(value.length - suffixLength)}`;
-}
-
/**
* Removes all occurrences of needle from the beginning and end of haystack.
* @param haystack string to trim
@@ -139,18 +100,7 @@
export function ltrim(haystack: string, needle: string): string {
if (!haystack || !needle) {
return haystack;
- }
-
- const needleLen = needle.length;
- if (needleLen === 0 || haystack.length === 0) {
- return haystack;
- }
-
- let offset = 0;
-
- while (haystack.indexOf(needle, offset) === offset) {
- offset = offset + needleLen;
- }
+ { … 12 line(s) … ⟦tj:6c04225d2962caa3e2f4f238b555f2c2⟧ }
return haystack.substring(offset);
}
@@ -162,29 +112,7 @@
export function rtrim(haystack: string, needle: string): string {
if (!haystack || !needle) {
return haystack;
- }
-
- const needleLen = needle.length,
- haystackLen = haystack.length;
-
- if (needleLen === 0 || haystackLen === 0) {
- return haystack;
- }
-
- let offset = haystackLen,
- idx = -1;
-
- while (true) {
- idx = haystack.lastIndexOf(needle, offset - 1);
- if (idx === -1 || idx + needleLen !== offset) {
- break;
- }
- if (idx === 0) {
- return '';
- }
- offset = idx;
- }
-
+ { … 23 line(s) … ⟦tj:f906a59dc03564798a1e43fbcabc5295⟧ }
return haystack.substring(0, offset);
}
@@ -207,48 +135,12 @@
export function createRegExp(searchString: string, isRegex: boolean, options: RegExpOptions = {}): RegExp {
if (!searchString) {
throw new Error('Cannot create regex from empty string');
- }
- if (!isRegex) {
- searchString = escapeRegExpCharacters(searchString);
- }
- if (options.wholeWord) {
- if (!/\B/.test(searchString.charAt(0))) {
- searchString = '\\b' + searchString;
- }
- if (!/\B/.test(searchString.charAt(searchString.length - 1))) {
- searchString = searchString + '\\b';
- }
- }
- let modifiers = '';
- if (options.global) {
- modifiers += 'g';
- }
- if (!options.matchCase) {
- modifiers += 'i';
- }
- if (options.multiline) {
- modifiers += 'm';
- }
- if (options.unicode) {
- modifiers += 'u';
- }
-
+ { … 26 line(s) … ⟦tj:ba31bd62edf2046dc41d4c5aaa6e6569⟧ }
return new RegExp(searchString, modifiers);
}
-export function regExpLeadsToEndlessLoop(regexp: RegExp): boolean {
- // Exit early if it's one of these special cases which are meant to match
- // against an empty string
- if (regexp.source === '^' || regexp.source === '^$' || regexp.source === '$' || regexp.source === '^\\s*$') {
- return false;
- }
+export function regExpLeadsToEndlessLoop(regexp: RegExp): boolean { … 12 line(s) … ⟦tj:c0c427b5c9f543234c51ef84d4dc5ca5⟧ }
- // We check against an empty string. If the regular expression doesn't advance
- // (e.g. ends in an endless loop) it will match an empty string.
- const match = regexp.exec('');
- return !!(match && regexp.lastIndex === 0);
-}
-
export function joinStrings(items: (string | undefined | null | false)[], separator: string): string {
return items.filter(item => item !== undefined && item !== null && item !== false).join(separator);
}
@@ -321,19 +213,7 @@
export function replaceAsync(str: string, search: RegExp, replacer: (match: string, ...args: any[]) => Promise<string>): Promise<string> {
const parts: (string | Promise<string>)[] = [];
- let last = 0;
- for (const match of str.matchAll(search)) {
- parts.push(str.slice(last, match.index));
- if (match.index === undefined) {
- throw new Error('match.index should be defined');
- }
-
- last = match.index + match[0].length;
- parts.push(replacer(match[0], ...match.slice(1), match.index, str, match.groups));
- }
-
- parts.push(str.slice(last));
-
+ { … 13 line(s) … ⟦tj:9872a004e1c9be8241b5cebcbc838979⟧ }
return Promise.all(parts).then(p => p.join(''));
}
@@ -350,20 +230,7 @@
export function compareSubstring(a: string, b: string, aStart: number = 0, aEnd: number = a.length, bStart: number = 0, bEnd: number = b.length): number {
for (; aStart < aEnd && bStart < bEnd; aStart++, bStart++) {
const codeA = a.charCodeAt(aStart);
- const codeB = b.charCodeAt(bStart);
- if (codeA < codeB) {
- return -1;
- } else if (codeA > codeB) {
- return 1;
- }
- }
- const aLen = aEnd - aStart;
- const bLen = bEnd - bStart;
- if (aLen < bLen) {
- return -1;
- } else if (aLen > bLen) {
- return 1;
- }
+ { … 14 line(s) … ⟦tj:3981c5355f6a5b2997afccc41c121c73⟧ }
return 0;
}
@@ -374,47 +241,7 @@
export function compareSubstringIgnoreCase(a: string, b: string, aStart: number = 0, aEnd: number = a.length, bStart: number = 0, bEnd: number = b.length): number {
for (; aStart < aEnd && bStart < bEnd; aStart++, bStart++) {
-
- let codeA = a.charCodeAt(aStart);
- let codeB = b.charCodeAt(bStart);
-
- if (codeA === codeB) {
- // equal
- continue;
- }
-
- if (codeA >= 128 || codeB >= 128) {
- // not ASCII letters -> fallback to lower-casing strings
- return compareSubstring(a.toLowerCase(), b.toLowerCase(), aStart, aEnd, bStart, bEnd);
- }
-
- // mapper lower-case ascii letter onto upper-case varinats
- // [97-122] (lower ascii) --> [65-90] (upper ascii)
- if (isLowerAsciiLetter(codeA)) {
- codeA -= 32;
- }
- if (isLowerAsciiLetter(codeB)) {
- codeB -= 32;
- }
-
- // compare both code points
- const diff = codeA - codeB;
- if (diff === 0) {
- continue;
- }
-
- return diff;
- }
-
- const aLen = aEnd - aStart;
- const bLen = bEnd - bStart;
-
- if (aLen < bLen) {
- return -1;
- } else if (aLen > bLen) {
- return 1;
- }
-
+{ … 41 line(s) … ⟦tj:ffcfce8764b6ce93bde8610fdf542afc⟧ }
return 0;
}
@@ -446,37 +273,15 @@
/**
* @returns the length of the common prefix of the two strings.
*/
-export function commonPrefixLength(a: string, b: string): number {
+export function commonPrefixLength(a: string, b: string): number { … 13 line(s) … ⟦tj:7ad543f1b6d25aa8a30cbeead1aa2738⟧ }
- const len = Math.min(a.length, b.length);
- let i: number;
-
- for (i = 0; i < len; i++) {
- if (a.charCodeAt(i) !== b.charCodeAt(i)) {
- return i;
- }
- }
-
- return len;
-}
-
/**
* @returns the length of the common suffix of the two strings.
*/
export function commonSuffixLength(a: string, b: string): number {
const len = Math.min(a.length, b.length);
- let i: number;
-
- const aLastIndex = a.length - 1;
- const bLastIndex = b.length - 1;
-
- for (i = 0; i < len; i++) {
- if (a.charCodeAt(aLastIndex - i) !== b.charCodeAt(bLastIndex - i)) {
- return i;
- }
- }
-
+{ … 11 line(s) … ⟦tj:b2ed04748bc14dbb23cf60394bb53ab5⟧ }
return len;
}
@@ -504,30 +309,12 @@
/**
* get the code point that begins at offset `offset`
*/
-export function getNextCodePoint(str: string, len: number, offset: number): number {
- const charCode = str.charCodeAt(offset);
- if (isHighSurrogate(charCode) && offset + 1 < len) {
- const nextCharCode = str.charCodeAt(offset + 1);
- if (isLowSurrogate(nextCharCode)) {
- return computeCodePoint(charCode, nextCharCode);
- }
- }
- return charCode;
-}
+export function getNextCodePoint(str: string, len: number, offset: number): number { … 10 line(s) … ⟦tj:a1832651628290d77e630079a0ddd3fe⟧ }
/**
* get the code point that ends right before offset `offset`
*/
-function getPrevCodePoint(str: string, offset: number): number {
- const charCode = str.charCodeAt(offset - 1);
- if (isLowSurrogate(charCode) && offset > 1) {
- const prevCharCode = str.charCodeAt(offset - 2);
- if (isHighSurrogate(prevCharCode)) {
- return computeCodePoint(prevCharCode, charCode);
- }
- }
- return charCode;
-}
+function getPrevCodePoint(str: string, offset: number): number { … 10 line(s) … ⟦tj:a35bbd88f63be6c2d091e0e3d1d505dc⟧ }
export class CodePointIterator {
@@ -581,40 +368,16 @@
public nextGraphemeLength(): number {
const graphemeBreakTree = GraphemeBreakTree.getInstance();
const iterator = this._iterator;
- const initialOffset = iterator.offset;
-
- let graphemeBreakType = graphemeBreakTree.getGraphemeBreakType(iterator.nextCodePoint());
- while (!iterator.eol()) {
- const offset = iterator.offset;
- const nextGraphemeBreakType = graphemeBreakTree.getGraphemeBreakType(iterator.nextCodePoint());
- if (breakBetweenGraphemeBreakType(graphemeBreakType, nextGraphemeBreakType)) {
- // move iterator back
- iterator.setOffset(offset);
- break;
- }
- graphemeBreakType = nextGraphemeBreakType;
- }
+ { … 13 line(s) … ⟦tj:b05e8faeb6b0e9605aaad912890fa0d2⟧ }
return (iterator.offset - initialOffset);
- }
+}
public prevGraphemeLength(): number {
const graphemeBreakTree = GraphemeBreakTree.getInstance();
const iterator = this._iterator;
- const initialOffset = iterator.offset;
-
- let graphemeBreakType = graphemeBreakTree.getGraphemeBreakType(iterator.prevCodePoint());
- while (iterator.offset > 0) {
- const offset = iterator.offset;
- const prevGraphemeBreakType = graphemeBreakTree.getGraphemeBreakType(iterator.prevCodePoint());
- if (breakBetweenGraphemeBreakType(prevGraphemeBreakType, graphemeBreakType)) {
- // move iterator back
- iterator.setOffset(offset);
- break;
- }
- graphemeBreakType = prevGraphemeBreakType;
- }
+ { … 13 line(s) … ⟦tj:8a7928fb13b417790a5763b86e0b8009⟧ }
return (initialOffset - iterator.offset);
- }
+}
public eol(): boolean {
return this._iterator.eol();
@@ -687,46 +450,7 @@
export function isFullWidthCharacter(charCode: number): boolean {
// Do a cheap trick to better support wrapping of wide characters, treat them as 2 columns
// http://jrgraphix.net/research/unicode_blocks.php
- // 2E80 - 2EFF CJK Radicals Supplement
- // 2F00 - 2FDF Kangxi Radicals
- // 2FF0 - 2FFF Ideographic Description Characters
- // 3000 - 303F CJK Symbols and Punctuation
- // 3040 - 309F Hiragana
- // 30A0 - 30FF Katakana
- // 3100 - 312F Bopomofo
- // 3130 - 318F Hangul Compatibility Jamo
- // 3190 - 319F Kanbun
- // 31A0 - 31BF Bopomofo Extended
- // 31F0 - 31FF Katakana Phonetic Extensions
- // 3200 - 32FF Enclosed CJK Letters and Months
- // 3300 - 33FF CJK Compatibility
- // 3400 - 4DBF CJK Unified Ideographs Extension A
- // 4DC0 - 4DFF Yijing Hexagram Symbols
- // 4E00 - 9FFF CJK Unified Ideographs
- // A000 - A48F Yi Syllables
- // A490 - A4CF Yi Radicals
- // AC00 - D7AF Hangul Syllables
- // [IGNORE] D800 - DB7F High Surrogates
- // [IGNORE] DB80 - DBFF High Private Use Surrogates
- // [IGNORE] DC00 - DFFF Low Surrogates
- // [IGNORE] E000 - F8FF Private Use Area
- // F900 - FAFF CJK Compatibility Ideographs
- // [IGNORE] FB00 - FB4F Alphabetic Presentation Forms
- // [IGNORE] FB50 - FDFF Arabic Presentation Forms-A
- // [IGNORE] FE00 - FE0F Variation Selectors
- // [IGNORE] FE20 - FE2F Combining Half Marks
- // [IGNORE] FE30 - FE4F CJK Compatibility Forms
- // [IGNORE] FE50 - FE6F Small Form Variants
- // [IGNORE] FE70 - FEFF Arabic Presentation Forms-B
- // FF00 - FFEF Halfwidth and Fullwidth Forms
- // [https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms]
- // of which FF01 - FF5E fullwidth ASCII of 21 to 7E
- // [IGNORE] and FF65 - FFDC halfwidth of Katakana and Hangul
- // [IGNORE] FFF0 - FFFF Specials
- return (
- (charCode >= 0x2E80 && charCode <= 0xD7AF)
- || (charCode >= 0xF900 && charCode <= 0xFAFF)
- || (charCode >= 0xFF01 && charCode <= 0xFF5E)
+ { … 40 line(s) … ⟦tj:ca705a15806d018d1a5d5d16af61c2df⟧ }
);
}
@@ -752,25 +476,7 @@
export function lcut(text: string, n: number, prefix = '') {
const trimmed = text.trimStart();
- if (trimmed.length < n) {
- return trimmed;
- }
-
- const re = /\b/g;
- let i = 0;
- while (re.test(trimmed)) {
- if (trimmed.length - re.lastIndex < n) {
- break;
- }
-
- i = re.lastIndex;
- re.lastIndex += 1;
- }
-
- if (i === 0) {
- return trimmed;
- }
-
+ { … 19 line(s) … ⟦tj:7fcc144946acd55053b86a20d05e98fb⟧ }
return prefix + trimmed.substring(i).trimStart();
}
@@ -782,17 +488,8 @@
export function* forAnsiStringParts(str: string) {
let last = 0;
for (const match of str.matchAll(CSI_SEQUENCE)) {
- if (last !== match.index) {
- yield { isCode: false, str: str.substring(last, match.index) };
- }
-
- yield { isCode: true, str: match[0] };
- last = match.index + match[0].length;
+ { … 10 line(s) … ⟦tj:d380cb08115578f615aff53713958e1f⟧ }
}
-
- if (last !== str.length) {
- yield { isCode: false, str: str.substring(last) };
- }
}
/**
@@ -845,43 +542,12 @@
export function fuzzyContains(target: string, query: string): boolean {
if (!target || !query) {
return false; // return early if target or query are undefined
- }
-
- if (target.length < query.length) {
- return false; // impossible for query to be contained in target
- }
-
- const queryLen = query.length;
- const targetLower = target.toLowerCase();
-
- let index = 0;
- let lastIndexOf = -1;
- while (index < queryLen) {
- const indexOf = targetLower.indexOf(query[index], lastIndexOf + 1);
- if (indexOf < 0) {
- return false;
- }
-
- lastIndexOf = indexOf;
-
- index++;
- }
-
+ { … 22 line(s) … ⟦tj:ed89a488a56594b9c8a34cce5ca0f778⟧ }
return true;
}
-export function containsUppercaseCharacter(target: string, ignoreEscapedChars = false): boolean {
- if (!target) {
- return false;
- }
+export function containsUppercaseCharacter(target: string, ignoreEscapedChars = false): boolean { … 11 line(s) … ⟦tj:26f491c2efa1a56e37e1e165b598878d⟧ }
- if (ignoreEscapedChars) {
- target = target.replace(/\\./g, '');
- }
-
- return target.toLowerCase() !== target;
-}
-
export function uppercaseFirstLetter(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
@@ -889,40 +555,15 @@
export function getNLines(str: string, n = 1): string {
if (n === 0) {
return '';
- }
-
- let idx = -1;
- do {
- idx = str.indexOf('\n', idx + 1);
- n--;
- } while (n > 0 && idx >= 0);
-
- if (idx === -1) {
- return str;
- }
-
- if (str[idx - 1] === '\r') {
- idx--;
- }
-
+ { … 16 line(s) … ⟦tj:84eb5cb41bb9b6b1c0fb554f22bd9181⟧ }
return str.substr(0, idx);
}
/**
* Produces 'a'-'z', followed by 'A'-'Z'... followed by 'a'-'z', etc.
*/
-export function singleLetterHash(n: number): string {
- const LETTERS_CNT = (CharCode.Z - CharCode.A + 1);
+export function singleLetterHash(n: number): string { … 11 line(s) … ⟦tj:ca666e40349a1b81d0455b9c37ca0a86⟧ }
- n = n % (2 * LETTERS_CNT);
-
- if (n < LETTERS_CNT) {
- return String.fromCharCode(CharCode.a + n);
- }
-
- return String.fromCharCode(CharCode.A + n - LETTERS_CNT);
-}
-
//#region Unicode Grapheme Break
export function getGraphemeBreakType(codePoint: number): GraphemeBreakType {
@@ -933,80 +574,7 @@
function breakBetweenGraphemeBreakType(breakTypeA: GraphemeBreakType, breakTypeB: GraphemeBreakType): boolean {
// http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundary_Rules
- // !!! Let's make the common case a bit faster
- if (breakTypeA === GraphemeBreakType.Other) {
- // see https://www.unicode.org/Public/13.0.0/ucd/auxiliary/GraphemeBreakTest-13.0.0d10.html#table
- return (breakTypeB !== GraphemeBreakType.Extend && breakTypeB !== GraphemeBreakType.SpacingMark);
- }
-
- // Do not break between a CR and LF. Otherwise, break before and after controls.
- // GB3 CR × LF
- // GB4 (Control | CR | LF) ÷
- // GB5 ÷ (Control | CR | LF)
- if (breakTypeA === GraphemeBreakType.CR) {
- if (breakTypeB === GraphemeBreakType.LF) {
- return false; // GB3
- }
- }
- if (breakTypeA === GraphemeBreakType.Control || breakTypeA === GraphemeBreakType.CR || breakTypeA === GraphemeBreakType.LF) {
- return true; // GB4
- }
- if (breakTypeB === GraphemeBreakType.Control || breakTypeB === GraphemeBreakType.CR || breakTypeB === GraphemeBreakType.LF) {
- return true; // GB5
- }
-
- // Do not break Hangul syllable sequences.
- // GB6 L × (L | V | LV | LVT)
- // GB7 (LV | V) × (V | T)
- // GB8 (LVT | T) × T
- if (breakTypeA === GraphemeBreakType.L) {
- if (breakTypeB === GraphemeBreakType.L || breakTypeB === GraphemeBreakType.V || breakTypeB === GraphemeBreakType.LV || breakTypeB === GraphemeBreakType.LVT) {
- return false; // GB6
- }
- }
- if (breakTypeA === GraphemeBreakType.LV || breakTypeA === GraphemeBreakType.V) {
- if (breakTypeB === GraphemeBreakType.V || breakTypeB === GraphemeBreakType.T) {
- return false; // GB7
- }
- }
- if (breakTypeA === GraphemeBreakType.LVT || breakTypeA === GraphemeBreakType.T) {
- if (breakTypeB === GraphemeBreakType.T) {
- return false; // GB8
- }
- }
-
- // Do not break before extending characters or ZWJ.
- // GB9 × (Extend | ZWJ)
- if (breakTypeB === GraphemeBreakType.Extend || breakTypeB === GraphemeBreakType.ZWJ) {
- return false; // GB9
- }
-
- // The GB9a and GB9b rules only apply to extended grapheme clusters:
- // Do not break before SpacingMarks, or after Prepend characters.
- // GB9a × SpacingMark
- // GB9b Prepend ×
- if (breakTypeB === GraphemeBreakType.SpacingMark) {
- return false; // GB9a
- }
- if (breakTypeA === GraphemeBreakType.Prepend) {
- return false; // GB9b
- }
-
- // Do not break within emoji modifier sequences or emoji zwj sequences.
- // GB11 \p{Extended_Pictographic} Extend* ZWJ × \p{Extended_Pictographic}
- if (breakTypeA === GraphemeBreakType.ZWJ && breakTypeB === GraphemeBreakType.Extended_Pictographic) {
- // Note: we are not implementing the rule entirely here to avoid introducing states
- return false; // GB11
- }
-
- // GB12 sot (RI RI)* RI × RI
- // GB13 [^RI] (RI RI)* RI × RI
- if (breakTypeA === GraphemeBreakType.Regional_Indicator && breakTypeB === GraphemeBreakType.Regional_Indicator) {
- // Note: we are not implementing the rule entirely here to avoid introducing states
- return false; // GB12 & GB13
- }
-
- // GB999 Any ÷ Any
+ { … 74 line(s) … ⟦tj:4a656f8e6fc84cda1062380177801b8a⟧ }
return true;
}
@@ -1047,38 +615,10 @@
public getGraphemeBreakType(codePoint: number): GraphemeBreakType {
// !!! Let's make 7bit ASCII a bit faster: 0..31
if (codePoint < 32) {
- if (codePoint === CharCode.LineFeed) {
- return GraphemeBreakType.LF;
- }
- if (codePoint === CharCode.CarriageReturn) {
- return GraphemeBreakType.CR;
- }
- return GraphemeBreakType.Control;
- }
- // !!! Let's make 7bit ASCII a bit faster: 32..126
- if (codePoint < 127) {
- return GraphemeBreakType.Other;
- }
-
- const data = this._data;
- const nodeCount = data.length / 3;
- let nodeIndex = 1;
- while (nodeIndex <= nodeCount) {
- if (codePoint < data[3 * nodeIndex]) {
- // go left
- nodeIndex = 2 * nodeIndex;
- } else if (codePoint > data[3 * nodeIndex + 1]) {
- // go right
- nodeIndex = 2 * nodeIndex + 1;
- } else {
- // hit
- return data[3 * nodeIndex + 2];
- }
- }
-
+ { … 29 line(s) … ⟦tj:7d26dd0a95e695956ec6a119e58e9b12⟧ }
return GraphemeBreakType.Other;
- }
}
+}
function getGraphemeBreakRawData(): number[] {
// generated using https://github.com/alexdima/unicode-utils/blob/main/grapheme-break.js
@@ -1094,53 +634,14 @@
export function getLeftDeleteOffset(offset: number, str: string): number {
if (offset === 0) {
return 0;
- }
-
- // Try to delete emoji part.
- const emojiOffset = getOffsetBeforeLastEmojiComponent(offset, str);
- if (emojiOffset !== undefined) {
- return emojiOffset;
- }
-
- // Otherwise, just skip a single code point.
- const iterator = new CodePointIterator(str, offset);
- iterator.prevCodePoint();
+ { … 11 line(s) … ⟦tj:0ba57d4b8aaf366e66263e58719f5c80⟧ }
return iterator.offset;
}
function getOffsetBeforeLastEmojiComponent(initialOffset: number, str: string): number | undefined {
// See https://www.unicode.org/reports/tr51/tr51-14.html#EBNF_and_Regex for the
// structure of emojis.
- const iterator = new CodePointIterator(str, initialOffset);
- let codePoint = iterator.prevCodePoint();
-
- // Skip modifiers
- while ((isEmojiModifier(codePoint) || codePoint === CodePoint.emojiVariantSelector || codePoint === CodePoint.enclosingKeyCap)) {
- if (iterator.offset === 0) {
- // Cannot skip modifier, no preceding emoji base.
- return undefined;
- }
- codePoint = iterator.prevCodePoint();
- }
-
- // Expect base emoji
- if (!isEmojiImprecise(codePoint)) {
- // Unexpected code point, not a valid emoji.
- return undefined;
- }
-
- let resultOffset = iterator.offset;
-
- if (resultOffset > 0) {
- // Skip optional ZWJ code points that combine multiple emojis.
- // In theory, we should check if that ZWJ actually combines multiple emojis
- // to prevent deleting ZWJs in situations we didn't account for.
- const optionalZwjCodePoint = iterator.prevCodePoint();
- if (optionalZwjCodePoint === CodePoint.zwj) {
- resultOffset = iterator.offset;
- }
- }
-
+ { … 30 line(s) … ⟦tj:69133285877b21d7812c34fac1f37d7f⟧ }
return resultOffset;
}
@@ -1184,59 +685,9 @@
>({ getCacheKey: JSON.stringify }, (locales) => {
function arrayToMap(arr: number[]): Map<number, number> {
const result = new Map<number, number>();
- for (let i = 0; i < arr.length; i += 2) {
- result.set(arr[i], arr[i + 1]);
- }
- return result;
- }
-
- function mergeMaps(
- map1: Map<number, number>,
- map2: Map<number, number>
- ): Map<number, number> {
- const result = new Map<number, number>(map1);
- for (const [key, value] of map2) {
- result.set(key, value);
- }
- return result;
- }
-
- function intersectMaps(
- map1: Map<number, number> | undefined,
- map2: Map<number, number>
- ) {
- if (!map1) {
- return map2;
- }
- const result = new Map<number, number>();
- for (const [key, value] of map1) {
- if (map2.has(key)) {
- result.set(key, value);
- }
- }
- return result;
- }
-
- const data = this.ambiguousCharacterData.value;
-
- let filteredLocales = locales.filter(
- (l) => !l.startsWith('_') && l in data
- );
- if (filteredLocales.length === 0) {
- filteredLocales = ['_default'];
- }
-
- let languageSpecificMap: Map<number, number> | undefined = undefined;
- for (const locale of filteredLocales) {
- const map = arrayToMap(data[locale]);
- languageSpecificMap = intersectMaps(languageSpecificMap, map);
- }
-
- const commonMap = arrayToMap(data['_common']);
- const map = mergeMaps(commonMap, languageSpecificMap!);
-
+ { … 51 line(s) … ⟦tj:969a0c67bd72fedbb8bd07baaf557e95⟧ }
return new AmbiguousCharacters(map);
- });
+});
public static getInstance(locales: Set<string>): AmbiguousCharacters {
return AmbiguousCharacters.cache.get(Array.from(locales));
@@ -1301,18 +752,12 @@
return InvisibleCharacters.getData().has(codePoint);
}
- public static containsInvisibleCharacter(str: string): boolean {
- for (let i = 0; i < str.length; i++) {
- const codePoint = str.codePointAt(i);
- if (typeof codePoint === 'number' && InvisibleCharacters.isInvisibleCharacter(codePoint)) {
- return true;
- }
- }
- return false;
+ public static containsInvisibleCharacter(str: string): boolean { … 10 line(s) … ⟦tj:23d282f0a7bc7c1a7caf0c4fdd3b305e⟧ }
- }
-
public static get codePoints(): ReadonlySet<number> {
return InvisibleCharacters.getData();
}
}
+[omitted blocks are individually retrievable: call tinyjuice_retrieve with the token inside an omission marker to expand just that block]
+
+[PARTIAL view — full original (88368 bytes): call tinyjuice_retrieve with token "88f2644577e2bf476f49d323d0afa9d7"]
\ No newline at end of file