1 use std::fmt;
2 use std::fmt::Formatter;
3
4 pub(crate) use crate::util::is_continuation;
5
6 use super::wtf8;
7 pub(crate) use super::wtf8::ends_with;
8 pub(crate) use super::wtf8::starts_with;
9 use super::wtf8::CodePoints;
10 use super::Result;
11
validate_bytes(string: &[u8]) -> Result<()>12 pub(crate) fn validate_bytes(string: &[u8]) -> Result<()> {
13 wtf8::encode_wide(string).try_for_each(|x| x.map(drop))
14 }
15
encode_wide_unchecked( string: &[u8], ) -> impl '_ + Iterator<Item = u16>16 pub(crate) fn encode_wide_unchecked(
17 string: &[u8],
18 ) -> impl '_ + Iterator<Item = u16> {
19 wtf8::encode_wide(string).map(|x| expect_encoded!(x))
20 }
21
decode_code_point(string: &[u8]) -> u3222 pub(crate) fn decode_code_point(string: &[u8]) -> u32 {
23 let mut code_points = CodePoints::new(string.iter().copied());
24 let code_point = expect_encoded!(code_points
25 .next()
26 .expect("cannot parse code point from empty string"));
27 assert_eq!(None, code_points.next(), "multiple code points found");
28 code_point
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