ascii_domain 0.6.6

Parser for DNS names based on a provided ASCII character set.
Documentation
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use core::{
    error::Error,
    fmt::{self, Display, Formatter},
    str,
};
/// Error returned from [`AllowedAscii::try_from_unique_ascii`].
#[expect(variant_size_differences, reason = "usize is fine in size")]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum AsciiErr {
    /// Since `AllowedAscii` only allows unique ASCII characters and doesn't allow `b'.'`, the maximum count is
    /// 127. This variant is returned when the count exceeds that.
    CountTooLarge(usize),
    /// The contained `u8` is not valid ASCII (i.e., it is strictly greater than 127).
    InvalidByte(u8),
    /// `b'.'` was in the allowed ASCII. It is the only ASCII value not allowed since it is always used
    /// as a [`crate::dom::Label`] separator.
    Contains46,
    /// The contained ASCII appeared more than once.
    Duplicate(u8),
}
impl Display for AsciiErr {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Self::CountTooLarge(byt) => {
                write!(f, "the allowed ASCII had {byt} values, but 127 is the max")
            }
            Self::InvalidByte(byt) => {
                write!(f, "allowed ASCII was passed the invalid byte value {byt}")
            }
            Self::Contains46 => f.write_str("allowed ASCII contains '.'"),
            Self::Duplicate(byt) => {
                let input = [byt];
                if let Ok(val) = str::from_utf8(input.as_slice()) {
                    write!(f, "allowed ASCII has the duplicate value '{val}'")
                } else {
                    write!(f, "allowed ASCII has the invalid value '{byt}'")
                }
            }
        }
    }
}
impl Error for AsciiErr {}
/// Container of the ASCII `u8`s that are allowed to appear in a [`crate::dom::Label`].
///
/// Note that while
/// [`crate::dom::Domain`] treats ASCII uppercase letters as lowercase, it still depends on such `u8`s being
/// included. For example if `b'A'` is not included, then `b'A'` is not allowed even if `b'a'` is included.
///
/// It is _highly_ unlikely that non-printable ASCII nor `b'\\'` should be used since such ASCII would almost
/// certainly require being escaped.
#[derive(Debug)]
pub struct AllowedAscii<T> {
    /// The allowed ASCII `u8`s.
    allowed: T,
}
impl<T> AllowedAscii<T> {
    /// Returns a reference to the contained value.
    ///
    /// # Example
    ///
    /// ```
    /// use ascii_domain::char_set;
    /// assert!(char_set::ASCII_LETTERS.as_inner().len() == 52);
    /// ```
    #[inline]
    pub const fn as_inner(&self) -> &T {
        &self.allowed
    }
    /// Returns the contained value consuming `self`.
    ///
    /// # Example
    ///
    /// ```
    /// use ascii_domain::char_set;
    /// assert!(char_set::ASCII_LETTERS.into_inner().len() == 52);
    /// ```
    #[inline]
    pub fn into_inner(self) -> T {
        self.allowed
    }
}
impl<T: AsRef<[u8]>> AllowedAscii<T> {
    /// Returns `true` iff `val` is an allowed ASCII value in a [`crate::dom::Label`].
    ///
    /// # Example
    ///
    /// ```
    /// use ascii_domain::char_set;
    /// assert!(char_set::ASCII_LETTERS.contains(b'a'));
    /// ```
    #[inline]
    #[must_use]
    pub fn contains(&self, val: u8) -> bool {
        // We sort `allowed` in `try_from_unique_ascii`, so `binary_search` is fine.
        self.allowed.as_ref().binary_search(&val).is_ok()
    }
    /// Returns the number of allowed ASCII characters.
    ///
    /// # Example
    ///
    /// ```
    /// use ascii_domain::char_set;
    /// assert!(char_set::ASCII_LETTERS.len() == 52);
    /// ```
    #[expect(
        clippy::as_conversions,
        clippy::cast_possible_truncation,
        reason = "comment justifies its correctness"
    )]
    #[inline]
    #[must_use]
    pub fn len(&self) -> u8 {
        // We enforce only unique non `b'.'` ASCII in `try_from_unique_ascii` which among other things means
        // the max count is 127 so truncation will not occur
        self.allowed.as_ref().len() as u8
    }
    /// Returns `true` iff `self` does not contain any ASCII.
    ///
    /// # Examples
    ///
    /// ```
    /// use ascii_domain::char_set::{self, AllowedAscii};
    /// assert!(!char_set::ASCII_LETTERS.is_empty());
    /// assert!(AllowedAscii::try_from_unique_ascii([]).unwrap().is_empty());
    /// ```
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.allowed.as_ref().is_empty()
    }
}
impl<T: AsMut<[u8]>> AllowedAscii<T> {
    /// `allowed` must contain unique ASCII `u8`s. Note it is likely `allowed` should be a subset of
    /// [`PRINTABLE_ASCII`] since any other ASCII would likely require some form of escape character logic.
    /// Additionally, it is likely `allowed.as_mut().len()` should be greater than 0; otherwise the returned
    /// `AllowedAscii` will always cause [`crate::dom::Domain::try_from_bytes`] to error since `Domain` requires
    /// at least one non-root [`crate::dom::Label`].
    ///
    /// `allowed` is mutated such that `allowed.as_mut()` is sorted in order.
    ///
    /// # Errors
    ///
    /// Returns `AsciiError` iff `allowed` does not contain a set of unique ASCII `u8`s or contains `b'.'`.
    ///
    /// # Examples
    ///
    /// ```
    /// use ascii_domain::char_set::{AllowedAscii, AsciiErr};
    /// assert!(AllowedAscii::try_from_unique_ascii(b"asdfghjkl".to_owned()).map_or(false, |ascii| ascii.contains(b'a') && !ascii.contains(b'A')));
    /// assert!(AllowedAscii::try_from_unique_ascii(b"aa".to_owned()).map_or_else(|err| err == AsciiErr::Duplicate(b'a'), |_| false));
    /// assert!(AllowedAscii::try_from_unique_ascii([255]).map_or_else(|err| err == AsciiErr::InvalidByte(255), |_| false));
    /// assert!(AllowedAscii::try_from_unique_ascii([0; 128]).map_or_else(|err| err == AsciiErr::CountTooLarge(128), |_| false));
    /// assert!(AllowedAscii::try_from_unique_ascii([b'.']).map_or_else(|err| err == AsciiErr::Contains46, |_| false));
    /// ```
    #[inline]
    pub fn try_from_unique_ascii(mut allowed: T) -> Result<Self, AsciiErr> {
        let bytes = allowed.as_mut();
        if bytes.len() > 127 {
            Err(AsciiErr::CountTooLarge(bytes.len()))
        } else {
            bytes.sort_unstable();
            // Since `bytes` is sorted, we simply have to check the last value to determine if valid ASCII was
            // provided.
            if let Some(byt) = bytes.last() {
                let b = *byt;
                if b > 127 {
                    return Err(AsciiErr::InvalidByte(b));
                }
            }
            bytes
                .iter()
                // 255 is not valid ASCII, so we can use it as an initializer.
                .try_fold(255, |prev, b| {
                    let byt = *b;
                    if byt == b'.' {
                        Err(AsciiErr::Contains46)
                    } else if prev == byt {
                        Err(AsciiErr::Duplicate(prev))
                    } else {
                        Ok(byt)
                    }
                })
                .map(|_| Self { allowed })
        }
    }
}
/// Printable ASCII that should not need to be "escaped".
///
/// That is to say printable ASCII excluding space (i.e., 32), dot (i.e. 46), and backslash (i.e., 92).
/// This returns all `u8`s inclusively between 33 and 126 except 46 and 92.
pub const PRINTABLE_ASCII: AllowedAscii<[u8; 92]> = AllowedAscii {
    allowed: [
        b'!', b'"', b'#', b'$', b'%', b'&', b'\'', b'(', b')', b'*', b'+', b',', b'-', b'/', b'0',
        b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b':', b';', b'<', b'=', b'>', b'?',
        b'@', b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N',
        b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'[', b']', b'^',
        b'_', b'`', b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm',
        b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z', b'{', b'|',
        b'}', b'~',
    ],
};
/// ASCII allowed in [RFC 5322 `atext`](https://www.rfc-editor.org/rfc/rfc5322#section-3.2.3).
/// This contains the following `u8`s:
///
/// 33, 35–39, 42–43, 45, 47–57, 61, 63, 65–90, and 94–126.
pub const RFC5322_ATEXT: AllowedAscii<[u8; 81]> = AllowedAscii {
    allowed: [
        b'!', b'#', b'$', b'%', b'&', b'\'', b'*', b'+', b'-', b'/', b'0', b'1', b'2', b'3', b'4',
        b'5', b'6', b'7', b'8', b'9', b'=', b'?', b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H',
        b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W',
        b'X', b'Y', b'Z', b'^', b'_', b'`', b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i',
        b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x',
        b'y', b'z', b'{', b'|', b'}', b'~',
    ],
};
/// ASCII allowed in a domain by [Firefox](https://www.mozilla.org/en-US/firefox/)
/// as of 2023-09-03T20:50+00:00.
/// This contains the following `u8`s:
///
/// 33, 36, 38–41, 43–45, 48–57, 59, 61, 65–90, 95–123, and 125–126.
pub const ASCII_FIREFOX: AllowedAscii<[u8; 78]> = AllowedAscii {
    allowed: [
        b'!', b'$', b'&', b'\'', b'(', b')', b'+', b',', b'-', b'0', b'1', b'2', b'3', b'4', b'5',
        b'6', b'7', b'8', b'9', b';', b'=', b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I',
        b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X',
        b'Y', b'Z', b'_', b'`', b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k',
        b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z',
        b'{', b'}', b'~',
    ],
};
/// ASCII hyphen, digits, and letters.
/// This contains 45 and all `u8`s inclusively between 48 and 57, 65 and 90, and 97 and 122.
pub const ASCII_HYPHEN_DIGITS_LETTERS: AllowedAscii<[u8; 63]> = AllowedAscii {
    allowed: [
        b'-', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D',
        b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S',
        b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h',
        b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w',
        b'x', b'y', b'z',
    ],
};
/// ASCII digits and letters.
/// This contains all `u8`s inclusively between 48 and 57, 65 and 90, and 97 and 122.
pub const ASCII_DIGITS_LETTERS: AllowedAscii<[u8; 62]> = AllowedAscii {
    allowed: [
        b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E',
        b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T',
        b'U', b'V', b'W', b'X', b'Y', b'Z', b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i',
        b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x',
        b'y', b'z',
    ],
};
/// ASCII letters.
/// This contains all `u8`s inclusively between 65 and 90 and 97 and 122.
pub const ASCII_LETTERS: AllowedAscii<[u8; 52]> = AllowedAscii {
    allowed: [
        b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O',
        b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'a', b'b', b'c', b'd',
        b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's',
        b't', b'u', b'v', b'w', b'x', b'y', b'z',
    ],
};
/// ASCII hyphen, digits, and uppercase letters.
/// This contains 45 and all `u8`s inclusively between 48 and 57 and 65 and 90.
pub const ASCII_HYPHEN_DIGITS_UPPERCASE: AllowedAscii<[u8; 37]> = AllowedAscii {
    allowed: [
        b'-', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D',
        b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S',
        b'T', b'U', b'V', b'W', b'X', b'Y', b'Z',
    ],
};
/// ASCII hyphen, digits, and lowercase letters.
/// This contains 45 and all `u8`s inclusively between 48 and 57 and 97 and 122.
pub const ASCII_HYPHEN_DIGITS_LOWERCASE: AllowedAscii<[u8; 37]> = AllowedAscii {
    allowed: [
        b'-', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'a', b'b', b'c', b'd',
        b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's',
        b't', b'u', b'v', b'w', b'x', b'y', b'z',
    ],
};
/// ASCII digits and uppercase letters.
/// This contains all `u8`s inclusively between 48 and 57 and 65 and 90.
pub const ASCII_DIGITS_UPPERCASE: AllowedAscii<[u8; 36]> = AllowedAscii {
    allowed: [
        b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E',
        b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T',
        b'U', b'V', b'W', b'X', b'Y', b'Z',
    ],
};
/// ASCII digits and lowercase letters.
/// This contains all `u8`s inclusively between 48 and 57 and 97 and 122.
pub const ASCII_DIGITS_LOWERCASE: AllowedAscii<[u8; 36]> = AllowedAscii {
    allowed: [
        b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'a', b'b', b'c', b'd', b'e',
        b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't',
        b'u', b'v', b'w', b'x', b'y', b'z',
    ],
};
/// ASCII uppercase letters.
/// This contains all `u8`s inclusively between 65 and 90.
pub const ASCII_UPPERCASE: AllowedAscii<[u8; 26]> = AllowedAscii {
    allowed: [
        b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O',
        b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z',
    ],
};
/// ASCII lowercase letters.
/// This contains all `u8`s inclusively between 97 and 122.
pub const ASCII_LOWERCASE: AllowedAscii<[u8; 26]> = AllowedAscii {
    allowed: [
        b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o',
        b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z',
    ],
};
/// ASCII digits.
/// This contains all `u8`s inclusively between 48 and 57.
pub const ASCII_DIGITS: AllowedAscii<[u8; 10]> = AllowedAscii {
    allowed: [b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9'],
};
/// ASCII that is not a [forbidden domain code point](https://url.spec.whatwg.org/#forbidden-domain-code-point).
///
/// This contains the following `u8`s:
///
/// 33, 34, 36, 38–45, 48–57, 59, 61, 65–90, 95–123, and 125–126
pub const WHATWG_VALID_DOMAIN_CODE_POINTS: AllowedAscii<[u8; 80]> = AllowedAscii {
    allowed: [
        b'!', b'"', b'$', b'&', b'\'', b'(', b')', b'*', b'+', b',', b'-', b'0', b'1', b'2', b'3',
        b'4', b'5', b'6', b'7', b'8', b'9', b';', b'=', b'A', b'B', b'C', b'D', b'E', b'F', b'G',
        b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V',
        b'W', b'X', b'Y', b'Z', b'_', b'`', b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i',
        b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x',
        b'y', b'z', b'{', b'}', b'~',
    ],
};
#[cfg(test)]
mod tests {
    extern crate alloc;
    use crate::char_set::{
        ASCII_DIGITS, ASCII_DIGITS_LETTERS, ASCII_DIGITS_LOWERCASE, ASCII_DIGITS_UPPERCASE,
        ASCII_FIREFOX, ASCII_HYPHEN_DIGITS_LETTERS, ASCII_HYPHEN_DIGITS_LOWERCASE,
        ASCII_HYPHEN_DIGITS_UPPERCASE, ASCII_LETTERS, ASCII_LOWERCASE, ASCII_UPPERCASE,
        AllowedAscii, AsciiErr, PRINTABLE_ASCII, RFC5322_ATEXT, WHATWG_VALID_DOMAIN_CODE_POINTS,
    };
    use alloc::{borrow::ToOwned, vec::Vec};
    #[test]
    fn try_from() {
        // Empty is allowed.
        assert!(AllowedAscii::try_from_unique_ascii([]).is_ok());
        // Duplicates are not allowed.
        assert!(
            AllowedAscii::try_from_unique_ascii(b"aba".to_owned())
                .map_or_else(|e| e == AsciiErr::Duplicate(b'a'), |_| false)
        );
        // `b'.'` is not allowed.
        assert!(
            AllowedAscii::try_from_unique_ascii(b"a.c".to_owned())
                .map_or_else(|e| e == AsciiErr::Contains46, |_| false)
        );
        // At most 127 bytes are allowed.
        assert!(
            AllowedAscii::try_from_unique_ascii([0; 128])
                .map_or_else(|e| e == AsciiErr::CountTooLarge(128), |_| false)
        );
        let mut all_ascii = (0..b'.').collect::<Vec<u8>>();
        let next = b'.' + 1;
        all_ascii.extend(next..=127);
        assert!(AllowedAscii::try_from_unique_ascii(all_ascii).is_ok());
        // Only ASCII is allowed.
        assert!(
            AllowedAscii::try_from_unique_ascii([255])
                .map_or_else(|e| e == AsciiErr::InvalidByte(255), |_| false)
        );
        assert!(
            AllowedAscii::try_from_unique_ascii(b"abcdef".to_owned()).map_or(false, |bytes| bytes
                .contains(b'a')
                && bytes.contains(b'b')
                && bytes.contains(b'c')
                && bytes.contains(b'd')
                && bytes.contains(b'e')
                && bytes.contains(b'f'))
        );
    }
    #[test]
    fn test_consts() {
        let letters = ASCII_LETTERS;
        assert!(letters.len() == 52);
        for i in b'A'..=b'Z' {
            assert!(letters.contains(i));
        }
        for i in b'a'..=b'z' {
            assert!(letters.contains(i));
        }
        let digits = ASCII_DIGITS;
        assert!(digits.len() == 10);
        for i in b'0'..=b'9' {
            assert!(digits.contains(i));
        }
        let lower = ASCII_LOWERCASE;
        assert!(lower.len() == 26);
        for i in b'a'..=b'z' {
            assert!(lower.contains(i));
        }
        let upper = ASCII_UPPERCASE;
        assert!(upper.len() == 26);
        for i in b'A'..=b'Z' {
            assert!(upper.contains(i));
        }
        let dig_let = ASCII_DIGITS_LETTERS;
        assert!(dig_let.len() == 62);
        for i in b'a'..=b'z' {
            assert!(dig_let.contains(i));
        }
        for i in b'0'..=b'9' {
            assert!(dig_let.contains(i));
        }
        for i in b'A'..=b'Z' {
            assert!(dig_let.contains(i));
        }
        let dig_lower = ASCII_DIGITS_LOWERCASE;
        assert!(dig_lower.len() == 36);
        for i in b'a'..=b'z' {
            assert!(dig_lower.contains(i));
        }
        for i in b'0'..=b'9' {
            assert!(dig_lower.contains(i));
        }
        let dig_upper = ASCII_DIGITS_UPPERCASE;
        assert!(dig_upper.len() == 36);
        for i in b'A'..=b'Z' {
            assert!(dig_upper.contains(i));
        }
        for i in b'0'..=b'9' {
            assert!(dig_upper.contains(i));
        }
        let ffox = ASCII_FIREFOX;
        assert!(ffox.len() == 78);
        for i in b'A'..=b'Z' {
            assert!(ffox.contains(i));
        }
        for i in b'a'..=b'z' {
            assert!(ffox.contains(i));
        }
        for i in b'0'..=b'9' {
            assert!(ffox.contains(i));
        }
        assert!(ffox.contains(b'!'));
        assert!(ffox.contains(b'$'));
        assert!(ffox.contains(b'&'));
        assert!(ffox.contains(b'\''));
        assert!(ffox.contains(b'('));
        assert!(ffox.contains(b')'));
        assert!(ffox.contains(b'+'));
        assert!(ffox.contains(b','));
        assert!(ffox.contains(b'-'));
        assert!(ffox.contains(b';'));
        assert!(ffox.contains(b'='));
        assert!(ffox.contains(b'_'));
        assert!(ffox.contains(b'`'));
        assert!(ffox.contains(b'{'));
        assert!(ffox.contains(b'}'));
        assert!(ffox.contains(b'~'));
        assert!(ASCII_HYPHEN_DIGITS_LETTERS.len() == 63);
        assert!(ASCII_HYPHEN_DIGITS_LETTERS.contains(b'-'));
        for i in b'A'..=b'Z' {
            assert!(ASCII_HYPHEN_DIGITS_LETTERS.contains(i));
        }
        for i in b'a'..=b'z' {
            assert!(ASCII_HYPHEN_DIGITS_LETTERS.contains(i));
        }
        for i in b'0'..=b'9' {
            assert!(ASCII_HYPHEN_DIGITS_LETTERS.contains(i));
        }
        let hyp_lower = ASCII_HYPHEN_DIGITS_LOWERCASE;
        assert!(hyp_lower.len() == 37);
        assert!(hyp_lower.contains(b'-'));
        for i in b'a'..=b'z' {
            assert!(hyp_lower.contains(i));
        }
        for i in b'0'..=b'9' {
            assert!(hyp_lower.contains(i));
        }
        let hyp_upper = ASCII_HYPHEN_DIGITS_UPPERCASE;
        assert!(hyp_upper.len() == 37);
        assert!(hyp_upper.contains(b'-'));
        for i in b'A'..=b'Z' {
            assert!(hyp_upper.contains(i));
        }
        for i in b'0'..=b'9' {
            assert!(hyp_upper.contains(i));
        }
        let printable = PRINTABLE_ASCII;
        assert!(printable.len() == 92);
        let stop = b'.' - 1;
        for i in 33..=stop {
            assert!(printable.contains(i));
        }
        let stop2 = b'\\' - 1;
        for i in stop + 2..=stop2 {
            assert!(printable.contains(i));
        }
        for i in stop2 + 2..=b'~' {
            assert!(printable.contains(i));
        }
        let rfc = RFC5322_ATEXT;
        assert!(rfc.len() == 81);
        for i in b'A'..=b'Z' {
            assert!(rfc.contains(i));
        }
        for i in b'a'..=b'z' {
            assert!(rfc.contains(i));
        }
        for i in b'0'..=b'9' {
            assert!(rfc.contains(i));
        }
        assert!(rfc.contains(b'!'));
        assert!(rfc.contains(b'#'));
        assert!(rfc.contains(b'$'));
        assert!(rfc.contains(b'%'));
        assert!(rfc.contains(b'&'));
        assert!(rfc.contains(b'\''));
        assert!(rfc.contains(b'*'));
        assert!(rfc.contains(b'+'));
        assert!(rfc.contains(b'-'));
        assert!(rfc.contains(b'/'));
        assert!(rfc.contains(b'='));
        assert!(rfc.contains(b'?'));
        assert!(rfc.contains(b'^'));
        assert!(rfc.contains(b'_'));
        assert!(rfc.contains(b'`'));
        assert!(rfc.contains(b'{'));
        assert!(rfc.contains(b'|'));
        assert!(rfc.contains(b'}'));
        assert!(rfc.contains(b'~'));
        let whatwg = WHATWG_VALID_DOMAIN_CODE_POINTS;
        for i in 0..=0x1f {
            assert!(!whatwg.contains(i));
        }
        assert!(!whatwg.contains(b'\x20'));
        assert!(!whatwg.contains(b'#'));
        assert!(!whatwg.contains(b'/'));
        assert!(!whatwg.contains(b':'));
        assert!(!whatwg.contains(b'<'));
        assert!(!whatwg.contains(b'>'));
        assert!(!whatwg.contains(b'?'));
        assert!(!whatwg.contains(b'@'));
        assert!(!whatwg.contains(b'['));
        assert!(!whatwg.contains(b'\\'));
        assert!(!whatwg.contains(b']'));
        assert!(!whatwg.contains(b'^'));
        assert!(!whatwg.contains(b'|'));
        assert!(!whatwg.contains(b'%'));
        assert!(!whatwg.contains(b'\x7f'));
        assert!(!whatwg.contains(b'.'));
        assert!(whatwg.len() == 128 - 32 - 16);
    }
}