Skip to content

Commit 3c2f5a9

Browse files
committed
feat(quoted_string): add isQuotedString function
1 parent dfcd316 commit 3c2f5a9

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,24 @@ assert(isQdtext("\xFF"));
604604
assertFalse(isQdtext(`"`));
605605
```
606606

607+
### isQuotedPair
608+
609+
Whether the input is
610+
[quoted-pair](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.4-4) or
611+
not.
612+
613+
```ts
614+
import { isQuotedPair } from "https://deno.land/x/http_utils@$VERSION/quoted_string.ts";
615+
import {
616+
assert,
617+
assertFalse,
618+
} from "https://deno.land/std@$VERSION/testing/asserts.ts";
619+
620+
assert(isQuotedPair("\\\t"));
621+
assert(isQuotedPair("\\\xFF"));
622+
assertFalse(isQuotedPair("\\"));
623+
```
624+
607625
## License
608626

609627
Copyright © 2023-present [httpland](https://github.com/httpland).

quoted_string.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,32 @@ const reQdtext = /^[\t \x21\x23-\x5B\x5D-\x7E\x80-\xFF]$/;
3030
export function isQdtext(input: string): boolean {
3131
return reQdtext.test(input);
3232
}
33+
34+
/**
35+
* ```abnf
36+
* quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
37+
* ```
38+
*/
39+
const reQuotedPair = /^\\[\t \x21-\x7E\x80-\xFF]$/;
40+
41+
/** [Quoted pair](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.4-4). */
42+
export type QuotedPair = `\\${string}`;
43+
44+
/** Whether the input is [quoted-pair](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.4-4) or not.
45+
*
46+
* @example
47+
* ```ts
48+
* import { isQuotedPair } from "https://deno.land/x/http_utils@$VERSION/quoted_string.ts";
49+
* import {
50+
* assert,
51+
* assertFalse,
52+
* } from "https://deno.land/std@$VERSION/testing/asserts.ts";
53+
*
54+
* assert(isQuotedPair("\\\t"));
55+
* assert(isQuotedPair("\\\xFF"));
56+
* assertFalse(isQuotedPair("\\"));
57+
* ```
58+
*/
59+
export function isQuotedPair(input: string): input is QuotedPair {
60+
return reQuotedPair.test(input);
61+
}

quoted_string_test.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isQdtext } from "./quoted_string.ts";
1+
import { isQdtext, isQuotedPair } from "./quoted_string.ts";
22
import {
33
assert,
44
assertEquals,
@@ -19,6 +19,9 @@ function charRange(start: string, end: string): string[] {
1919
);
2020
}
2121

22+
const vchars = charRange("\x21", "\x7E");
23+
const obsTexts = charRange("\x80", "\xFF");
24+
2225
describe("charRange", () => {
2326
it("should throw error is the range is invalid", () => {
2427
const table: [string, string, string[]][] = [
@@ -37,15 +40,15 @@ describe("charRange", () => {
3740
});
3841
});
3942

40-
describe("describe", () => {
43+
describe("isQdtext", () => {
4144
it("should return true", () => {
4245
const table: string[] = [
4346
"\t",
4447
" ",
4548
"\x21",
4649
...charRange("\x23", "\x5B"),
4750
...charRange("\x5D", "\x7E"),
48-
...charRange("\x80", "\xFF"),
51+
...obsTexts,
4952
];
5053

5154
table.forEach((input) => {
@@ -60,11 +63,47 @@ describe("describe", () => {
6063
...charRange("\x10", "\x19"), // \x20 is " "
6164
"\x22",
6265
"\xFF" + 1,
63-
"\x22\x22",
66+
"\x21\x21",
6467
];
6568

6669
table.forEach((input) => {
6770
assertFalse(isQdtext(input), input);
6871
});
6972
});
7073
});
74+
75+
describe("isQuotedPair", () => {
76+
function slashed(input: string): `\\${string}` {
77+
return `\\${input}`;
78+
}
79+
80+
it("should return true", () => {
81+
const table: string[] = [
82+
"\\\t",
83+
"\\ ",
84+
...vchars.map(slashed),
85+
...obsTexts.map(slashed),
86+
];
87+
88+
table.forEach((input) => {
89+
assert(isQuotedPair(input));
90+
});
91+
});
92+
93+
it("should return false", () => {
94+
const table: string[] = [
95+
"",
96+
"\\",
97+
...charRange("\x00", "\x08").map(slashed), // \x09 is "\t"
98+
...charRange("\x10", "\x19").map(slashed), // \x20 is " "
99+
"\\\x7F",
100+
"\\" + "\xFF" + 1,
101+
102+
"\\\x21\x21",
103+
];
104+
105+
table.forEach((input) => {
106+
assertFalse(isQuotedPair(input), input);
107+
});
108+
});
109+
});

0 commit comments

Comments
 (0)