Skip to main content

parse_net/
text.rs

1//! The lexical forms of RFC 9110 section 5.6: tokens, optional whitespace,
2//! quoted strings, words (`token / quoted-string`), and comments.
3//!
4//! # Examples
5//!
6//! ```
7//! use parse_net::text;
8//!
9//! assert!(text::is_token(b"no-cache") && !text::is_token(b"no cache"));
10//! assert_eq!(text::trim_ows(b" \t gzip \t"), b"gzip");
11//! let (q, used) = text::parse_quoted(b"\"a \\\"b\\\" c\"; x")?;
12//! assert_eq!(used, 11);
13//! assert!(q.eq_bytes(b"a \"b\" c"));
14//! # Ok::<(), parse_net::ParseError>(())
15//! ```
16
17use core::fmt;
18
19use crate::{
20    class,
21    error::{ErrorKind, Result, err},
22    limits::Limits,
23    scan::control_kind,
24};
25
26/// Whether `s` is a token: non-empty, every byte `tchar` (RFC 9110 section
27/// 5.6.2).
28///
29/// # Examples
30///
31/// ```
32/// use parse_net::text::is_token;
33///
34/// assert!(is_token(b"GET") && !is_token(b"GE T") && !is_token(b""));
35/// ```
36#[inline]
37#[must_use]
38pub fn is_token(s: &[u8]) -> bool {
39    !s.is_empty() && class::all(s, class::Class::TCHAR)
40}
41
42/// Whether `b` is optional whitespace: SP or HTAB only.
43///
44/// # Examples
45///
46/// ```
47/// use parse_net::text::is_ows;
48///
49/// assert!(is_ows(b' ') && is_ows(b'\t') && !is_ows(0x0b));
50/// ```
51#[inline]
52#[must_use]
53pub const fn is_ows(b: u8) -> bool {
54    b == b' ' || b == b'\t'
55}
56
57/// `s` without leading and trailing SP and HTAB.
58///
59/// `<[u8]>::trim_ascii` is deliberately not used: it also strips vertical tab
60/// and form feed, which are invalid in a field value and must be refused, not
61/// silently removed. NBSP and every other byte stay too.
62///
63/// # Examples
64///
65/// ```
66/// use parse_net::text::trim_ows;
67///
68/// assert_eq!(trim_ows(b" \t a b \t"), b"a b");
69/// assert_eq!(trim_ows(b"\x0ba\x0c"), b"\x0ba\x0c");
70/// assert_eq!(trim_ows(b"   "), b"");
71/// ```
72#[inline]
73#[must_use]
74pub fn trim_ows(mut s: &[u8]) -> &[u8] {
75    while let [first, rest @ ..] = s {
76        if is_ows(*first) {
77            s = rest;
78        } else {
79            break;
80        }
81    }
82    while let [rest @ .., last] = s {
83        if is_ows(*last) {
84            s = rest;
85        } else {
86            break;
87        }
88    }
89    s
90}
91
92/// The number of leading SP and HTAB bytes in `s`.
93///
94/// # Examples
95///
96/// ```
97/// assert_eq!(parse_net::text::skip_ows(b" \t x"), 3);
98/// ```
99#[inline]
100#[must_use]
101pub fn skip_ows(s: &[u8]) -> usize {
102    s.iter().take_while(|&&b| is_ows(b)).count()
103}
104
105/// The longest token prefix of `s`, and its length.
106///
107/// The rest of `s` is the caller's to check.
108///
109/// # Errors
110///
111/// [`ErrorKind::Empty`] when `s` does not start with a `tchar`.
112///
113/// # Examples
114///
115/// ```
116/// use parse_net::text::parse_token;
117///
118/// assert_eq!(parse_token(b"charset=utf-8")?, (&b"charset"[..], 7));
119/// assert!(parse_token(b"=x").is_err());
120/// # Ok::<(), parse_net::ParseError>(())
121/// ```
122pub fn parse_token(s: &[u8]) -> Result<(&[u8], usize)> {
123    let n = s.iter().take_while(|&&b| class::is_tchar(b)).count();
124    if n == 0 {
125        return err(ErrorKind::Empty, 0);
126    }
127    Ok((s.get(..n).unwrap_or(&[]), n))
128}
129
130/// The error for a byte refused inside a quoted string or comment.
131#[inline]
132const fn refused(b: u8) -> ErrorKind {
133    if b < 0x20 || b == 0x7f {
134        control_kind(b)
135    } else {
136        ErrorKind::InvalidByte
137    }
138}
139
140/// Parses the quoted string at the start of `s` (RFC 9110 section 5.6.4).
141///
142/// Returns the view and the length used, both quotes included.
143///
144/// # Errors
145///
146/// [`ErrorKind::InvalidByte`] at offset 0 when `s` does not start with `"`;
147/// [`ErrorKind::BareCr`], [`ErrorKind::LineFeed`], [`ErrorKind::Nul`], or
148/// [`ErrorKind::Control`] for a control byte inside; [`ErrorKind::InvalidQuotedPair`]
149/// when the byte after `\` is not `HTAB / SP / VCHAR / obs-text`;
150/// [`ErrorKind::UnterminatedQuote`] at the end of input.
151///
152/// # Examples
153///
154/// ```
155/// use parse_net::text::parse_quoted;
156///
157/// let (q, used) = parse_quoted(b"\"say \\\"hi\\\"\" rest")?;
158/// assert_eq!(used, 12);
159/// assert_eq!(q.raw(), b"say \\\"hi\\\"");
160/// # Ok::<(), parse_net::ParseError>(())
161/// ```
162pub fn parse_quoted(s: &[u8]) -> Result<(Quoted<'_>, usize)> {
163    let Some((&b'"', body)) = s.split_first() else {
164        return err(ErrorKind::InvalidByte, 0);
165    };
166    let mut has_escapes = false;
167    let mut i = 0usize;
168    let mut bytes = body.iter();
169    while let Some(&b) = bytes.next() {
170        match b {
171            b'"' => {
172                let raw = body.get(..i).unwrap_or(&[]);
173                return Ok((Quoted { raw, has_escapes }, i + 2));
174            }
175            b'\\' => {
176                has_escapes = true;
177                match bytes.next() {
178                    None => return err(ErrorKind::UnterminatedQuote, s.len()),
179                    Some(&e) if class::is_quoted_pair_char(e) => i += 2,
180                    Some(_) => return err(ErrorKind::InvalidQuotedPair, i + 2),
181                }
182            }
183            _ if class::is_qdtext(b) => i += 1,
184            _ => return err(refused(b), i + 1),
185        }
186    }
187    err(ErrorKind::UnterminatedQuote, s.len())
188}
189
190/// Parses a word at the start of `s`: a quoted string when `s` starts with
191/// `"`, a token otherwise.
192///
193/// # Errors
194///
195/// As [`parse_token`] and [`parse_quoted`].
196///
197/// # Examples
198///
199/// ```
200/// use parse_net::text::parse_word;
201///
202/// let (a, _) = parse_word(b"utf-8")?;
203/// let (b, _) = parse_word(b"\"UTF-8\"")?;
204/// assert!(a.eq_ignore_ascii_case(b"UTF-8") && b.eq_ignore_ascii_case(b"utf-8"));
205/// # Ok::<(), parse_net::ParseError>(())
206/// ```
207pub fn parse_word(s: &[u8]) -> Result<(Word<'_>, usize)> {
208    if s.first() == Some(&b'"') {
209        let (q, n) = parse_quoted(s)?;
210        Ok((Word::Quoted(q), n))
211    } else {
212        let (t, n) = parse_token(s)?;
213        Ok((Word::Token(t), n))
214    }
215}
216
217/// Parses the comment at the start of `s` (RFC 9110 section 5.6.5), nested
218/// comments included, counted and never recursed.
219///
220/// Returns the view and the length used, both outer parentheses included.
221///
222/// # Errors
223///
224/// [`ErrorKind::InvalidByte`] at offset 0 when `s` does not start with `(`;
225/// a control kind for a control byte; [`ErrorKind::InvalidQuotedPair`];
226/// [`ErrorKind::UnbalancedComment`] when the comment never closes;
227/// [`ErrorKind::CommentTooDeep`] at the `(` that exceeded
228/// [`Limits::max_comment_depth`].
229///
230/// # Examples
231///
232/// ```
233/// use parse_net::{text::parse_comment, Limits};
234///
235/// let (c, used) = parse_comment(b"(compatible; (nested)) rest", &Limits::DEFAULT)?;
236/// assert_eq!((c.raw(), c.max_depth(), used), (&b"compatible; (nested)"[..], 2, 22));
237/// # Ok::<(), parse_net::ParseError>(())
238/// ```
239pub fn parse_comment<'a>(s: &'a [u8], limits: &Limits) -> Result<(Comment<'a>, usize)> {
240    if s.first() != Some(&b'(') {
241        return err(ErrorKind::InvalidByte, 0);
242    }
243    let end = comment_end(s, 0, limits)?;
244    let (end, max_depth) = end;
245    let raw = s.get(1..end - 1).unwrap_or(&[]);
246    Ok((Comment { raw, max_depth }, end))
247}
248
249/// Scans the comment that starts at `s[start]` (an opening `(`). Returns the
250/// offset just past its closing `)` and the deepest nesting seen.
251pub(crate) fn comment_end(s: &[u8], start: usize, limits: &Limits) -> Result<(usize, u8)> {
252    let limit = u16::from(limits.comment_depth);
253    let mut depth: u16 = 0;
254    let mut max: u16 = 0;
255    let mut i = start;
256    let mut bytes = s.get(start..).unwrap_or(&[]).iter();
257    while let Some(&b) = bytes.next() {
258        match b {
259            b'(' => {
260                depth += 1;
261                if depth > limit {
262                    return err(ErrorKind::CommentTooDeep, i);
263                }
264                max = max.max(depth);
265                i += 1;
266            }
267            b')' => {
268                depth -= 1;
269                i += 1;
270                if depth == 0 {
271                    // The limit is a u8, so `max` fits.
272                    return Ok((i, u8::try_from(max).unwrap_or(u8::MAX)));
273                }
274            }
275            b'\\' => match bytes.next() {
276                None => return err(ErrorKind::UnbalancedComment, s.len()),
277                Some(&e) if class::is_quoted_pair_char(e) => i += 2,
278                Some(_) => return err(ErrorKind::InvalidQuotedPair, i + 1),
279            },
280            _ if class::is_ctext(b) => i += 1,
281            _ => return err(refused(b), i),
282        }
283    }
284    err(ErrorKind::UnbalancedComment, s.len())
285}
286
287/// A validated quoted string: the bytes between the quotes, and whether any
288/// quoted pair occurs.
289///
290/// `Debug` prints the raw bytes as an escaped byte string.
291///
292/// # Examples
293///
294/// ```
295/// use parse_net::text;
296///
297/// let (q, _) = text::parse_quoted(b"\"say \\\"hi\\\"\"")?;
298/// assert!(q.has_escapes() && q.as_plain().is_none());
299/// assert_eq!(q.unescaped_len(), 8);
300/// let mut buf = [0u8; 8];
301/// assert_eq!(q.unescape_into(&mut buf)?, b"say \"hi\"");
302/// assert!(q.eq_bytes(b"say \"hi\""));
303/// let mut short = [0u8; 7];
304/// assert_eq!(q.unescape_into(&mut short).unwrap_err().kind(), parse_net::ErrorKind::BufferTooSmall);
305/// # Ok::<(), parse_net::ParseError>(())
306/// ```
307#[derive(Clone, Copy, PartialEq, Eq, Hash)]
308pub struct Quoted<'a> {
309    raw: &'a [u8],
310    has_escapes: bool,
311}
312
313impl<'a> Quoted<'a> {
314    /// The content between the quotes exactly as sent, quoted pairs included.
315    #[must_use]
316    pub const fn raw(&self) -> &'a [u8] {
317        self.raw
318    }
319
320    /// Whether the content contains a quoted pair (`\x`).
321    #[must_use]
322    pub const fn has_escapes(&self) -> bool {
323        self.has_escapes
324    }
325
326    /// The content when it contains no quoted pair: the value, with no copy.
327    #[must_use]
328    pub const fn as_plain(&self) -> Option<&'a [u8]> {
329        if self.has_escapes {
330            None
331        } else {
332            Some(self.raw)
333        }
334    }
335
336    /// The length of the value with quoted pairs replaced.
337    #[must_use]
338    pub fn unescaped_len(&self) -> usize {
339        if self.has_escapes {
340            self.bytes().count()
341        } else {
342            self.raw.len()
343        }
344    }
345
346    /// The bytes of the value with quoted pairs replaced.
347    #[must_use]
348    pub fn bytes(&self) -> Unescape<'a> {
349        Unescape {
350            rest: self.raw.iter(),
351        }
352    }
353
354    /// Writes the value into `out`, every quoted pair replaced by the byte
355    /// after the backslash, and returns the written part.
356    ///
357    /// # Errors
358    ///
359    /// [`ErrorKind::BufferTooSmall`] when `out` is shorter than
360    /// [`unescaped_len`](Self::unescaped_len); nothing is written.
361    pub fn unescape_into<'b>(&self, out: &'b mut [u8]) -> Result<&'b [u8]> {
362        let len = self.unescaped_len();
363        let Some(dst) = out.get_mut(..len) else {
364            return err(ErrorKind::BufferTooSmall, 0);
365        };
366        for (d, b) in dst.iter_mut().zip(self.bytes()) {
367            *d = b;
368        }
369        Ok(dst)
370    }
371
372    /// Whether the value equals `other`, compared without a buffer.
373    #[must_use]
374    pub fn eq_bytes(&self, other: &[u8]) -> bool {
375        match self.as_plain() {
376            Some(p) => p == other,
377            None => self.bytes().eq(other.iter().copied()),
378        }
379    }
380
381    /// Whether the value equals `other` ignoring ASCII case, compared without
382    /// a buffer.
383    #[must_use]
384    pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
385        match self.as_plain() {
386            Some(p) => p.eq_ignore_ascii_case(other),
387            None => {
388                let mut theirs = other.iter();
389                self.bytes()
390                    .all(|b| theirs.next().is_some_and(|o| o.eq_ignore_ascii_case(&b)))
391                    && theirs.next().is_none()
392            }
393        }
394    }
395}
396
397impl fmt::Debug for Quoted<'_> {
398    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
399        f.write_str("Quoted(\"")?;
400        for &b in self.raw {
401            for c in core::ascii::escape_default(b) {
402                f.write_str(core::str::from_utf8(&[c]).unwrap_or("?"))?;
403            }
404        }
405        f.write_str("\")")
406    }
407}
408
409/// The unescaped bytes of a [`Quoted`] value.
410#[derive(Clone, Debug)]
411pub struct Unescape<'a> {
412    rest: core::slice::Iter<'a, u8>,
413}
414
415impl Iterator for Unescape<'_> {
416    type Item = u8;
417
418    #[inline]
419    fn next(&mut self) -> Option<u8> {
420        match self.rest.next() {
421            Some(&b'\\') => self.rest.next().copied(),
422            other => other.copied(),
423        }
424    }
425
426    fn size_hint(&self) -> (usize, Option<usize>) {
427        let n = self.rest.len();
428        (n.div_ceil(2), Some(n))
429    }
430}
431
432/// `token / quoted-string`: the value form of parameters and directives.
433///
434/// A token and a quoted string with the same content compare equal, as
435/// RFC 9110 section 5.6.6 requires: [`eq_bytes`](Self::eq_bytes) and
436/// [`eq_ignore_ascii_case`](Self::eq_ignore_ascii_case) compare unescaped
437/// content. The derived `PartialEq` compares the forms structurally.
438///
439/// # Examples
440///
441/// ```
442/// use parse_net::text;
443///
444/// let (a, _) = text::parse_word(b"utf-8")?;
445/// let (b, _) = text::parse_word(b"\"UTF-8\"")?;
446/// assert!(a.eq_ignore_ascii_case(b"UTF-8") && b.eq_ignore_ascii_case(b"utf-8"));
447/// assert_eq!(b.as_str(), Some("UTF-8"));
448/// # Ok::<(), parse_net::ParseError>(())
449/// ```
450#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
451pub enum Word<'a> {
452    /// A token.
453    Token(&'a [u8]),
454    /// A quoted string.
455    Quoted(Quoted<'a>),
456}
457
458impl<'a> Word<'a> {
459    /// The token, or the quoted content when it has no quoted pair.
460    #[must_use]
461    pub const fn as_plain(&self) -> Option<&'a [u8]> {
462        match self {
463            Self::Token(t) => Some(t),
464            Self::Quoted(q) => q.as_plain(),
465        }
466    }
467
468    /// [`as_plain`](Self::as_plain) when it is valid UTF-8.
469    #[must_use]
470    pub fn as_str(&self) -> Option<&'a str> {
471        self.as_plain().and_then(|p| core::str::from_utf8(p).ok())
472    }
473
474    /// Whether the unescaped value equals `other`.
475    #[must_use]
476    pub fn eq_bytes(&self, other: &[u8]) -> bool {
477        match self {
478            Self::Token(t) => *t == other,
479            Self::Quoted(q) => q.eq_bytes(other),
480        }
481    }
482
483    /// Whether the unescaped value equals `other` ignoring ASCII case.
484    #[must_use]
485    pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
486        match self {
487            Self::Token(t) => t.eq_ignore_ascii_case(other),
488            Self::Quoted(q) => q.eq_ignore_ascii_case(other),
489        }
490    }
491
492    /// Writes the unescaped value into `out` and returns the written part.
493    ///
494    /// # Errors
495    ///
496    /// [`ErrorKind::BufferTooSmall`]; nothing is written.
497    pub fn unescape_into<'b>(&self, out: &'b mut [u8]) -> Result<&'b [u8]> {
498        match self {
499            Self::Token(t) => {
500                let Some(dst) = out.get_mut(..t.len()) else {
501                    return err(ErrorKind::BufferTooSmall, 0);
502                };
503                dst.copy_from_slice(t);
504                Ok(dst)
505            }
506            Self::Quoted(q) => q.unescape_into(out),
507        }
508    }
509
510    /// The length of the unescaped value.
511    #[must_use]
512    pub fn len(&self) -> usize {
513        match self {
514            Self::Token(t) => t.len(),
515            Self::Quoted(q) => q.unescaped_len(),
516        }
517    }
518
519    /// Whether the unescaped value is empty (only a quoted string can be).
520    #[must_use]
521    pub fn is_empty(&self) -> bool {
522        match self {
523            Self::Token(t) => t.is_empty(),
524            Self::Quoted(q) => q.raw.is_empty(),
525        }
526    }
527}
528
529/// A validated comment: the text without the outer parentheses, and the
530/// deepest nesting reached.
531///
532/// # Examples
533///
534/// ```
535/// use parse_net::{text::parse_comment, Limits};
536///
537/// let (c, _) = parse_comment(b"(a(b))", &Limits::DEFAULT)?;
538/// assert_eq!((c.raw(), c.max_depth()), (&b"a(b)"[..], 2));
539/// # Ok::<(), parse_net::ParseError>(())
540/// ```
541#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
542pub struct Comment<'a> {
543    raw: &'a [u8],
544    max_depth: u8,
545}
546
547impl<'a> Comment<'a> {
548    /// The text between the outer parentheses, as sent.
549    #[must_use]
550    pub const fn raw(&self) -> &'a [u8] {
551        self.raw
552    }
553
554    /// The deepest nesting reached: 1 for `(a)`, 2 for `(a(b))`.
555    #[must_use]
556    pub const fn max_depth(&self) -> u8 {
557        self.max_depth
558    }
559}
560
561#[cfg(test)]
562mod tests {
563    use std::{format, vec, vec::Vec};
564
565    use super::*;
566
567    fn q(s: &[u8]) -> Result<(Quoted<'_>, usize)> {
568        parse_quoted(s)
569    }
570
571    #[test]
572    fn test_quoted_edge_cases() {
573        assert_eq!(q(b"\"\"").unwrap().0.raw(), b"");
574        assert!(q(b"\"\\\"\"").unwrap().0.eq_bytes(b"\""));
575        assert!(q(b"\"\\\\\"").unwrap().0.eq_bytes(b"\\"));
576        assert_eq!(q(b"\"\\").unwrap_err().kind(), ErrorKind::UnterminatedQuote);
577        assert_eq!(q(b"\"a").unwrap_err().kind(), ErrorKind::UnterminatedQuote);
578        let e = q(b"\"a\x00\"").unwrap_err();
579        assert_eq!((e.kind(), e.offset()), (ErrorKind::Nul, 2));
580        let e = q(b"\"\\\x7f\"").unwrap_err();
581        assert_eq!((e.kind(), e.offset()), (ErrorKind::InvalidQuotedPair, 2));
582        assert!(q(b"\"\x80\xff\"").is_ok());
583        assert!(q(b"\"\\\t\"").unwrap().0.eq_bytes(b"\t"));
584        assert_eq!(q(b"x").unwrap_err().kind(), ErrorKind::InvalidByte);
585        assert_eq!(q(b"").unwrap_err().kind(), ErrorKind::InvalidByte);
586        assert_eq!(q(b"\"a\rb\"").unwrap_err().kind(), ErrorKind::BareCr);
587        assert_eq!(q(b"\"a\nb\"").unwrap_err().kind(), ErrorKind::LineFeed);
588    }
589
590    #[test]
591    fn test_quoted_unescape_buffer_one_short_writes_nothing() {
592        let (v, _) = q(b"\"a\\\"b\"").unwrap();
593        let mut buf = [0xaau8; 2];
594        assert_eq!(
595            v.unescape_into(&mut buf).unwrap_err().kind(),
596            ErrorKind::BufferTooSmall
597        );
598        assert_eq!(buf, [0xaa, 0xaa]);
599        let mut buf = [0u8; 3];
600        assert_eq!(v.unescape_into(&mut buf).unwrap(), b"a\"b");
601    }
602
603    #[test]
604    fn test_quoted_many_backslashes_linear() {
605        let mut v = vec![b'"'];
606        v.extend(core::iter::repeat_n(b'\\', 64 * 1024));
607        v.push(b'"');
608        let (quoted, used) = q(&v).unwrap();
609        assert_eq!(used, v.len());
610        assert_eq!(quoted.unescaped_len(), 32 * 1024);
611    }
612
613    #[test]
614    fn test_quoted_case_insensitive_compare() {
615        let (v, _) = q(b"\"A\\Bc\"").unwrap();
616        assert!(v.eq_ignore_ascii_case(b"abc"));
617        assert!(!v.eq_ignore_ascii_case(b"ab"));
618        assert!(!v.eq_ignore_ascii_case(b"abcd"));
619        assert!(!v.eq_bytes(b"abc"));
620        assert_eq!(v.bytes().collect::<Vec<u8>>(), b"ABc");
621    }
622
623    #[test]
624    fn test_word_token_and_quoted_compare_equal() {
625        let (a, _) = parse_word(b"abc").unwrap();
626        let (b, _) = parse_word(b"\"a\\bc\"").unwrap();
627        assert!(a.eq_bytes(b"abc") && b.eq_bytes(b"abc"));
628        assert_ne!(a, b);
629        assert_eq!((a.len(), b.len()), (3, 3));
630        assert_eq!(b.as_str(), None);
631        let (e, _) = parse_word(b"\"\"").unwrap();
632        assert!(e.is_empty() && !a.is_empty());
633        let mut buf = [0u8; 3];
634        assert_eq!(a.unescape_into(&mut buf).unwrap(), b"abc");
635        assert_eq!(
636            a.unescape_into(&mut buf[..2]).unwrap_err().kind(),
637            ErrorKind::BufferTooSmall
638        );
639        assert_eq!(parse_word(b";").unwrap_err().kind(), ErrorKind::Empty);
640    }
641
642    #[test]
643    fn test_trim_ows_never_strips_vt_ff_nbsp() {
644        assert_eq!(trim_ows(b"\x0b a"), b"\x0b a");
645        assert_eq!(trim_ows(b"a\x0c"), b"a\x0c");
646        assert_eq!(trim_ows(b"\xc2\xa0a"), b"\xc2\xa0a");
647        assert_eq!(trim_ows(b""), b"");
648        assert_eq!(skip_ows(b""), 0);
649    }
650
651    #[test]
652    fn test_comment_edge_cases() {
653        let l = Limits::DEFAULT;
654        assert_eq!(parse_comment(b"()", &l).unwrap().0.raw(), b"");
655        assert_eq!(parse_comment(b"(a(b)c)", &l).unwrap().0.max_depth(), 2);
656        assert_eq!(parse_comment(b"(\\))", &l).unwrap().1, 4);
657        assert_eq!(
658            parse_comment(b"((a)", &l).unwrap_err().kind(),
659            ErrorKind::UnbalancedComment
660        );
661        assert_eq!(parse_comment(b"(a))", &l).unwrap().1, 3);
662        assert_eq!(
663            parse_comment(b"x", &l).unwrap_err().kind(),
664            ErrorKind::InvalidByte
665        );
666        assert_eq!(
667            parse_comment(b"(\\", &l).unwrap_err().kind(),
668            ErrorKind::UnbalancedComment
669        );
670        assert_eq!(
671            parse_comment(b"(\\\x00)", &l).unwrap_err().kind(),
672            ErrorKind::InvalidQuotedPair
673        );
674        assert_eq!(
675            parse_comment(b"(a\rb)", &l).unwrap_err().kind(),
676            ErrorKind::BareCr
677        );
678    }
679
680    #[test]
681    fn test_comment_depth_limit_plus_one_refused() {
682        let l = Limits::DEFAULT.max_comment_depth(3);
683        assert!(parse_comment(b"(((x)))", &l).is_ok());
684        let e = parse_comment(b"((((x))))", &l).unwrap_err();
685        assert_eq!((e.kind(), e.offset()), (ErrorKind::CommentTooDeep, 3));
686        let deep: Vec<u8> = core::iter::repeat_n(b'(', 10_000)
687            .chain(core::iter::repeat_n(b')', 10_000))
688            .collect();
689        assert_eq!(
690            parse_comment(&deep, &Limits::DEFAULT).unwrap_err().kind(),
691            ErrorKind::CommentTooDeep
692        );
693    }
694
695    #[test]
696    fn test_quoted_debug_escapes() {
697        let (v, _) = q(b"\"a\\\"\x80\"").unwrap();
698        assert_eq!(format!("{v:?}"), "Quoted(\"a\\\\\\\"\\x80\")");
699    }
700}