Skip to content

Commit 0433674

Browse files
committed
skip ascii path
1 parent f834067 commit 0433674

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/grapheme.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import { consonant_ranges } from './_incb_data.js';
3333

3434
export { GraphemeCategory };
3535

36+
const BMP_MAX = 0xFFFF;
37+
3638
/**
3739
* Unicode segmentation by extended grapheme rules.
3840
*
@@ -49,7 +51,7 @@ export function* graphemeSegments(input) {
4951
if (cp == null) return;
5052

5153
/** Current cursor position. */
52-
let cursor = cp <= 0xFFFF ? 1 : 2;
54+
let cursor = cp <= BMP_MAX ? 1 : 2;
5355

5456
/** Total length of the input string. */
5557
let len = input.length;
@@ -137,7 +139,7 @@ export function* graphemeSegments(input) {
137139
_hd = cp;
138140
}
139141

140-
cursor += cp <= 0xFFFF ? 1 : 2;
142+
cursor += cp <= BMP_MAX ? 1 : 2;
141143
catBefore = catAfter;
142144
}
143145

@@ -202,15 +204,14 @@ export function* splitGraphemes(text) {
202204
* For code points >= 0x10000 we fall back to binary search.
203205
*/
204206
let [bmpLookup, bmpCursor] = (() => {
205-
let max = 0xFFFF;
206-
let bmpLookup = new Uint8Array(max + 1);
207+
let bmpLookup = new Uint8Array(BMP_MAX + 1);
207208
let bmpCursor = 0;
208209
let cp = 0;
209210
while (true) {
210211
let range = grapheme_ranges[bmpCursor++];
211212
for (cp = range[0]; cp <= range[1];) {
212213
bmpLookup[cp++] = range[2];
213-
if (cp >= max) {
214+
if (cp > BMP_MAX) {
214215
return [bmpLookup, bmpCursor];
215216
}
216217
}
@@ -227,16 +228,11 @@ let [bmpLookup, bmpCursor] = (() => {
227228
* @return {GraphemeCategoryNum}
228229
*/
229230
function cat(cp, cache) {
230-
// Fast path for ASCII
231-
if (cp < 127) {
232-
if (cp >= 32) return 0 /* GC_Any */;
233-
if (cp === 10) return 6 /* GC_LF */;
234-
if (cp === 13) return 1 /* GC_CR */;
235-
return 2 /* GC_Control */;
236-
}
231+
if (cp === 10) return 6 /* GC_LF */;
232+
if (cp === 13) return 1 /* GC_CR */;
237233

238234
// Fast lookup for BMP (0x0000..0xFFFF) using precomputed table
239-
if (cp < bmpLookup.length) {
235+
if (cp <= BMP_MAX) {
240236
return /** @type {GraphemeCategoryNum} */ (bmpLookup[cp]);
241237
}
242238

0 commit comments

Comments
 (0)