aboutsummaryrefslogtreecommitdiff
path: root/src/windows/wtf8/convert.rs
blob: fcaf56274301f622fe4c9070f52e6aad39d3f6b3 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::char;
use std::char::DecodeUtf16;
use std::num::NonZeroU16;

use crate::util::BYTE_SHIFT;
use crate::util::CONT_MASK;
use crate::util::CONT_TAG;

use super::CodePoints;
use super::Result;

const MIN_HIGH_SURROGATE: u16 = 0xD800;

const MIN_LOW_SURROGATE: u16 = 0xDC00;

const MIN_SURROGATE_CODE: u32 = (u16::MAX as u32) + 1;

macro_rules! static_assert {
    ( $condition:expr ) => {
        const _: () = assert!($condition, "static assertion failed");
    };
}

pub(in super::super) struct DecodeWide<I>
where
    I: Iterator<Item = u16>,
{
    iter: DecodeUtf16<I>,
    code_point: u32,
    shift: u8,
}

impl<I> DecodeWide<I>
where
    I: Iterator<Item = u16>,
{
    pub(in super::super) fn new<S>(string: S) -> Self
    where
        S: IntoIterator<IntoIter = I, Item = I::Item>,
    {
        Self {
            iter: char::decode_utf16(string),
            code_point: 0,
            shift: 0,
        }
    }
}

impl<I> Iterator for DecodeWide<I>
where
    I: Iterator<Item = u16>,
{
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(shift) = self.shift.checked_sub(BYTE_SHIFT) {
            self.shift = shift;
            return Some(
                ((self.code_point >> self.shift) as u8 & CONT_MASK) | CONT_TAG,
            );
        }

        self.code_point = self
            .iter
            .next()?
            .map(Into::into)
            .unwrap_or_else(|x| x.unpaired_surrogate().into());

        macro_rules! decode {
            ( $tag:expr ) => {
                Some((self.code_point >> self.shift) as u8 | $tag)
            };
        }
        macro_rules! try_decode {
            ( $tag:expr , $upper_bound:expr ) => {
                if self.code_point < $upper_bound {
                    return decode!($tag);
                }
                self.shift += BYTE_SHIFT;
            };
        }
        try_decode!(0, 0x80);
        try_decode!(0xC0, 0x800);
        try_decode!(0xE0, MIN_SURROGATE_CODE);
        decode!(0xF0)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (low, high) = self.iter.size_hint();
        let shift = self.shift.into();
        (
            low.saturating_add(shift),
            high.and_then(|x| x.checked_mul(4))
                .and_then(|x| x.checked_add(shift)),
        )
    }
}

struct EncodeWide<I>
where
    I: Iterator<Item = u8>,
{
    iter: CodePoints<I>,
    surrogate: Option<NonZeroU16>,
}

impl<I> EncodeWide<I>
where
    I: Iterator<Item = u8>,
{
    pub(in super::super) fn new<S>(string: S) -> Self
    where
        S: IntoIterator<IntoIter = I, Item = I::Item>,
    {
        Self {
            iter: CodePoints::new(string),
            surrogate: None,
        }
    }
}

impl<I> Iterator for EncodeWide<I>
where
    I: Iterator<Item = u8>,
{
    type Item = Result<u16>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(surrogate) = self.surrogate.take() {
            return Some(Ok(surrogate.get()));
        }

        self.iter.next().map(|code_point| {
            code_point.map(|code_point| {
                code_point
                    .checked_sub(MIN_SURROGATE_CODE)
                    .map(|offset| {
                        static_assert!(MIN_LOW_SURROGATE != 0);

                        self.surrogate = Some(unsafe {
                            NonZeroU16::new_unchecked(
                                (offset & 0x3FF) as u16 | MIN_LOW_SURROGATE,
                            )
                        });
                        (offset >> 10) as u16 | MIN_HIGH_SURROGATE
                    })
                    .unwrap_or(code_point as u16)
            })
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (low, high) = self.iter.inner_size_hint();
        let additional = self.surrogate.is_some().into();
        (
            (low.saturating_add(2) / 3).saturating_add(additional),
            high.and_then(|x| x.checked_add(additional)),
        )
    }
}

pub(in super::super) fn encode_wide(
    string: &[u8],
) -> impl '_ + Iterator<Item = Result<u16>> {
    EncodeWide::new(string.iter().copied())
}