Skip to content

Commit

Permalink
feat: add isCharAndNotRestricted to xml/1.1/ed2
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Sep 6, 2019
1 parent bdf0a6c commit 907ae53
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/xml/1.1/ed2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions test/xmlchars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)[])) {
Expand All @@ -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);
Expand Down

0 comments on commit 907ae53

Please sign in to comment.