1 //! Operations on ASCII strings and characters.
3 //! Most string operations in Rust act on UTF-8 strings. However, at times it
4 //! makes more sense to only consider the ASCII character set for a specific
7 //! The [`escape_default`] function provides an iterator over the bytes of an
8 //! escaped version of the character given.
10 //! [`escape_default`]: fn.escape_default.html
12 #![stable(feature = "core_ascii", since = "1.26.0")]
15 use crate::ops::Range;
16 use crate::iter::FusedIterator;
18 /// An iterator over the escaped version of a byte.
20 /// This `struct` is created by the [`escape_default`] function. See its
21 /// documentation for more.
23 /// [`escape_default`]: fn.escape_default.html
24 #[stable(feature = "rust1", since = "1.0.0")]
25 pub struct EscapeDefault {
30 /// Returns an iterator that produces an escaped version of a `u8`.
32 /// The default is chosen with a bias toward producing literals that are
33 /// legal in a variety of languages, including C++11 and similar C-family
34 /// languages. The exact rules are:
36 /// * Tab is escaped as `\t`.
37 /// * Carriage return is escaped as `\r`.
38 /// * Line feed is escaped as `\n`.
39 /// * Single quote is escaped as `\'`.
40 /// * Double quote is escaped as `\"`.
41 /// * Backslash is escaped as `\\`.
42 /// * Any character in the 'printable ASCII' range `0x20` .. `0x7e`
43 /// inclusive is not escaped.
44 /// * Any other chars are given hex escapes of the form '\xNN'.
45 /// * Unicode escapes are never generated by this function.
52 /// let escaped = ascii::escape_default(b'0').next().unwrap();
53 /// assert_eq!(b'0', escaped);
55 /// let mut escaped = ascii::escape_default(b'\t');
57 /// assert_eq!(b'\\', escaped.next().unwrap());
58 /// assert_eq!(b't', escaped.next().unwrap());
60 /// let mut escaped = ascii::escape_default(b'\r');
62 /// assert_eq!(b'\\', escaped.next().unwrap());
63 /// assert_eq!(b'r', escaped.next().unwrap());
65 /// let mut escaped = ascii::escape_default(b'\n');
67 /// assert_eq!(b'\\', escaped.next().unwrap());
68 /// assert_eq!(b'n', escaped.next().unwrap());
70 /// let mut escaped = ascii::escape_default(b'\'');
72 /// assert_eq!(b'\\', escaped.next().unwrap());
73 /// assert_eq!(b'\'', escaped.next().unwrap());
75 /// let mut escaped = ascii::escape_default(b'"');
77 /// assert_eq!(b'\\', escaped.next().unwrap());
78 /// assert_eq!(b'"', escaped.next().unwrap());
80 /// let mut escaped = ascii::escape_default(b'\\');
82 /// assert_eq!(b'\\', escaped.next().unwrap());
83 /// assert_eq!(b'\\', escaped.next().unwrap());
85 /// let mut escaped = ascii::escape_default(b'\x9d');
87 /// assert_eq!(b'\\', escaped.next().unwrap());
88 /// assert_eq!(b'x', escaped.next().unwrap());
89 /// assert_eq!(b'9', escaped.next().unwrap());
90 /// assert_eq!(b'd', escaped.next().unwrap());
92 #[stable(feature = "rust1", since = "1.0.0")]
93 pub fn escape_default(c: u8) -> EscapeDefault {
94 let (data, len) = match c {
95 b'\t' => ([b'\\', b't', 0, 0], 2),
96 b'\r' => ([b'\\', b'r', 0, 0], 2),
97 b'\n' => ([b'\\', b'n', 0, 0], 2),
98 b'\\' => ([b'\\', b'\\', 0, 0], 2),
99 b'\'' => ([b'\\', b'\'', 0, 0], 2),
100 b'"' => ([b'\\', b'"', 0, 0], 2),
101 b'\x20' ..= b'\x7e' => ([c, 0, 0, 0], 1),
102 _ => ([b'\\', b'x', hexify(c >> 4), hexify(c & 0xf)], 4),
105 return EscapeDefault { range: 0..len, data };
107 fn hexify(b: u8) -> u8 {
115 #[stable(feature = "rust1", since = "1.0.0")]
116 impl Iterator for EscapeDefault {
118 fn next(&mut self) -> Option<u8> { self.range.next().map(|i| self.data[i]) }
119 fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
120 fn last(mut self) -> Option<u8> { self.next_back() }
122 #[stable(feature = "rust1", since = "1.0.0")]
123 impl DoubleEndedIterator for EscapeDefault {
124 fn next_back(&mut self) -> Option<u8> {
125 self.range.next_back().map(|i| self.data[i])
128 #[stable(feature = "rust1", since = "1.0.0")]
129 impl ExactSizeIterator for EscapeDefault {}
130 #[stable(feature = "fused", since = "1.26.0")]
131 impl FusedIterator for EscapeDefault {}
133 #[stable(feature = "std_debug", since = "1.16.0")]
134 impl fmt::Debug for EscapeDefault {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 f.pad("EscapeDefault { .. }")