-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquoted_string.ts
93 lines (85 loc) · 2.64 KB
/
quoted_string.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* ```abnf
* obs-text = %x80-FF
* qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
* ```
*/
const reQdtext = /^[\t \x21\x23-\x5B\x5D-\x7E\x80-\xFF]$/;
/** Whether the input is [qdtext](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.4-2) or not.
*
* @example
* ```ts
* import { isQdtext } from "https://deno.land/x/http_utils@$VERSION/quoted_string.ts";
* import {
* assert,
* assertFalse,
* } from "https://deno.land/std@$VERSION/testing/asserts.ts";
*
* assert(isQdtext("\t"));
* assert(isQdtext("\xFF"));
* assertFalse(isQdtext(`"`));
* ```
*/
export function isQdtext(input: string): boolean {
return reQdtext.test(input);
}
/**
* ```abnf
* obs-text = %x80-FF
* quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
* ```
*/
const reQuotedPair = /^\\[\t \x21-\x7E\x80-\xFF]$/;
/** [Quoted pair](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.4-4). */
export type QuotedPair = `\\${string}`;
/** Whether the input is [quoted-pair](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.4-4) or not.
*
* @example
* ```ts
* import { isQuotedPair } from "https://deno.land/x/http_utils@$VERSION/quoted_string.ts";
* import {
* assert,
* assertFalse,
* } from "https://deno.land/std@$VERSION/testing/asserts.ts";
*
* assert(isQuotedPair("\\\t"));
* assert(isQuotedPair("\\\xFF"));
* assertFalse(isQuotedPair("\\"));
* ```
*/
export function isQuotedPair(input: string): input is QuotedPair {
return reQuotedPair.test(input);
}
/** [quoted-string](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.4-2). */
export type QuotedString = `"${string}"`;
/**
* ```abnf
* quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
* qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
* quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
* obs-text = %x80-FF
* ```
*/
const reQuotedString =
/^"(?:[\t \x21\x23-\x5B\x5D-\x7E\x80-\xFF]|\\[\t \x21-\x7E\x80-\xFF])*?"$/;
/** Whether the input is [quoted-string](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.4-2) or not.
*
* @example
* ```ts
* import { isQuotedString } from "https://deno.land/x/http_utils@$VERSION/quoted_string.ts";
* import {
* assert,
* assertFalse,
* } from "https://deno.land/std@$VERSION/testing/asserts.ts";
*
* assert(isQuotedString(`""`));
* assert(isQuotedString(`"qdtext"`));
* assert(isQuotedString(`"quoted-pair"`));
* assertFalse(isQuotedString(""));
* ```
*/
export function isQuotedString(input: string): input is QuotedString {
return reQuotedString.test(input);
}