feat(common): preflighting on uset 🙀

- update docs on UnicodeSet
- uset parser: fix/clarify that the wasm interface takes a bufferSize, but the ts interface takes a range count. This was muddled before.

For: #7377
This commit is contained in:
Steven R. Loomis 2023-07-04 10:26:50 -05:00
parent 0f0c1e5e61
commit 52b8dfb205
2 changed files with 15 additions and 11 deletions

View file

@ -6,9 +6,9 @@ export interface UnicodeSetParser {
/**
* Parse a UnicodeSet into ranges
* @param pattern string to parse such as `[a-z]`
* @param bufferSize number of ranges to allow for
* @param rangeCount number of ranges to allow for
*/
parseUnicodeSet(pattern: string, bufferSize: number) : UnicodeSet | null;
parseUnicodeSet(pattern: string, rangeCount: number) : UnicodeSet | null;
/**
* Calculate the number of ranges in a UnicodeSet
* @param pattern string to parse such as `[a-z]`
@ -18,9 +18,16 @@ export interface UnicodeSetParser {
}
/**
* Represents a parsed UnicodeSet
* Parsed UnicodeSet, return value of of parseUnicodeSet()
*/
export class UnicodeSet {
/**
* A UnicodeSet in range form.
* For example, `[ħa-z]` will parse to
* ranges = `[[0x41, 0x7A], [0x0127, 0x0127]]` meaning `[a,z], [ħ,ħ]`
* @param pattern the pattern per UnicodeSet syntax such as `[a-z]`
* @param ranges array of 2-element arrays, 'start' and 'end'.
*/
constructor(public pattern: string, public ranges: number[][]) {
}
/**

View file

@ -228,22 +228,19 @@ export class KmnCompiler implements UnicodeSetParser {
/**
*
* @param pattern UnicodeSet pattern such as `[a-z]`
* @param bufferSize guess as to the buffer size
* @param rangeCount number of ranges to allocate
* @returns UnicodeSet accessor object, or null on failure
*/
public parseUnicodeSet(pattern: string, bufferSize: number) : UnicodeSet | null {
public parseUnicodeSet(pattern: string, rangeCount: number) : UnicodeSet | null {
if(!this.verifyInitialized()) {
/* c8 ignore next 2 */
return null;
}
if (!bufferSize) {
/* c8 ignore next 2 */
bufferSize = 100; // TODO-LDML: Preflight mode? Reuse buffer?
}
const buf = this.Module.asm.malloc(bufferSize * 2 * this.Module.HEAPU32.BYTES_PER_ELEMENT);
const buf = this.Module.asm.malloc(rangeCount * 2 * this.Module.HEAPU32.BYTES_PER_ELEMENT);
// TODO-LDML: Catch OOM
const rc = this.Module.kmcmp_parseUnicodeSet(pattern, buf, bufferSize);
/** return code, if positive: range count */
const rc = this.Module.kmcmp_parseUnicodeSet(pattern, buf, rangeCount * 2);
if (rc >= 0) {
const ranges = [];
const startu = (buf / this.Module.HEAPU32.BYTES_PER_ELEMENT);