Skip to content

Commit

Permalink
feat(common): fix marker parsing 🙀
Browse files Browse the repository at this point in the history
- cleaned up regex

For: #9119
  • Loading branch information
srl295 committed Oct 6, 2023
1 parent 8f4be0c commit 25c20ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 8 additions & 2 deletions common/web/types/src/ldml-keyboard/pattern-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ export class MarkerParser {
public static readonly MAX_MARKER_INDEX = constants.marker_max_index;
/** Max count of markers */
public static readonly MAX_MARKER_COUNT = constants.marker_max_count;

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}]`;
}

/** Expression that matches any marker */
public static readonly ANY_MARKER_MATCH =
this.SENTINEL + this.MARKER_CODE + `[\\u0001-\\u${this.MAX_MARKER_INDEX.toString(16)}]`;
public static readonly ANY_MARKER_MATCH = MarkerParser.anyMarkerMatch();

/**
* Pattern for matching a marker reference, OR the special marker \m{.}
Expand Down
14 changes: 9 additions & 5 deletions common/web/types/test/ldml-keyboard/test-pattern-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,20 @@ describe('Test of Pattern Parsers', () => {
)
);
// verify the matching behavior of these
assert.isTrue(new RegExp(MarkerParser.toSentinelString(`Q\\m{a}`, markers, true), 'u')
assert.isTrue(new RegExp(MarkerParser.toSentinelString(`^Q\\m{a}$`, markers, true), 'u')
.test(MarkerParser.toSentinelString(`Q\\m{a}`, markers, false)), `Q\\m{a} did not match`);
assert.isFalse(new RegExp(MarkerParser.toSentinelString(`Q\\m{a}`, markers, true), 'u')
assert.isFalse(new RegExp(MarkerParser.toSentinelString(`^Q\\m{a}$`, markers, true), 'u')
.test(MarkerParser.toSentinelString(`Q\\m{b}`, markers, false)), `Q\\m{a} should not match Q\\m{b}`);
assert.isTrue(new RegExp(MarkerParser.toSentinelString(`Q\\m{.}`, markers, true), 'u')
assert.isTrue(new RegExp(MarkerParser.toSentinelString(`^Q\\m{.}$`, markers, true), 'u')
.test(MarkerParser.toSentinelString(`Q\\m{a}`, markers, false)), `Q\\m{.} did not match Q\\m{a}`);
assert.isTrue(new RegExp(MarkerParser.toSentinelString(`Q\\m{.}`, markers, true), 'u')
assert.isTrue(new RegExp(MarkerParser.toSentinelString(`^Q\\m{.}$`, markers, true), 'u')
.test(MarkerParser.toSentinelString(`Q\\m{zz}`, markers, false)), `Q\\m{.} did not match Q\\m{zz} (max marker)`);
assert.isFalse(new RegExp(MarkerParser.toSentinelString(`Q\\m{.}`, markers, true), 'u')
assert.isFalse(new RegExp(MarkerParser.toSentinelString(`^Q\\m{.}$`, markers, true), 'u')
.test(MarkerParser.toSentinelString(`\\m{a}`, markers, false)), `Q\\m{.} did not match \\m{a}`);
assert.isTrue(new RegExp(MarkerParser.toSentinelString(`^\\m{.}$`, markers, true), 'u')
.test(MarkerParser.toSentinelString(`\\m{a}`, markers, false)), `\\m{.} did not match \\m{a}`);
assert.isFalse(new RegExp(MarkerParser.toSentinelString(`^\\m{.}$`, markers, true), 'u')
.test(MarkerParser.toSentinelString(`\\m{a}\\m{b}`, markers, false)), `\\m{.} did not match \\m{a}\\m{b}`);
});
it('should match some marker constants', () => {
assert.equal(constants.uc_sentinel, KMXFile.UC_SENTINEL);
Expand Down

0 comments on commit 25c20ee

Please sign in to comment.