-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10692 from keymanapp/feat/web/wordbreaker-data-op…
…timization feat(web): optimize the wordbreaker data table for filesize and ease of first-load parsing ⚡
- Loading branch information
Showing
4 changed files
with
146 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
common/models/wordbreakers/src/main/default/searchForProperty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { WordBreakProperty, WORD_BREAK_PROPERTY_BMP, WORD_BREAK_PROPERTY_NON_BMP } from "./data.inc.js"; | ||
|
||
export function searchForProperty(codePoint: number): WordBreakProperty { | ||
const bucketSize = codePoint <= 0xFFFF ? 2 : 3; | ||
|
||
// SMP chars take a bit more space to encode. | ||
const encodedArray = bucketSize == 2 ? WORD_BREAK_PROPERTY_BMP : WORD_BREAK_PROPERTY_NON_BMP; | ||
|
||
return _searchForProperty(encodedArray, codePoint, bucketSize, 0, encodedArray.length / bucketSize - 1) - 0x20; | ||
} | ||
|
||
/** | ||
* Binary search for the word break property of a given CODE POINT. | ||
* | ||
* The auto-generated data.ts master strings encode **character range** | ||
* lookup tables. If a character's codepoint is equal to or greater than | ||
* the start-of-range value for an entry and exclusively less than the next | ||
* entry's start-of-range, it falls within the first entry's range bucket | ||
* and is classified accordingly by this method. | ||
*/ | ||
function _searchForProperty(encodedArray: string, codePoint: number, bucketSize: number, left: number, right: number): WordBreakProperty { | ||
// All items that are not found in the array are assigned the 'Other' property. | ||
if (right < left) { // May need special handling at end of BMP / start of non-BMP. | ||
return WordBreakProperty.Other; | ||
} | ||
|
||
let midpoint = left + ~~((right - left) / 2); | ||
let candidate = encodedArray.codePointAt(bucketSize * midpoint); | ||
|
||
// If out-of-bounds, gives NaN. | ||
let nextRange = encodedArray.codePointAt(bucketSize * (midpoint + 1)); | ||
let startOfNextRange = isNaN(nextRange) ? Infinity : nextRange; | ||
|
||
if (codePoint < candidate) { | ||
return _searchForProperty(encodedArray, codePoint, bucketSize, left, midpoint - 1); | ||
} else if (codePoint >= startOfNextRange) { | ||
return _searchForProperty(encodedArray, codePoint, bucketSize, midpoint + 1, right); | ||
} | ||
|
||
// We found it! | ||
const propertyCode = encodedArray.charCodeAt(bucketSize * (midpoint + 1) - 1); | ||
return propertyCode as WordBreakProperty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Smoke-test the default | ||
*/ | ||
|
||
import { assert } from 'chai'; | ||
import { searchForProperty } from '../build/obj/default/searchForProperty.js'; | ||
import { propertyMap } from '../build/obj/default/data.inc.js'; | ||
|
||
describe('searchForProperty', () => { | ||
it('correctly finds character classes for standard ASCII characters', () => { | ||
assert.equal(searchForProperty('a'.codePointAt(0)), propertyMap.indexOf('ALetter')); | ||
assert.equal(searchForProperty('Z'.codePointAt(0)), propertyMap.indexOf('ALetter')); | ||
|
||
assert.equal(searchForProperty("'".codePointAt(0)), propertyMap.indexOf('Single_Quote')); | ||
assert.equal(searchForProperty('"'.codePointAt(0)), propertyMap.indexOf('Double_Quote')); | ||
assert.equal(searchForProperty(','.codePointAt(0)), propertyMap.indexOf('MidNum')); | ||
assert.equal(searchForProperty('.'.codePointAt(0)), propertyMap.indexOf('MidNumLet')); | ||
assert.equal(searchForProperty('-'.codePointAt(0)), propertyMap.indexOf('Other')); | ||
}); | ||
|
||
it('correctly finds character classes for specialized BMP characters', () => { | ||
assert.equal(searchForProperty(0x05D0), propertyMap.indexOf('Hebrew_Letter')); | ||
assert.equal(searchForProperty(0x3031), propertyMap.indexOf('Katakana')); | ||
assert.equal(searchForProperty(0xFFFE), propertyMap.indexOf('Other')); | ||
assert.equal(searchForProperty(0xFFFF), propertyMap.indexOf('Other')); | ||
}); | ||
|
||
it('correctly finds character classes for non-BMP characters', () => { | ||
assert.equal(searchForProperty(0x0001F1E6), propertyMap.indexOf('Regional_Indicator')); | ||
assert.equal(searchForProperty(0x00013430), propertyMap.indexOf('Format')); | ||
assert.equal(searchForProperty(0x00010000), propertyMap.indexOf('ALetter')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters