diff --git a/developer/src/kmc-kmn/src/compiler/compiler.ts b/developer/src/kmc-kmn/src/compiler/compiler.ts index 33d1b545f5..c3c3bb3256 100644 --- a/developer/src/kmc-kmn/src/compiler/compiler.ts +++ b/developer/src/kmc-kmn/src/compiler/compiler.ts @@ -421,6 +421,12 @@ export class KmnCompiler implements UnicodeSetParser { return Module.kmcmp_testSentry(); } + /** convert `\u{1234}` to `\u1234` */ + public static fixNewPattern(pattern: string) : string { + return pattern.replaceAll(/\\u\{([0-9a-fA-F]{4})\}/g, `\\u$1`); + // TODO-LDML: other lengths! #9515 + } + /** * * @param pattern UnicodeSet pattern such as `[a-z]` @@ -435,6 +441,8 @@ export class KmnCompiler implements UnicodeSetParser { // TODO-LDML: Catch OOM const buf = this.wasmExports.malloc(rangeCount * 2 * Module.HEAPU32.BYTES_PER_ELEMENT); + // fix \u1234 pattern format + pattern = KmnCompiler.fixNewPattern(pattern); /** If <= 0: return code. If positive: range count */ const rc = Module.kmcmp_parseUnicodeSet(pattern, buf, rangeCount * 2); if (rc >= 0) { @@ -459,6 +467,8 @@ export class KmnCompiler implements UnicodeSetParser { /* c8 ignore next 2 */ return null; } + // fix \u1234 pattern format + pattern = KmnCompiler.fixNewPattern(pattern); // call with rangeCount = 0 to invoke in 'preflight' mode. const rc = Module.kmcmp_parseUnicodeSet(pattern, 0, 0); if (rc >= 0) { diff --git a/developer/src/kmc-kmn/test/test-wasm-uset.ts b/developer/src/kmc-kmn/test/test-wasm-uset.ts index a924f9fe05..3d9c4e0139 100644 --- a/developer/src/kmc-kmn/test/test-wasm-uset.ts +++ b/developer/src/kmc-kmn/test/test-wasm-uset.ts @@ -6,6 +6,18 @@ import { CompilerMessages } from '../src/compiler/messages.js'; import { compilerErrorFormatCode } from '@keymanapp/common-types'; describe('Compiler UnicodeSet function', function() { + it('should fixup \\u1234 format escapes', function() { + assert.equal(KmnCompiler.fixNewPattern(`\\u{1234}`), `\\u1234`); + assert.equal(KmnCompiler.fixNewPattern(`\\u1234`), `\\u1234`); + assert.equal(KmnCompiler.fixNewPattern(`[\\u{1234}-\\u{5678}]`), `[\\u1234-\\u5678]`); + assert.equal(KmnCompiler.fixNewPattern(`something else`), `something else`); + }); + + it.skip('should fixup more creative \\u format escapes', function() { + assert.equal(KmnCompiler.fixNewPattern(`\\u{22}`), `\\u0022`); // " + assert.equal(KmnCompiler.fixNewPattern(`\\u{1F640}`), `\\uD83D\\uDE40`); // TODO-LDM #9515: or something, 🙀 + }); + it('should start', async function() { const compiler = new KmnCompiler(); const callbacks = new TestCompilerCallbacks();