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
//! A wrapper for strings that have escape characters in them
use std::fmt::{self, Display, Formatter};
use std::iter::FusedIterator;
use std::str::Chars;

/// A string with backslash escapes in it
///
/// This wrapper allows referencing escaped strings in the source while preventing improper use.
/// Use [EscapedStr::as_raw_str] to access the underlying buffer, or the [Display] trait to get
/// owned versions. Use [EscapedStr::unescape] to access a [char] iter.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct EscapedStr {
    escaped: str,
}

impl EscapedStr {
    // NOTE this could panic if not called on a validated string
    pub(crate) fn new<S: AsRef<str> + ?Sized>(escaped: &S) -> &Self {
        unsafe { &*(escaped.as_ref() as *const str as *const EscapedStr) }
    }

    /// Access the underly buffer with escape sequences in it
    pub fn as_raw_str(&self) -> &str {
        &self.escaped
    }

    /// Get an iterator over the true characters
    pub fn unescape(&self) -> Unescaped<'_> {
        Unescaped {
            chars: self.escaped.chars(),
        }
    }
}

impl Display for EscapedStr {
    fn fmt(&self, out: &mut Formatter<'_>) -> Result<(), fmt::Error> {
        write!(out, "{}", self.unescape())
    }
}

/// An iterator over the true characters of an [EscapedStr]
#[derive(Debug, Clone)]
pub struct Unescaped<'a> {
    chars: Chars<'a>,
}

impl<'a> Display for Unescaped<'a> {
    fn fmt(&self, out: &mut Formatter<'_>) -> Result<(), fmt::Error> {
        for chr in self.clone() {
            write!(out, "{}", chr)?;
        }
        Ok(())
    }
}

impl<'a> Iterator for Unescaped<'a> {
    type Item = char;

    fn next(&mut self) -> Option<Self::Item> {
        match self.chars.next() {
            Some('\\') => Some(self.chars.next().unwrap()),
            chr => chr,
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (min, max) = self.chars.size_hint();
        ((min + 1) / 2, max)
    }
}

impl<'a> FusedIterator for Unescaped<'a> {}

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

    #[test]
    fn test_formatting() {
        let escaped = EscapedStr::new("air \\\" quote");
        let res = escaped.to_string();
        assert_eq!(res, "air \" quote");
    }
}