parse-net 0.6.0

Grammar for Internet message fields: RFC 9110 field syntax, media types, RFC 9651 Structured Fields, HTTP and RFC 5322 dates, negotiation, entity tags, ranges, Content-Disposition, Cache-Control, Forwarded, auth challenges, and the email lexical layer. no_std, no allocation, sans-IO, zero dependencies.
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
//! The byte classes of the ABNF, in one table.
//!
//! One `static` table of 256 `u32` entries (1 KiB), built at compile time from
//! the ABNF, holds one bit per class, so classifying a byte is one load and
//! one mask. The table is the single reviewable source of every class; the
//! tests compare it, for all 256 bytes, with slow functions written straight
//! from the RFC text.
//!
//! Every class is ASCII-only except `OBS_TEXT`, `FIELD_TEXT`, `QDTEXT`,
//! `CTEXT`, `QUOTED_PAIR`, and `ETAGC`, which include 0x80 to 0xFF as
//! `obs-text`. DEL (0x7F) is in no class.
//!
//! # Examples
//!
//! ```
//! use parse_net::class::{self, Class};
//!
//! assert!(class::is_tchar(b'!') && !class::is_tchar(b'('));
//! assert!(class::is_mime_token_char(b'{') && !class::is_tchar(b'{'));
//! assert_eq!(class::hex_value(b'f'), Some(15));
//! assert!(class::all(b"no-cache", Class::TCHAR));
//! assert!(Class::UNRESERVED.union(Class::SUB_DELIM).contains(b'~'));
//! ```

const TCHAR: u32 = 1 << 0;
const VCHAR: u32 = 1 << 1;
const OBS_TEXT: u32 = 1 << 2;
const FIELD_TEXT: u32 = 1 << 3;
const QDTEXT: u32 = 1 << 4;
const CTEXT: u32 = 1 << 5;
const QUOTED_PAIR: u32 = 1 << 6;
const ETAGC: u32 = 1 << 7;
const ATTR_CHAR: u32 = 1 << 8;
const MIME_TOKEN: u32 = 1 << 9;
const UNRESERVED: u32 = 1 << 10;
const SUB_DELIM: u32 = 1 << 11;
const PCHAR_NO_PCT: u32 = 1 << 12;
const URI_TARGET: u32 = 1 << 13;
const HEXDIG: u32 = 1 << 14;
const SF_KEY_START: u32 = 1 << 15;
const SF_KEY: u32 = 1 << 16;
const SF_TOKEN: u32 = 1 << 17;
const SF_STRING: u32 = 1 << 18;
const TOKEN68: u32 = 1 << 19;
const BASE64: u32 = 1 << 20;
const ATEXT: u32 = 1 << 21;
const BCHAR_NOSPACE: u32 = 1 << 22;
const COOKIE_OCTET: u32 = 1 << 23;
const AV_OCTET: u32 = 1 << 24;
/// Internal: field text that is not a list delimiter (`,` `"` `(` `)`), so
/// the list walker can skip it with one lookup per byte.
const LIST_PLAIN: u32 = 1 << 26;

/// Whether `c` is one of the bytes in `set`.
const fn one_of(c: u8, set: &[u8]) -> bool {
    let mut rest = set;
    while let [first, tail @ ..] = rest {
        if *first == c {
            return true;
        }
        rest = tail;
    }
    false
}

const fn classify(c: u8) -> u32 {
    let alpha = c.is_ascii_alphabetic();
    let digit = c.is_ascii_digit();
    let alnum = alpha || digit;
    let vchar = c >= 0x21 && c <= 0x7e;
    let obs = c >= 0x80;
    let sp_ht = c == b' ' || c == b'\t';
    let mut class = 0u32;

    // RFC 9110 section 5.6.2.
    if alnum || one_of(c, b"!#$%&'*+-.^_`|~") {
        class |= TCHAR;
    }
    if vchar {
        class |= VCHAR;
    }
    if obs {
        class |= OBS_TEXT;
    }
    // RFC 9110 section 5.5: field-vchar / SP / HTAB.
    if vchar || obs || sp_ht {
        class |= FIELD_TEXT;
    }
    // RFC 9110 section 5.6.4: HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text.
    if sp_ht || c == 0x21 || (c >= 0x23 && c <= 0x5b) || (c >= 0x5d && c <= 0x7e) || obs {
        class |= QDTEXT;
    }
    // RFC 9110 section 5.6.5: HTAB / SP / %x21-27 / %x2A-5B / %x5D-7E / obs-text.
    if sp_ht
        || (c >= 0x21 && c <= 0x27)
        || (c >= 0x2a && c <= 0x5b)
        || (c >= 0x5d && c <= 0x7e)
        || obs
    {
        class |= CTEXT;
    }
    // The byte after "\": HTAB / SP / VCHAR / obs-text.
    if sp_ht || vchar || obs {
        class |= QUOTED_PAIR;
    }
    // RFC 9110 section 8.8.3: "!" / %x23-7E / obs-text.
    if c == 0x21 || (c >= 0x23 && c <= 0x7e) || obs {
        class |= ETAGC;
    }
    // RFC 8187 section 3.2.1.
    if alnum || one_of(c, b"!#$&+-.^_`|~") {
        class |= ATTR_CHAR;
    }
    // RFC 2045 section 5.1: any CHAR except SP, CTLs, and tspecials.
    if vchar && !one_of(c, b"()<>@,;:\\\"/[]?=") {
        class |= MIME_TOKEN;
    }
    // RFC 3986 sections 2.2 and 2.3.
    let unreserved = alnum || one_of(c, b"-._~");
    let sub_delim = one_of(c, b"!$&'()*+,;=");
    if unreserved {
        class |= UNRESERVED;
    }
    if sub_delim {
        class |= SUB_DELIM;
    }
    let pchar = unreserved || sub_delim || c == b':' || c == b'@';
    if pchar {
        class |= PCHAR_NO_PCT;
    }
    // pchar / "/" / "?", plus the "%" that introduces a pct-encoded triple.
    if pchar || c == b'/' || c == b'?' || c == b'%' {
        class |= URI_TARGET;
    }
    if c.is_ascii_hexdigit() {
        class |= HEXDIG;
    }
    // RFC 9651 section 3.1.2: key = ( lcalpha / "*" ) *( lcalpha / DIGIT / "_" / "-" / "." / "*" ).
    let lcalpha = c.is_ascii_lowercase();
    if lcalpha || c == b'*' {
        class |= SF_KEY_START;
    }
    if lcalpha || digit || one_of(c, b"_-.*") {
        class |= SF_KEY;
    }
    // RFC 9651 section 3.3.4: the characters after the first in a token.
    if alnum || one_of(c, b"!#$%&'*+-.^_`|~") || c == b':' || c == b'/' {
        class |= SF_TOKEN;
    }
    // RFC 9651 section 3.3.3: unescaped = %x20-21 / %x23-5B / %x5D-7E.
    if c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x5b) || (c >= 0x5d && c <= 0x7e) {
        class |= SF_STRING;
    }
    // RFC 9110 section 11.2 (the trailing "=" is handled apart).
    if alnum || one_of(c, b"-._~+/") {
        class |= TOKEN68;
    }
    // RFC 4648 section 4 (the "=" padding is handled apart).
    if alnum || c == b'+' || c == b'/' {
        class |= BASE64;
    }
    // RFC 5322 section 3.2.3.
    if alnum || one_of(c, b"!#$%&'*+-/=?^_`{|}~") {
        class |= ATEXT;
    }
    // RFC 2046 section 5.1.1: bcharsnospace.
    if alnum || one_of(c, b"'()+_,-./:=?") {
        class |= BCHAR_NOSPACE;
    }
    // draft-ietf-httpbis-rfc6265bis section 4.1.1.
    if c == 0x21
        || (c >= 0x23 && c <= 0x2b)
        || (c >= 0x2d && c <= 0x3a)
        || (c >= 0x3c && c <= 0x5b)
        || (c >= 0x5d && c <= 0x7e)
    {
        class |= COOKIE_OCTET;
    }
    if (c >= 0x20 && c <= 0x3a) || (c >= 0x3c && c <= 0x7e) {
        class |= AV_OCTET;
    }
    if (vchar || obs || sp_ht) && !one_of(c, b",\"()") {
        class |= LIST_PLAIN;
    }
    class
}

const fn build() -> [u32; 256] {
    let mut table = [0u32; 256];
    let mut b: u8 = 0;
    loop {
        // `b` is a u8, so the index is always below 256.
        #[allow(clippy::indexing_slicing)]
        {
            table[b as usize] = classify(b);
        }
        if b == u8::MAX {
            break;
        }
        b += 1;
    }
    table
}

static CLASSES: [u32; 256] = build();

#[inline]
const fn has(b: u8, bit: u32) -> bool {
    // A u8 index into a 256-entry table cannot be out of bounds.
    #[allow(clippy::indexing_slicing)]
    let entry = CLASSES[b as usize];
    entry & bit != 0
}

/// A set of byte classes, for [`all`] and the scanning functions.
///
/// The named constants are the classes of [`dev/PLAN.md` section 7.1](https://github.com/jamesgober/parse-net/blob/main/dev/PLAN.md#71-the-byte-class-table).
///
/// # Examples
///
/// ```
/// use parse_net::class::Class;
///
/// let host_chars = Class::UNRESERVED.union(Class::SUB_DELIM);
/// assert!(host_chars.contains(b'!') && !host_chars.contains(b'/'));
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Class(u32);

impl Class {
    /// `tchar` (RFC 9110 section 5.6.2).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"no-cache", Class::TCHAR) && !Class::TCHAR.contains(b'('));
    /// ```
    pub const TCHAR: Self = Self(TCHAR);
    /// `VCHAR`: 0x21 to 0x7E (RFC 5234).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"a=b;c", Class::VCHAR) && !Class::VCHAR.contains(b' '));
    /// ```
    pub const VCHAR: Self = Self(VCHAR);
    /// `obs-text`: 0x80 to 0xFF (RFC 9110 section 5.5).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"\xc3\xa9", Class::OBS_TEXT) && !Class::OBS_TEXT.contains(b'a'));
    /// ```
    pub const OBS_TEXT: Self = Self(OBS_TEXT);
    /// `field-vchar / SP / HTAB` (RFC 9110 section 5.5).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"text/html; q=0.9\t", Class::FIELD_TEXT));
    /// assert!(!Class::FIELD_TEXT.contains(b'\r'));
    /// ```
    pub const FIELD_TEXT: Self = Self(FIELD_TEXT);
    /// `qdtext` (RFC 9110 section 5.6.4).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"a b,c", Class::QDTEXT));
    /// assert!(!Class::QDTEXT.contains(b'"') && !Class::QDTEXT.contains(b'\\'));
    /// ```
    pub const QDTEXT: Self = Self(QDTEXT);
    /// `ctext` (RFC 9110 section 5.6.5).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"compatible; MSIE", Class::CTEXT) && !Class::CTEXT.contains(b'('));
    /// ```
    pub const CTEXT: Self = Self(CTEXT);
    /// The byte after `\` in a `quoted-pair`: `HTAB / SP / VCHAR / obs-text`.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::Class;
    ///
    /// assert!(Class::QUOTED_PAIR.contains(b'"') && Class::QUOTED_PAIR.contains(b'\t'));
    /// assert!(!Class::QUOTED_PAIR.contains(0x7f));
    /// ```
    pub const QUOTED_PAIR: Self = Self(QUOTED_PAIR);
    /// `etagc` (RFC 9110 section 8.8.3).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"W/xyz", Class::ETAGC) && !Class::ETAGC.contains(b'"'));
    /// ```
    pub const ETAGC: Self = Self(ETAGC);
    /// `attr-char` (RFC 8187 section 3.2.1).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"utf-8", Class::ATTR_CHAR));
    /// assert!(!Class::ATTR_CHAR.contains(b'%') && !Class::ATTR_CHAR.contains(b'\''));
    /// ```
    pub const ATTR_CHAR: Self = Self(ATTR_CHAR);
    /// The MIME `token` characters (RFC 2045 section 5.1); unlike `tchar`,
    /// they include `{` and `}`.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"{x}", Class::MIME_TOKEN) && !all(b"{x}", Class::TCHAR));
    /// assert!(!Class::MIME_TOKEN.contains(b'/'));
    /// ```
    pub const MIME_TOKEN: Self = Self(MIME_TOKEN);
    /// `unreserved` (RFC 3986 section 2.3).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"a-b_c.d~", Class::UNRESERVED) && !Class::UNRESERVED.contains(b'!'));
    /// ```
    pub const UNRESERVED: Self = Self(UNRESERVED);
    /// `sub-delims` (RFC 3986 section 2.2).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"!$&'()*+,;=", Class::SUB_DELIM) && !Class::SUB_DELIM.contains(b'/'));
    /// ```
    pub const SUB_DELIM: Self = Self(SUB_DELIM);
    /// `unreserved / sub-delims / ":" / "@"`: `pchar` without `pct-encoded`.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"user:pass@host", Class::PCHAR_NO_PCT));
    /// assert!(!Class::PCHAR_NO_PCT.contains(b'/') && !Class::PCHAR_NO_PCT.contains(b'%'));
    /// ```
    pub const PCHAR_NO_PCT: Self = Self(PCHAR_NO_PCT);
    /// `pchar / "/" / "?"` plus `%`: the characters of a request target.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"/a%20b?q=1", Class::URI_TARGET) && !Class::URI_TARGET.contains(b'#'));
    /// ```
    pub const URI_TARGET: Self = Self(URI_TARGET);
    /// `HEXDIG`, either case.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"0aF9", Class::HEXDIG) && !Class::HEXDIG.contains(b'g'));
    /// ```
    pub const HEXDIG: Self = Self(HEXDIG);
    /// The first byte of a Structured Field key: `lcalpha / "*"`.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::Class;
    ///
    /// assert!(Class::SF_KEY_START.contains(b'*') && Class::SF_KEY_START.contains(b'a'));
    /// assert!(!Class::SF_KEY_START.contains(b'_') && !Class::SF_KEY_START.contains(b'0'));
    /// ```
    pub const SF_KEY_START: Self = Self(SF_KEY_START);
    /// A Structured Field key byte: `lcalpha / DIGIT / "_" / "-" / "." / "*"`.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"max-age_2.*", Class::SF_KEY) && !Class::SF_KEY.contains(b'A'));
    /// ```
    pub const SF_KEY: Self = Self(SF_KEY);
    /// A Structured Field token byte after the first: `tchar / ":" / "/"`.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"text/html:x", Class::SF_TOKEN) && !Class::SF_TOKEN.contains(b'"'));
    /// ```
    pub const SF_TOKEN: Self = Self(SF_TOKEN);
    /// An unescaped Structured Field string byte: `%x20-21 / %x23-5B / %x5D-7E`.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"hello world!", Class::SF_STRING));
    /// assert!(!Class::SF_STRING.contains(b'\\') && !Class::SF_STRING.contains(b'"'));
    /// ```
    pub const SF_STRING: Self = Self(SF_STRING);
    /// A `token68` byte (RFC 9110 section 11.2), without the `=` padding.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"abc-._~+/", Class::TOKEN68) && !Class::TOKEN68.contains(b'='));
    /// ```
    pub const TOKEN68: Self = Self(TOKEN68);
    /// The base64 alphabet (RFC 4648 section 4), without the `=` padding.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"aGk+/w", Class::BASE64));
    /// assert!(!Class::BASE64.contains(b'=') && !Class::BASE64.contains(b'-'));
    /// ```
    pub const BASE64: Self = Self(BASE64);
    /// `atext` (RFC 5322 section 3.2.3).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"user+tag", Class::ATEXT) && !Class::ATEXT.contains(b'@'));
    /// ```
    pub const ATEXT: Self = Self(ATEXT);
    /// `bcharsnospace` (RFC 2046 section 5.1.1).
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"----=_Part_0", Class::BCHAR_NOSPACE) && !Class::BCHAR_NOSPACE.contains(b' '));
    /// ```
    pub const BCHAR_NOSPACE: Self = Self(BCHAR_NOSPACE);
    /// `cookie-octet` (rfc6265bis section 4.1.1), for cookie-net.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"abc123!", Class::COOKIE_OCTET));
    /// assert!(!Class::COOKIE_OCTET.contains(b';') && !Class::COOKIE_OCTET.contains(b','));
    /// ```
    pub const COOKIE_OCTET: Self = Self(COOKIE_OCTET);
    /// Cookie attribute value bytes (rfc6265bis section 4.1.1), for cookie-net.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::{all, Class};
    ///
    /// assert!(all(b"Mon, 01 Jan", Class::AV_OCTET) && !Class::AV_OCTET.contains(b';'));
    /// ```
    pub const AV_OCTET: Self = Self(AV_OCTET);

    /// Whether `b` is in this class.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::Class;
    ///
    /// assert!(Class::HEXDIG.contains(b'a') && !Class::HEXDIG.contains(b'g'));
    /// ```
    #[inline]
    #[must_use]
    pub const fn contains(self, b: u8) -> bool {
        has(b, self.0)
    }

    /// A class holding the bytes of both.
    ///
    /// # Examples
    ///
    /// ```
    /// use parse_net::class::Class;
    ///
    /// let c = Class::HEXDIG.union(Class::SUB_DELIM);
    /// assert!(c.contains(b'f') && c.contains(b'+') && !c.contains(b'g'));
    /// ```
    #[must_use]
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }
}

/// Whether every byte of `s` is in `class` (true for an empty `s`). Stops at
/// the first byte outside.
///
/// # Examples
///
/// ```
/// use parse_net::class::{all, Class};
///
/// assert!(all(b"gzip", Class::TCHAR));
/// assert!(!all(b"gzip, br", Class::TCHAR));
/// assert!(all(b"", Class::TCHAR));
/// ```
#[inline]
#[must_use]
pub const fn all(s: &[u8], class: Class) -> bool {
    // An indexed loop: the compiler drops the bounds check for `i < len`,
    // which the slice-pattern form does not get as reliably.
    let mut i = 0;
    while i < s.len() {
        // `i < s.len()` was just checked.
        #[allow(clippy::indexing_slicing)]
        let b = s[i];
        if !has(b, class.0) {
            return false;
        }
        i += 1;
    }
    true
}

macro_rules! predicate {
    ($(#[$doc:meta])* $name:ident, $bit:ident, $example_yes:literal, $example_no:literal) => {
        $(#[$doc])*
        ///
        /// # Examples
        ///
        /// ```
        #[doc = concat!("assert!(parse_net::class::", stringify!($name), "(", stringify!($example_yes), "));")]
        #[doc = concat!("assert!(!parse_net::class::", stringify!($name), "(", stringify!($example_no), "));")]
        /// ```
        #[inline]
        #[must_use]
        pub const fn $name(b: u8) -> bool {
            has(b, $bit)
        }
    };
}

predicate!(
    /// Whether `b` is a `tchar`, a byte allowed in a token (RFC 9110 section 5.6.2).
    is_tchar, TCHAR, b'~', b'('
);
predicate!(
    /// Whether `b` is a visible ASCII character, 0x21 to 0x7E.
    is_vchar, VCHAR, b'!', b' '
);
predicate!(
    /// Whether `b` is `obs-text`, 0x80 to 0xFF.
    is_obs_text, OBS_TEXT, 0x80, 0x7f
);
predicate!(
    /// Whether `b` may appear in a field value: `field-vchar / SP / HTAB`
    /// (RFC 9110 section 5.5).
    is_field_text, FIELD_TEXT, b'\t', b'\r'
);
predicate!(
    /// Whether `b` may appear unescaped in a quoted string (RFC 9110 section 5.6.4).
    is_qdtext, QDTEXT, b' ', b'"'
);
predicate!(
    /// Whether `b` may appear unescaped in a comment (RFC 9110 section 5.6.5).
    is_ctext, CTEXT, b'a', b'('
);
predicate!(
    /// Whether `b` may follow `\` in a `quoted-pair`: `HTAB / SP / VCHAR / obs-text`.
    is_quoted_pair_char, QUOTED_PAIR, b'"', 0x7f
);
predicate!(
    /// Whether `b` is an `etagc` (RFC 9110 section 8.8.3).
    is_etagc, ETAGC, b'!', b'"'
);
predicate!(
    /// Whether `b` is an `attr-char` (RFC 8187 section 3.2.1).
    is_attr_char, ATTR_CHAR, b'|', b'%'
);
predicate!(
    /// Whether `b` may appear in a MIME `token` (RFC 2045 section 5.1).
    is_mime_token_char, MIME_TOKEN, b'{', b'/'
);
predicate!(
    /// Whether `b` is `unreserved` (RFC 3986 section 2.3).
    is_unreserved, UNRESERVED, b'~', b'!'
);
predicate!(
    /// Whether `b` is a `sub-delims` byte (RFC 3986 section 2.2).
    is_sub_delim, SUB_DELIM, b'!', b'/'
);
predicate!(
    /// Whether `b` is a `pchar` other than the `%` of `pct-encoded`
    /// (RFC 3986 section 3.3).
    is_pchar_no_pct, PCHAR_NO_PCT, b'@', b'/'
);
predicate!(
    /// Whether `b` may appear in a request target: `pchar / "/" / "?"` or the
    /// `%` of a `pct-encoded` triple (whose digits are checked apart).
    is_uri_target, URI_TARGET, b'?', b'#'
);
predicate!(
    /// Whether `b` is a hex digit, either case.
    is_hexdig, HEXDIG, b'F', b'g'
);
predicate!(
    /// Whether `b` may start a Structured Field key: `lcalpha / "*"`.
    is_sf_key_start, SF_KEY_START, b'*', b'A'
);
predicate!(
    /// Whether `b` may appear in a Structured Field key.
    is_sf_key_char, SF_KEY, b'_', b'A'
);
predicate!(
    /// Whether `b` may appear in a Structured Field token after its first byte.
    is_sf_token_char, SF_TOKEN, b':', b'"'
);
predicate!(
    /// Whether `b` may appear unescaped in a Structured Field string.
    is_sf_string_char, SF_STRING, b' ', b'\\'
);
predicate!(
    /// Whether `b` is a `token68` byte other than the `=` padding.
    is_token68_char, TOKEN68, b'~', b'='
);
predicate!(
    /// Whether `b` is in the base64 alphabet (RFC 4648 section 4), padding aside.
    is_base64_char, BASE64, b'+', b'-'
);
predicate!(
    /// Whether `b` is an `atext` byte (RFC 5322 section 3.2.3).
    is_atext, ATEXT, b'{', b'@'
);
predicate!(
    /// Whether `b` is a `bcharsnospace` byte (RFC 2046 section 5.1.1).
    is_bchar_nospace, BCHAR_NOSPACE, b'?', b' '
);
predicate!(
    /// Whether `b` is a `cookie-octet` (rfc6265bis section 4.1.1).
    is_cookie_octet, COOKIE_OCTET, b'!', b';'
);
predicate!(
    /// Whether `b` may appear in a cookie attribute value (rfc6265bis section 4.1.1).
    is_av_octet, AV_OCTET, b' ', b';'
);

/// Whether `b` is field text other than a list delimiter (`,` `"` `(` `)`).
#[inline]
pub(crate) const fn is_list_plain(b: u8) -> bool {
    has(b, LIST_PLAIN)
}

/// The value of one hex digit (either case), or `None`.
///
/// # Examples
///
/// ```
/// use parse_net::class::hex_value;
///
/// assert_eq!(hex_value(b'A'), Some(10));
/// assert_eq!(hex_value(b'g'), None);
/// ```
#[inline]
#[must_use]
pub const fn hex_value(b: u8) -> Option<u8> {
    match b {
        b'0'..=b'9' => Some(b - b'0'),
        b'a'..=b'f' => Some(b - b'a' + 10),
        b'A'..=b'F' => Some(b - b'A' + 10),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Slow references written straight from the ABNF, one per class.
    fn r_alnum(b: u8) -> bool {
        b.is_ascii_alphanumeric()
    }
    fn r_vchar(b: u8) -> bool {
        (0x21..=0x7e).contains(&b)
    }
    fn r_obs(b: u8) -> bool {
        b >= 0x80
    }
    fn r_tchar(b: u8) -> bool {
        r_alnum(b) || b"!#$%&'*+-.^_`|~".contains(&b)
    }

    fn check(name: &str, fast: fn(u8) -> bool, slow: impl Fn(u8) -> bool) {
        for b in 0u8..=255 {
            assert_eq!(fast(b), slow(b), "{name}: byte {b:#04x}");
        }
    }

    #[test]
    fn test_tchar_table_exhaustive() {
        check("tchar", is_tchar, r_tchar);
        check("vchar", is_vchar, r_vchar);
        check("obs-text", is_obs_text, r_obs);
        check("field-text", is_field_text, |b| {
            r_vchar(b) || r_obs(b) || b == b' ' || b == b'\t'
        });
    }

    #[test]
    fn test_quoting_classes_exhaustive() {
        check("qdtext", is_qdtext, |b| {
            b == b'\t'
                || b == b' '
                || b == 0x21
                || (0x23..=0x5b).contains(&b)
                || (0x5d..=0x7e).contains(&b)
                || r_obs(b)
        });
        check("ctext", is_ctext, |b| {
            b == b'\t'
                || b == b' '
                || (0x21..=0x27).contains(&b)
                || (0x2a..=0x5b).contains(&b)
                || (0x5d..=0x7e).contains(&b)
                || r_obs(b)
        });
        check("quoted-pair", is_quoted_pair_char, |b| {
            b == b'\t' || b == b' ' || r_vchar(b) || r_obs(b)
        });
        check("etagc", is_etagc, |b| {
            b == 0x21 || (0x23..=0x7e).contains(&b) || r_obs(b)
        });
    }

    #[test]
    fn test_mime_and_attr_classes_exhaustive() {
        check("attr-char", is_attr_char, |b| {
            r_alnum(b) || b"!#$&+-.^_`|~".contains(&b)
        });
        check("mime token", is_mime_token_char, |b| {
            (0x21..=0x7e).contains(&b) && !b"()<>@,;:\\\"/[]?=".contains(&b)
        });
        check("bcharsnospace", is_bchar_nospace, |b| {
            r_alnum(b) || b"'()+_,-./:=?".contains(&b)
        });
        check("atext", is_atext, |b| {
            r_alnum(b) || b"!#$%&'*+-/=?^_`{|}~".contains(&b)
        });
    }

    #[test]
    fn test_uri_classes_exhaustive() {
        let unreserved = |b: u8| r_alnum(b) || b"-._~".contains(&b);
        let sub = |b: u8| b"!$&'()*+,;=".contains(&b);
        check("unreserved", is_unreserved, unreserved);
        check("sub-delims", is_sub_delim, sub);
        check("pchar", is_pchar_no_pct, |b| {
            unreserved(b) || sub(b) || b == b':' || b == b'@'
        });
        check("target", is_uri_target, |b| {
            unreserved(b) || sub(b) || b":@/?%".contains(&b)
        });
        check("hexdig", is_hexdig, |b| b.is_ascii_hexdigit());
    }

    #[test]
    fn test_structured_field_classes_exhaustive() {
        check("sf key start", is_sf_key_start, |b| {
            b.is_ascii_lowercase() || b == b'*'
        });
        check("sf key", is_sf_key_char, |b| {
            b.is_ascii_lowercase() || b.is_ascii_digit() || b"_-.*".contains(&b)
        });
        check("sf token", is_sf_token_char, |b| {
            r_tchar(b) || b == b':' || b == b'/'
        });
        check("sf string", is_sf_string_char, |b| {
            b == 0x20 || b == 0x21 || (0x23..=0x5b).contains(&b) || (0x5d..=0x7e).contains(&b)
        });
    }

    #[test]
    fn test_base64_and_cookie_classes_exhaustive() {
        check("token68", is_token68_char, |b| {
            r_alnum(b) || b"-._~+/".contains(&b)
        });
        check("base64", is_base64_char, |b| {
            r_alnum(b) || b == b'+' || b == b'/'
        });
        check("cookie-octet", is_cookie_octet, |b| {
            b == 0x21
                || (0x23..=0x2b).contains(&b)
                || (0x2d..=0x3a).contains(&b)
                || (0x3c..=0x5b).contains(&b)
                || (0x5d..=0x7e).contains(&b)
        });
        check("av-octet", is_av_octet, |b| {
            (0x20..=0x3a).contains(&b) || (0x3c..=0x7e).contains(&b)
        });
    }

    #[test]
    fn test_list_plain_is_field_text_without_delimiters() {
        for b in 0u8..=255 {
            let expected = is_field_text(b) && !b",\"()".contains(&b);
            assert_eq!(is_list_plain(b), expected, "byte {b:#04x}");
        }
    }

    #[test]
    fn test_del_is_in_no_class() {
        assert_eq!(CLASSES[0x7f], 0);
    }

    #[test]
    fn test_unicode_bytes_never_in_ascii_classes() {
        let ascii_only = TCHAR
            | VCHAR
            | ATTR_CHAR
            | MIME_TOKEN
            | UNRESERVED
            | SUB_DELIM
            | PCHAR_NO_PCT
            | URI_TARGET
            | HEXDIG
            | SF_KEY_START
            | SF_KEY
            | SF_TOKEN
            | SF_STRING
            | TOKEN68
            | BASE64
            | ATEXT
            | BCHAR_NOSPACE
            | COOKIE_OCTET
            | AV_OCTET;
        for b in 0x80u8..=0xff {
            assert_eq!(CLASSES[b as usize] & ascii_only, 0, "byte {b:#04x}");
        }
    }

    #[test]
    fn test_hex_value_all_bytes() {
        for b in 0u8..=255 {
            let expected = (b as char).to_digit(16).map(|d| d as u8);
            assert_eq!(hex_value(b), expected, "byte {b:#04x}");
        }
    }

    #[test]
    fn test_class_all_and_union() {
        assert!(all(b"", Class::TCHAR));
        assert!(all(b"abc", Class::TCHAR));
        assert!(!all(b"a c", Class::TCHAR));
        let c = Class::TCHAR.union(Class::SF_KEY);
        assert!(c.contains(b'a') && c.contains(b'*') && !c.contains(b' '));
        for b in 0u8..=255 {
            assert_eq!(Class::QDTEXT.contains(b), is_qdtext(b));
        }
    }
}