spinoso_string/enc/utf8/
owned.rs

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use alloc::collections::TryReserveError;
use alloc::vec::Vec;

use scolapasta_strbuf::Buf;

use super::Utf8Str;
use crate::chars::ConventionallyUtf8;
use crate::codepoints::InvalidCodepointError;
use crate::iter::IntoIter;

mod eq;
mod impls;
#[cfg(feature = "std")]
mod io;

#[repr(transparent)]
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Utf8String {
    inner: Buf,
}

// Constructors
impl Utf8String {
    #[inline]
    pub const fn new(buf: Buf) -> Self {
        Self { inner: buf }
    }

    #[inline]
    pub fn empty() -> Self {
        Self { inner: Buf::new() }
    }
}

// Raw
impl Utf8String {
    #[inline]
    #[must_use]
    pub(crate) fn into_buf(self) -> Buf {
        self.inner
    }

    #[inline]
    #[must_use]
    pub fn as_utf8_str(&self) -> &Utf8Str {
        Utf8Str::from_bytes(self.inner.as_slice())
    }

    #[inline]
    #[must_use]
    pub fn as_mut_utf8_str(&mut self) -> &mut Utf8Str {
        Utf8Str::from_bytes_mut(self.inner.as_mut_slice())
    }
}

// Core Iterators
impl Utf8String {
    #[inline]
    #[must_use]
    pub fn into_iter(self) -> IntoIter {
        IntoIter::from_vec(self.inner.into_inner())
    }
}

// Size and Capacity
impl Utf8String {
    #[inline]
    pub unsafe fn set_len(&mut self, len: usize) {
        self.inner.set_len(len);
    }

    #[inline]
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.inner.capacity()
    }

    #[inline]
    pub fn clear(&mut self) {
        self.inner.clear();
    }

    #[inline]
    pub fn truncate(&mut self, len: usize) {
        self.inner.truncate(len);
    }
}

// Memory management
impl Utf8String {
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.inner.reserve(additional);
    }

    #[inline]
    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
        self.inner.try_reserve(additional)
    }

    #[inline]
    pub fn reserve_exact(&mut self, additional: usize) {
        self.inner.reserve_exact(additional);
    }

    #[inline]
    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
        self.inner.try_reserve_exact(additional)
    }

    #[inline]
    pub fn shrink_to_fit(&mut self) {
        self.inner.shrink_to_fit();
    }

    #[inline]
    pub fn shrink_to(&mut self, min_capacity: usize) {
        self.inner.shrink_to(min_capacity);
    }
}

// Pushing and popping bytes, codepoints, and strings.
impl Utf8String {
    #[inline]
    pub fn push_byte(&mut self, byte: u8) {
        self.inner.push_byte(byte);
    }

    #[inline]
    pub fn try_push_codepoint(&mut self, codepoint: i64) -> Result<(), InvalidCodepointError> {
        let codepoint = if let Ok(codepoint) = u32::try_from(codepoint) {
            codepoint
        } else {
            return Err(InvalidCodepointError::codepoint_out_of_range(codepoint));
        };
        if let Ok(ch) = char::try_from(codepoint) {
            self.push_char(ch);
            Ok(())
        } else {
            Err(InvalidCodepointError::invalid_utf8_codepoint(codepoint))
        }
    }

    #[inline]
    pub fn try_push_int(&mut self, int: i64) -> Result<(), InvalidCodepointError> {
        self.try_push_codepoint(int)
    }

    #[inline]
    pub fn push_char(&mut self, ch: char) {
        self.inner.push_char(ch);
    }

    #[inline]
    pub fn push_str(&mut self, s: &str) {
        self.inner.push_str(s);
    }

    #[inline]
    pub fn extend_from_slice(&mut self, other: &[u8]) {
        self.inner.extend_from_slice(other);
    }
}

// Casing
impl Utf8String {
    // TODO: #1723 Use roe for case changing operations. UTF-8 case changing
    //       needs to be parameterized on the case folding strategy to account
    //       for e.g. Turkic or ASCII-only modes
    #[inline]
    pub fn make_capitalized(&mut self) {
        use bstr::ByteVec;

        // This allocation assumes that in the common case, capitalizing
        // and lower-casing `char`s do not change the length of the
        // `String`.
        //
        // Use a `Vec` here instead of a `Buf` to ensure at most one alloc
        // fix-up happens instead of alloc fix-ups being O(chars).
        let mut replacement = Vec::with_capacity(self.len());
        let mut bytes = self.inner.as_slice();

        let (ch, size) = bstr::decode_utf8(bytes);
        // SAFETY: bstr guarantees that the size is within the bounds of the slice.
        let (chunk, remainder) = unsafe { bytes.split_at_unchecked(size) };
        bytes = remainder;

        if let Some(ch) = ch {
            // Converting a UTF-8 character to uppercase may yield multiple codepoints.
            for ch in ch.to_uppercase() {
                replacement.push_char(ch);
            }
        } else {
            replacement.extend_from_slice(chunk);
        }

        while !bytes.is_empty() {
            let (ch, size) = bstr::decode_utf8(bytes);
            // SAFETY: bstr guarantees that the size is within the bounds of the slice.
            let (chunk, remainder) = unsafe { bytes.split_at_unchecked(size) };
            bytes = remainder;

            if let Some(ch) = ch {
                // Converting a UTF-8 character to lowercase may yield
                // multiple codepoints.
                for ch in ch.to_lowercase() {
                    replacement.push_char(ch);
                }
            } else {
                replacement.extend_from_slice(chunk);
            }
        }
        self.inner = replacement.into();
    }

    #[inline]
    pub fn make_lowercase(&mut self) {
        use bstr::ByteVec;

        // This allocation assumes that in the common case, lower-casing
        // `char`s do not change the length of the `String`.
        //
        // Use a `Vec` here instead of a `Buf` to ensure at most one alloc
        // fix-up happens instead of alloc fix-ups being O(chars).
        let mut replacement = Vec::with_capacity(self.len());
        let mut bytes = self.inner.as_slice();

        while !bytes.is_empty() {
            let (ch, size) = bstr::decode_utf8(bytes);
            // SAFETY: bstr guarantees that the size is within the bounds of the slice.
            let (chunk, remainder) = unsafe { bytes.split_at_unchecked(size) };
            bytes = remainder;

            if let Some(ch) = ch {
                // Converting a UTF-8 character to lowercase may yield
                // multiple codepoints.
                for ch in ch.to_lowercase() {
                    replacement.push_char(ch);
                }
            } else {
                replacement.extend_from_slice(chunk);
            }
        }
        self.inner = replacement.into();
    }

    #[inline]
    pub fn make_uppercase(&mut self) {
        use bstr::ByteVec;

        // This allocation assumes that in the common case, upper-casing
        // `char`s do not change the length of the `String`.
        //
        // Use a `Vec` here instead of a `Buf` to ensure at most one alloc
        // fix-up happens instead of alloc fix-ups being O(chars).
        let mut replacement = Vec::with_capacity(self.len());
        let mut bytes = self.inner.as_slice();

        while !bytes.is_empty() {
            let (ch, size) = bstr::decode_utf8(bytes);
            // SAFETY: bstr guarantees that the size is within the bounds of the slice.
            let (chunk, remainder) = unsafe { bytes.split_at_unchecked(size) };
            bytes = remainder;

            if let Some(ch) = ch {
                // Converting a UTF-8 character to lowercase may yield
                // multiple codepoints.
                for ch in ch.to_uppercase() {
                    replacement.push_char(ch);
                }
            } else {
                replacement.extend_from_slice(chunk);
            }
        }
        self.inner = replacement.into();
    }
}

// Reversing
impl Utf8String {
    #[inline]
    pub fn reverse(&mut self) {
        // Fast path when all characters are one byte wide.
        if self.is_ascii_only() {
            self.inner.reverse();
            return;
        }
        // FIXME: this allocation can go away if `ConventionallyUtf8` impls
        // `DoubleEndedIterator`.
        let chars = ConventionallyUtf8::from(&self.inner[..]).collect::<Vec<_>>();
        // Use a `Vec` here instead of a `Buf` to ensure at most one alloc
        // fix-up happens instead of alloc fix-ups being O(chars).
        let mut replacement = Vec::with_capacity(self.inner.len());
        for &bytes in chars.iter().rev() {
            replacement.extend_from_slice(bytes);
        }
        self.inner = replacement.into();
    }
}

#[cfg(test)]
mod tests {
    use bstr::ByteSlice;

    use super::Utf8String;

    #[test]
    fn reverse_ascii() {
        let mut s = Utf8String::from("1234");
        s.reverse();
        assert_eq!(s, "4321");
    }

    #[test]
    fn reverse_ascii_with_invalid_utf8() {
        let mut s = Utf8String::from(b"1234\xFF\xFE");
        s.reverse();
        assert_eq!(s, b"\xFE\xFF4321".as_bstr());
    }

    #[test]
    fn reverse_multibyte() {
        // ```console
        // [3.2.2] > "怎么样".reverse
        // => "样么怎"
        // ```
        let mut s = Utf8String::from("怎么样");
        s.reverse();
        assert_eq!(s, "样么怎");
    }

    #[test]
    fn reverse_multibyte_with_invalid_utf8() {
        // ```console
        // [3.2.2] > "怎么样\xFF\xFE".reverse
        // => => "\xFE\xFF样么怎"
        // ```
        let mut s = Utf8String::from("怎么样");
        s.extend_from_slice(b"\xFF\xFE");
        s.reverse();

        let mut expected = b"\xFE\xFF".to_vec();
        expected.extend_from_slice("样么怎".as_bytes());
        assert_eq!(s, expected.as_bstr());
    }

    #[test]
    fn reverse_replacement_char_with_invalid_utf8_prefix() {
        // the Unicode replacement char has the following byte contents:
        //
        // ```console
        // [3.2.2] > puts "�".b.inspect
        // "\xEF\xBF\xBD"
        // ```
        //
        // `\xF0\x9F\x87` is a valid UTF-8 prefix for a 4 byte sequence but is
        // not itself a valid byte sequence. We expect these 3 bytes to be
        // treated as 3 characters.
        let mut s = Utf8String::from(b"abc\xF0\x9F\x87def\xEF\xBF\xBD");
        s.reverse();
        assert_eq!(s, b"\xEF\xBF\xBDfed\x87\x9F\xF0cba".as_bstr());
    }
}