summaryrefslogtreecommitdiffstats
path: root/vendor/os_str_bytes/src/windows/raw.rs
blob: 630eb01ea691b97bb48d0f22557059cadfe4b057 (plain)
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
use std::fmt;
use std::fmt::Formatter;

pub(crate) use crate::util::is_continuation;

use super::wtf8;
pub(crate) use super::wtf8::ends_with;
pub(crate) use super::wtf8::starts_with;
use super::wtf8::CodePoints;

pub(crate) fn encode_wide_unchecked(
    string: &[u8],
) -> impl '_ + Iterator<Item = u16> {
    wtf8::encode_wide(string).map(|x| x.expect("invalid string"))
}

pub(crate) fn decode_code_point(string: &[u8]) -> u32 {
    let mut code_points = CodePoints::new(string.iter().copied());
    let code_point = code_points
        .next()
        .expect("cannot parse code point from empty string")
        .expect("invalid string");
    assert_eq!(None, code_points.next(), "multiple code points found");
    code_point
}

pub(crate) fn debug(string: &[u8], f: &mut Formatter<'_>) -> fmt::Result {
    for wchar in encode_wide_unchecked(string) {
        write!(f, "\\u{{{:X}}}", wchar)?;
    }
    Ok(())
}

#[cfg(feature = "uniquote")]
pub(crate) mod uniquote {
    use uniquote::Formatter;
    use uniquote::Result;

    pub(crate) fn escape(string: &[u8], f: &mut Formatter<'_>) -> Result {
        f.escape_utf16(super::encode_wide_unchecked(string))
    }
}