From 907ae53f11db1a011575f5a048a659029996abe8 Mon Sep 17 00:00:00 2001 From: Louis-Dominique Dubeau Date: Fri, 6 Sep 2019 12:34:54 -0400 Subject: [PATCH] feat: add isCharAndNotRestricted to xml/1.1/ed2 --- src/xml/1.1/ed2.ts | 22 ++++++++++++++++++++++ test/xmlchars.spec.ts | 12 ++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/xml/1.1/ed2.ts b/src/xml/1.1/ed2.ts index 4aa6cde..de8396d 100644 --- a/src/xml/1.1/ed2.ts +++ b/src/xml/1.1/ed2.ts @@ -81,6 +81,28 @@ export function isRestrictedChar(c: number): boolean { (c >= 0x86 && c <= 0x9F); } +/** + * Determines whether a codepoint matches the ``CHAR`` production and does not + * match the ``RESTRICTED_CHAR`` production. ``isCharAndNotRestricted(x)`` is + * equivalent to ``isChar(x) && !isRestrictedChar(x)``. This function is faster + * than running the two-call equivalent. + * + * @param c The code point. + * + * @returns ``true`` if the codepoint matches ``CHAR`` and does not match + * ``RESTRICTED_CHAR``. + */ +export function isCharAndNotRestricted(c: number): boolean { + return (c === 0x9) || + (c === 0xA) || + (c === 0xD) || + (c > 0x1F && c < 0x7F) || + (c === 0x85) || + (c > 0x9F && c <= 0xD7FF) || + (c >= 0xE000 && c <= 0xFFFD) || + (c >= 0x10000 && c <= 0x10FFFF); +} + /** * Determines whether a codepoint matches the ``S`` (space) production. * diff --git a/test/xmlchars.spec.ts b/test/xmlchars.spec.ts index 5752b9f..dd0148f 100644 --- a/test/xmlchars.spec.ts +++ b/test/xmlchars.spec.ts @@ -324,6 +324,12 @@ describe("xml/1.1", () => { }, }; + // tslint:disable-next-line:mocha-no-side-effect-code + const isCharAndNotRestrictedCase: Case = { + matching: cases.CHAR_RE.matching + .filter(c => !cases.RESTRICTED_CHAR_RE.matching.includes(c)), + }; + describe("regexes", () => { // tslint:disable-next-line:mocha-no-side-effect-code for (const name of (Object.keys(cases) as (keyof typeof cases)[])) { @@ -345,6 +351,12 @@ describe("xml/1.1", () => { cases.RESTRICTED_CHAR_RE); }); + describe(".isCharAndNotRestricted", () => { + // tslint:disable-next-line:mocha-no-side-effect-code + makeCodePointTestTests(xml_1_1_ed2.isCharAndNotRestricted, + isCharAndNotRestrictedCase); + }); + describe(".isS", () => { // tslint:disable-next-line:mocha-no-side-effect-code makeCodePointTestTests(xml_1_1_ed2.isS, cases.S_RE);