Skip to content

Commit

Permalink
fix(developer): escape markers as regex 🙀
Browse files Browse the repository at this point in the history
- escape markers to prevent the n'th marker from having some other meaning
  • Loading branch information
srl295 committed Dec 29, 2023
1 parent 561cecb commit dbd06e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
28 changes: 22 additions & 6 deletions common/web/types/src/ldml-keyboard/pattern-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ export class MarkerParser {
/** Max count of markers */
public static readonly MAX_MARKER_COUNT = constants.marker_max_count;

/** 0000 … FFFF */
private static hexQuad(n: number): string {
if (n < 0x000 || n > 0xFFFF) {
throw RangeError(`${n} not in [0x0000,0xFFFF]`);
}
return (`0000` + (n).toString(16)).slice(-4);
}

private static anyMarkerMatch() : string {
const start = (`0000` + (this.MIN_MARKER_INDEX).toString(16)).slice(-4);
const end = (`0000` + (this.MAX_MARKER_INDEX).toString(16)).slice(-4);
return `${this.SENTINEL}${this.MARKER_CODE}[\\u${start}-\\u${end}]`;
const start = MarkerParser.hexQuad(this.MIN_MARKER_INDEX);
const end = MarkerParser.hexQuad(this.MAX_MARKER_INDEX);
return `${this.SENTINEL}${this.MARKER_CODE}[\\u${start}-\\u${end}]`; // TODO-LDML: #9121 wrong escape format
}

/** Expression that matches any marker */
Expand All @@ -91,12 +99,20 @@ export class MarkerParser {
return matchArray(str, this.REFERENCE);
}

private static markerCodeToString(n: number, forMatch?: boolean): string {
if (!forMatch) {
return String.fromCharCode(n);
} else {
return `\\u${MarkerParser.hexQuad(n)}`; // TODO-LDML: #9121 wrong escape format
}
}

/** @returns string for marker #n */
public static markerOutput(n: number): string {
public static markerOutput(n: number, forMatch?: boolean): string {
if (n < MarkerParser.MIN_MARKER_INDEX || n > MarkerParser.ANY_MARKER_INDEX) {
throw RangeError(`Internal Error: marker index out of range ${n}`);
}
return this.SENTINEL + this.MARKER_CODE + String.fromCharCode(n);
return this.SENTINEL + this.MARKER_CODE + this.markerCodeToString(n, forMatch);
}

/** @returns all marker strings as sentinel values */
Expand All @@ -118,7 +134,7 @@ export class MarkerParser {
} else if(order > MarkerParser.MAX_MARKER_INDEX) {
throw RangeError(`Internal Error: marker \\m{${arg}} has out of range index ${order}`);
} else {
return MarkerParser.markerOutput(order + 1);
return MarkerParser.markerOutput(order + 1, forMatch);
}
});
}
Expand Down
10 changes: 5 additions & 5 deletions developer/src/kmc-ldml/test/fixtures/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ block(strs) # struct COMP_KMXPLUS_STRS {
diff(strs,strKeys) sizeof(strKeys,2)
diff(strs,strIndicator) sizeof(strIndicator,2)
diff(strs,strSentinel0001) sizeof(strSentinel0001,2)

diff(strs,strSentinel0001r) sizeof(strSentinel0001r,2)

# String table -- block(x) is used to store the null u16char at end of each string
# without interfering with sizeof() calculation above
Expand Down Expand Up @@ -555,8 +555,8 @@ block(strs) # struct COMP_KMXPLUS_STRS {
block(strKeys) 90 17 b6 17 block(x) 00 00 # 'ថា'
# <reorder before="ᩫ" from="᩠᩵ᩅ" order="10 55 10" />
block(strIndicator) 3d d8 40 de block(x) 00 00 # '🙀'
block(strSentinel0001) FF FF 08 00 01 00 block(x) 00 00 # UC_SENTINEL CODE_DEADKEY U+0001

block(strSentinel0001) FF FF 08 00 01 00 block(x) 00 00 # UC_SENTINEL CODE_DEADKEY U+0001
block(strSentinel0001r) FF FF 08 00 5C 00 75 00 30 00 30 00 30 00 31 00 block(x) 00 00 # UC_SENTINEL CODE_DEADKEY \u0001 (regex form)


block(endstrs) # end of strs block
Expand Down Expand Up @@ -598,12 +598,12 @@ block(tran) # struct COMP_KMXPLUS_TRAN {

block(tranTransform1)
index(strNull,strA,2) # KMXPLUS_STR from; 'a'
index(strNull,strSentinel0001,2) # KMXPLUS_STR to; \m{a}
index(strNull,strSentinel0001,2) # KMXPLUS_STR to; \m{a} (plain form)
index(strNull,strNull,2) # mapFrom
index(strNull,strNull,2) # mapTo

block(tranTransform2) # Next group
index(strNull,strSentinel0001,2) # KMXPLUS_STR from; (\m{a})
index(strNull,strSentinel0001r,2) # KMXPLUS_STR from; (\m{a}) (regex form)
index(strNull,strNull,2) # KMXPLUS_STR to; (none)
index(strNull,strNull,2) # mapFrom
index(strNull,strNull,2) # mapTo
Expand Down

0 comments on commit dbd06e9

Please sign in to comment.