Skip to content

Commit b9ce9c5

Browse files
committed
using nullable reference is slow, use index pattern again
1 parent fb09d49 commit b9ce9c5

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

src/core.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ export function decodeUnicodeData(data, cats = '') {
6161
* @template {number} [T=number]
6262
* @param {number} cp
6363
* @param {CategorizedUnicodeRange<T>[]} ranges
64-
* @return {CategorizedUnicodeRange<T> | null}
64+
* @return {number} index of matched unicode range, or -1 if no match
6565
*/
66-
export function searchUnicodeRange(cp, ranges) {
66+
export function findUnicodeRangeIndex(cp, ranges) {
6767
let len = ranges.length
6868
, i = 1;
6969

7070
while (i <= len) {
7171
let range = ranges[i - 1]
7272
, l = range[0]
7373
, h = range[1];
74-
if (l <= cp && cp <= h) return range;
74+
if (l <= cp && cp <= h) return i - 1;
7575
i = 2 * i + (cp > h ? 1 : 0);
7676
}
77-
return null;
77+
return -1;
7878
}

src/emoji.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22

3-
import { searchUnicodeRange } from './core.js';
3+
import { findUnicodeRangeIndex } from './core.js';
44
import {
55
emoji_presentation_ranges,
66
extended_pictographic_ranges,
@@ -25,7 +25,7 @@ export function isEmoji(cp) {
2525
* @return boolean
2626
*/
2727
export function isEmojiPresentation(cp) {
28-
return searchUnicodeRange(cp, emoji_presentation_ranges) !== null;
28+
return findUnicodeRangeIndex(cp, emoji_presentation_ranges) >= 0;
2929
}
3030

3131
/**
@@ -35,5 +35,5 @@ export function isEmojiPresentation(cp) {
3535
* @return boolean
3636
*/
3737
export function isExtendedPictographic(cp) {
38-
return searchUnicodeRange(cp, extended_pictographic_ranges) !== null;
38+
return findUnicodeRangeIndex(cp, extended_pictographic_ranges) >= 0;
3939
}

src/general.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22

3-
import { searchUnicodeRange } from './core.js';
3+
import { findUnicodeRangeIndex } from './core.js';
44
import {
55
letter_ranges,
66
alphabetic_ranges,
@@ -14,7 +14,7 @@ import {
1414
* @return boolean
1515
*/
1616
export function isLetter(cp) {
17-
return searchUnicodeRange(cp, letter_ranges) !== null;
17+
return findUnicodeRangeIndex(cp, letter_ranges) >= 0;
1818
}
1919

2020
/**
@@ -24,7 +24,7 @@ export function isLetter(cp) {
2424
* @return boolean
2525
*/
2626
export function isAlphabetic(cp) {
27-
return searchUnicodeRange(cp, alphabetic_ranges) !== null;
27+
return findUnicodeRangeIndex(cp, alphabetic_ranges) >= 0;
2828
}
2929

3030
/**
@@ -34,7 +34,7 @@ export function isAlphabetic(cp) {
3434
* @return boolean true if
3535
*/
3636
export function isNumeric(cp) {
37-
return searchUnicodeRange(cp, numeric_ranges) !== null;
37+
return findUnicodeRangeIndex(cp, numeric_ranges) >= 0;
3838
}
3939

4040
/**

src/grapheme.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
// @ts-check
1515

16-
import { searchUnicodeRange } from './core.js';
16+
import { findUnicodeRangeIndex } from './core.js';
1717
import { isBMP } from './utils.js';
1818
import { GraphemeCategory, grapheme_ranges } from './_grapheme_data.js';
1919
import { consonant_ranges } from './_incb_data.js';
@@ -45,8 +45,11 @@ export {
4545
* @return A {@link GraphemeCategoryRange} value if found, or garbage value with {@link GC_Any} category.
4646
*/
4747
export function searchGraphemeCategory(cp) {
48-
let range = searchUnicodeRange(cp, grapheme_ranges);
49-
return range || [0, 0, 0 /* GC_Any */];
48+
let index = findUnicodeRangeIndex(cp, grapheme_ranges);
49+
if (index < 0) {
50+
return [0, 0, 0 /* GC_Any */];
51+
}
52+
return grapheme_ranges[index];
5053
}
5154

5255
/**
@@ -245,10 +248,11 @@ function cat(cp, cache) {
245248
// If this char isn't within the cached range, update the cache to the
246249
// range that includes it.
247250
if (cp < cache[0] || cp > cache[1]) {
248-
let range = searchUnicodeRange(cp, grapheme_ranges);
249-
if (!range) {
251+
let index = findUnicodeRangeIndex(cp, grapheme_ranges);
252+
if (index < 0) {
250253
return 0;
251254
}
255+
let range = grapheme_ranges[index];
252256
cache[0] = range[0];
253257
cache[1] = range[1];
254258
cache[2] = range[2];
@@ -262,7 +266,7 @@ function cat(cp, cache) {
262266
* @return {boolean}
263267
*/
264268
function isIndicConjunctCosonant(cp) {
265-
return searchUnicodeRange(cp, consonant_ranges) !== null;
269+
return findUnicodeRangeIndex(cp, consonant_ranges) >= 0;
266270
}
267271

268272
/**

0 commit comments

Comments
 (0)