1 use std::fmt;
2 use std::fmt::Formatter;
3 
4 pub(crate) use crate::util::is_continuation;
5 
6 use super::wtf8;
7 use super::wtf8::CodePoints;
8 
encode_wide_unchecked(string: &[u8]) -> impl '_ + Iterator<Item = u16>9 fn encode_wide_unchecked(string: &[u8]) -> impl '_ + Iterator<Item = u16> {
10     wtf8::encode_wide(string).map(|x| x.expect("invalid string"))
11 }
12 
decode_code_point(string: &[u8]) -> u3213 pub(crate) fn decode_code_point(string: &[u8]) -> u32 {
14     let mut code_points = CodePoints::new(string.iter().copied());
15     let code_point = code_points
16         .next()
17         .expect("cannot parse code point from empty string")
18         .expect("invalid string");
19     assert_eq!(None, code_points.next(), "multiple code points found");
20     code_point
21 }
22 
ends_with(string: &[u8], suffix: &[u8]) -> bool23 pub(crate) fn ends_with(string: &[u8], suffix: &[u8]) -> bool {
24     wtf8::ends_with(string, suffix).unwrap_or(false)
25 }
26 
starts_with(string: &[u8], prefix: &[u8]) -> bool27 pub(crate) fn starts_with(string: &[u8], prefix: &[u8]) -> bool {
28     wtf8::starts_with(string, prefix).unwrap_or(false)
29 }
30 
debug(string: &[u8], f: &mut Formatter<'_>) -> fmt::Result31 pub(crate) fn debug(string: &[u8], f: &mut Formatter<'_>) -> fmt::Result {
32     for wchar in encode_wide_unchecked(string) {
33         write!(f, "\\u{{{:X}}}", wchar)?;
34     }
35     Ok(())
36 }
37 
38 #[cfg(feature = "uniquote")]
39 pub(crate) mod uniquote {
40     use uniquote::Formatter;
41     use uniquote::Result;
42 
escape(string: &[u8], f: &mut Formatter<'_>) -> Result43     pub(crate) fn escape(string: &[u8], f: &mut Formatter<'_>) -> Result {
44         f.escape_utf16(super::encode_wide_unchecked(string))
45     }
46 }
47