• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::fmt;
2 use std::fmt::Formatter;
3 
4 use crate::RawOsStr;
5 
6 pub(crate) use super::wtf8::ends_with;
7 pub(crate) use super::wtf8::starts_with;
8 
9 if_nightly! {
10     use std::os::windows::ffi::OsStrExt;
11 }
12 
13 if_not_nightly! {
14     pub(crate) use crate::util::is_continuation;
15 
16     use super::wtf8;
17     use super::wtf8::CodePoints;
18     use super::Result;
19 }
20 
21 if_not_nightly! {
22     pub(crate) fn validate_bytes(string: &[u8]) -> Result<()> {
23         wtf8::encode_wide(string).try_for_each(|x| x.map(drop))
24     }
25 }
26 
27 #[cfg_attr(not(feature = "nightly"), allow(deprecated))]
encode_wide( string: &RawOsStr, ) -> impl '_ + Iterator<Item = u16>28 pub(crate) fn encode_wide(
29     string: &RawOsStr,
30 ) -> impl '_ + Iterator<Item = u16> {
31     if_nightly_return! {
32         {
33             string.as_os_str().encode_wide()
34         }
35         wtf8::encode_wide(string.as_raw_bytes()).map(|x| expect_encoded!(x))
36     }
37 }
38 
39 if_not_nightly! {
40     pub(crate) fn decode_code_point(string: &[u8]) -> u32 {
41         let mut code_points = CodePoints::new(string.iter().copied());
42         let code_point = expect_encoded!(code_points
43             .next()
44             .expect("cannot parse code point from empty string"));
45         assert_eq!(None, code_points.next(), "multiple code points found");
46         code_point
47     }
48 }
49 
debug(string: &RawOsStr, f: &mut Formatter<'_>) -> fmt::Result50 pub(crate) fn debug(string: &RawOsStr, f: &mut Formatter<'_>) -> fmt::Result {
51     for wchar in encode_wide(string) {
52         write!(f, "\\u{{{:X}}}", wchar)?;
53     }
54     Ok(())
55 }
56 
57 #[cfg(feature = "uniquote")]
58 pub(crate) mod uniquote {
59     use uniquote::Formatter;
60     use uniquote::Result;
61 
62     use crate::RawOsStr;
63 
escape(string: &RawOsStr, f: &mut Formatter<'_>) -> Result64     pub(crate) fn escape(string: &RawOsStr, f: &mut Formatter<'_>) -> Result {
65         f.escape_utf16(super::encode_wide(string))
66     }
67 }
68