hstr/wtf8/
mod.rs

1// Copyright (c) 2014 Simon Sapin
2// Licensed under the MIT License
3// Original source: https://github.com/SimonSapin/rust-wtf8
4
5/*!
6
7Implementation of [the WTF-8 encoding](https://simonsapin.github.io/wtf-8/).
8
9This library uses Rust’s type system to maintain
10[well-formedness](https://simonsapin.github.io/wtf-8/#well-formed),
11like the `String` and `&str` types do for UTF-8.
12
13Since [WTF-8 must not be used
14for interchange](https://simonsapin.github.io/wtf-8/#intended-audience),
15this library deliberately does not provide access to the underlying bytes
16of WTF-8 strings,
17nor can it decode WTF-8 from arbitrary bytes.
18WTF-8 strings can be obtained from UTF-8, UTF-16, or code points.
19
20*/
21
22extern crate alloc;
23
24use alloc::{
25    borrow::{Borrow, Cow},
26    string::String,
27    vec::Vec,
28};
29use core::{
30    cmp::Ordering,
31    fmt, hash,
32    iter::{FromIterator, IntoIterator},
33    mem::transmute,
34    ops::Deref,
35    slice, str,
36    str::FromStr,
37};
38use std::ops::Add;
39
40mod not_quite_std;
41
42static UTF8_REPLACEMENT_CHARACTER: &[u8] = b"\xEF\xBF\xBD";
43
44/// A Unicode code point: from U+0000 to U+10FFFF.
45///
46/// Compare with the `char` type,
47/// which represents a Unicode scalar value:
48/// a code point that is not a surrogate (U+D800 to U+DFFF).
49#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
50pub struct CodePoint {
51    value: u32,
52}
53
54impl Copy for CodePoint {}
55
56/// Format the code point as `U+` followed by four to six hexadecimal digits.
57/// Example: `U+1F4A9`
58impl fmt::Debug for CodePoint {
59    #[inline]
60    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
61        write!(formatter, "U+{:04X}", self.value)
62    }
63}
64
65impl CodePoint {
66    /// Unsafely create a new `CodePoint` without checking the value.
67    ///
68    /// # Safety
69    ///
70    /// Only use when `value` is known to be less than or equal to 0x10FFFF.
71    #[inline]
72    pub const unsafe fn from_u32_unchecked(value: u32) -> CodePoint {
73        CodePoint { value }
74    }
75
76    /// Create a new `CodePoint` if the value is a valid code point.
77    ///
78    /// Return `None` if `value` is above 0x10FFFF.
79    #[inline]
80    pub const fn from_u32(value: u32) -> Option<CodePoint> {
81        match value {
82            0..=0x10ffff => Some(CodePoint { value }),
83            _ => None,
84        }
85    }
86
87    /// Create a new `CodePoint` from a `char`.
88    ///
89    /// Since all Unicode scalar values are code points, this always succeds.
90    #[inline]
91    pub const fn from_char(value: char) -> CodePoint {
92        CodePoint {
93            value: value as u32,
94        }
95    }
96
97    /// Return the numeric value of the code point.
98    #[inline]
99    pub fn to_u32(&self) -> u32 {
100        self.value
101    }
102
103    /// Optionally return a Unicode scalar value for the code point.
104    ///
105    /// Return `None` if the code point is a surrogate (from U+D800 to U+DFFF).
106    #[inline]
107    pub fn to_char(&self) -> Option<char> {
108        match self.value {
109            0xd800..=0xdfff => None,
110            _ => Some(unsafe { char::from_u32_unchecked(self.value) }),
111        }
112    }
113
114    /// Return a Unicode scalar value for the code point.
115    ///
116    /// Return `'\u{FFFD}'` (the replacement character “�”)
117    /// if the code point is a surrogate (from U+D800 to U+DFFF).
118    #[inline]
119    pub fn to_char_lossy(&self) -> char {
120        self.to_char().unwrap_or('\u{FFFD}')
121    }
122
123    /// Return `true` if the code point is in the ASCII range.
124    #[inline]
125    pub fn is_ascii(&self) -> bool {
126        self.value <= 0x7f
127    }
128}
129
130impl PartialEq<char> for CodePoint {
131    fn eq(&self, other: &char) -> bool {
132        self.value == *other as u32
133    }
134}
135
136/// An owned, growable string of well-formed WTF-8 data.
137///
138/// Similar to `String`, but can additionally contain surrogate code points
139/// if they’re not in a surrogate pair.
140#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
141pub struct Wtf8Buf {
142    bytes: Vec<u8>,
143}
144
145impl Deref for Wtf8Buf {
146    type Target = Wtf8;
147
148    fn deref(&self) -> &Wtf8 {
149        unsafe { transmute(&*self.bytes) }
150    }
151}
152
153/// Format the string with double quotes,
154/// and surrogates as `\u` followed by four hexadecimal digits.
155/// Example: `"a\u{D800}"` for a string with code points [U+0061, U+D800]
156impl fmt::Debug for Wtf8Buf {
157    #[inline]
158    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
159        Wtf8::fmt(self, formatter)
160    }
161}
162
163impl Default for Wtf8Buf {
164    #[inline]
165    fn default() -> Self {
166        Self::new()
167    }
168}
169
170impl FromStr for Wtf8Buf {
171    type Err = core::convert::Infallible;
172
173    #[inline]
174    fn from_str(s: &str) -> Result<Self, Self::Err> {
175        Ok(Wtf8Buf {
176            bytes: s.as_bytes().to_vec(),
177        })
178    }
179}
180
181impl fmt::Write for Wtf8Buf {
182    fn write_str(&mut self, s: &str) -> std::fmt::Result {
183        self.push_str(s);
184        Ok(())
185    }
186}
187
188impl Add<&Wtf8> for Wtf8Buf {
189    type Output = Wtf8Buf;
190
191    fn add(self, rhs: &Wtf8) -> Self::Output {
192        let mut result = self;
193        result.push_wtf8(rhs);
194        result
195    }
196}
197
198impl Wtf8Buf {
199    /// Create an new, empty WTF-8 string.
200    #[inline]
201    pub fn new() -> Wtf8Buf {
202        Wtf8Buf { bytes: Vec::new() }
203    }
204
205    /// Create an new, empty WTF-8 string with pre-allocated capacity for `n`
206    /// bytes.
207    #[inline]
208    pub fn with_capacity(n: usize) -> Wtf8Buf {
209        Wtf8Buf {
210            bytes: Vec::with_capacity(n),
211        }
212    }
213
214    /// Create a WTF-8 string from an UTF-8 `String`.
215    ///
216    /// This takes ownership of the `String` and does not copy.
217    ///
218    /// Since WTF-8 is a superset of UTF-8, this always succeeds.
219    #[inline]
220    pub fn from_string(string: String) -> Wtf8Buf {
221        Wtf8Buf {
222            bytes: string.into_bytes(),
223        }
224    }
225
226    /// Create a WTF-8 string from an UTF-8 `&str` slice.
227    ///
228    /// This copies the content of the slice.
229    ///
230    /// Since WTF-8 is a superset of UTF-8, this always succeeds.
231    #[inline]
232    #[allow(clippy::should_implement_trait)]
233    pub fn from_str(s: &str) -> Wtf8Buf {
234        Wtf8Buf {
235            bytes: s.as_bytes().to_vec(),
236        }
237    }
238
239    /// Create a WTF-8 string from a potentially ill-formed UTF-16 slice of
240    /// 16-bit code units.
241    ///
242    /// This is lossless: calling `.to_ill_formed_utf16()` on the resulting
243    /// string will always return the original code units.
244    pub fn from_ill_formed_utf16(v: &[u16]) -> Wtf8Buf {
245        let mut string = Wtf8Buf::with_capacity(v.len());
246        for item in not_quite_std::decode_utf16(v.iter().cloned()) {
247            match item {
248                Ok(c) => string.push_char(c),
249                Err(s) => {
250                    // Surrogates are known to be in the code point range.
251                    let code_point = unsafe { CodePoint::from_u32_unchecked(s as u32) };
252                    // Skip the WTF-8 concatenation check,
253                    // surrogate pairs are already decoded by utf16_items
254                    not_quite_std::push_code_point(&mut string, code_point)
255                }
256            }
257        }
258        string
259    }
260
261    /// Reserves capacity for at least `additional` more bytes to be inserted
262    /// in the given `Wtf8Buf`.
263    /// The collection may reserve more space to avoid frequent reallocations.
264    ///
265    /// # Panics
266    ///
267    /// Panics if the new capacity overflows `usize`.
268    #[inline]
269    pub fn reserve(&mut self, additional: usize) {
270        self.bytes.reserve(additional)
271    }
272
273    /// Returns the number of bytes that this string buffer can hold without
274    /// reallocating.
275    #[inline]
276    pub fn capacity(&self) -> usize {
277        self.bytes.capacity()
278    }
279
280    /// Append an UTF-8 slice at the end of the string.
281    #[inline]
282    pub fn push_str(&mut self, other: &str) {
283        self.bytes.extend_from_slice(other.as_bytes())
284    }
285
286    /// Append a WTF-8 slice at the end of the string.
287    ///
288    /// This replaces newly paired surrogates at the boundary
289    /// with a supplementary code point,
290    /// like concatenating ill-formed UTF-16 strings effectively would.
291    #[inline]
292    pub fn push_wtf8(&mut self, other: &Wtf8) {
293        match (self.final_lead_surrogate(), other.initial_trail_surrogate()) {
294            // Replace newly paired surrogates by a supplementary code point.
295            (Some(lead), Some(trail)) => {
296                let len_without_lead_surrogate = self.len() - 3;
297                self.bytes.truncate(len_without_lead_surrogate);
298                let other_without_trail_surrogate = &other.bytes[3..];
299                // 4 bytes for the supplementary code point
300                self.bytes.reserve(4 + other_without_trail_surrogate.len());
301                self.push_char(decode_surrogate_pair(lead, trail));
302                self.bytes.extend_from_slice(other_without_trail_surrogate);
303            }
304            _ => self.bytes.extend_from_slice(&other.bytes),
305        }
306    }
307
308    /// Append a Unicode scalar value at the end of the string.
309    #[inline]
310    pub fn push_char(&mut self, c: char) {
311        not_quite_std::push_code_point(self, CodePoint::from_char(c))
312    }
313
314    /// Append a code point at the end of the string.
315    ///
316    /// This replaces newly paired surrogates at the boundary
317    /// with a supplementary code point,
318    /// like concatenating ill-formed UTF-16 strings effectively would.
319    #[inline]
320    pub fn push(&mut self, code_point: CodePoint) {
321        if let trail @ 0xdc00..=0xdfff = code_point.to_u32() {
322            if let Some(lead) = self.final_lead_surrogate() {
323                let len_without_lead_surrogate = self.len() - 3;
324                self.bytes.truncate(len_without_lead_surrogate);
325                self.push_char(decode_surrogate_pair(lead, trail as u16));
326                return;
327            }
328        }
329
330        // No newly paired surrogates at the boundary.
331        not_quite_std::push_code_point(self, code_point)
332    }
333
334    /// Shortens a string to the specified length.
335    ///
336    /// # Failure
337    ///
338    /// Fails if `new_len` > current length,
339    /// or if `new_len` is not a code point boundary.
340    #[inline]
341    pub fn truncate(&mut self, new_len: usize) {
342        assert!(not_quite_std::is_code_point_boundary(self, new_len));
343        self.bytes.truncate(new_len)
344    }
345
346    /// Clear the WTF-8 vector, removing all contents.
347    #[inline]
348    pub fn clear(&mut self) {
349        self.bytes.clear();
350    }
351
352    /// Consume the WTF-8 string and try to convert it to UTF-8.
353    ///
354    /// This does not copy the data.
355    ///
356    /// If the contents are not well-formed UTF-8
357    /// (that is, if the string contains surrogates),
358    /// the original WTF-8 string is returned instead.
359    pub fn into_string(self) -> Result<String, Wtf8Buf> {
360        match self.next_surrogate(0) {
361            None => Ok(unsafe { String::from_utf8_unchecked(self.bytes) }),
362            Some(_) => Err(self),
363        }
364    }
365
366    /// Consume the WTF-8 string and convert it lossily to UTF-8.
367    ///
368    /// This does not copy the data (but may overwrite parts of it in place).
369    ///
370    /// Surrogates are replaced with `"\u{FFFD}"` (the replacement character
371    /// “�”)
372    pub fn into_string_lossy(mut self) -> String {
373        let mut pos = 0;
374        loop {
375            match self.next_surrogate(pos) {
376                Some((surrogate_pos, _)) => {
377                    pos = surrogate_pos + 3;
378                    self.bytes[surrogate_pos..pos].copy_from_slice(UTF8_REPLACEMENT_CHARACTER);
379                }
380                None => return unsafe { String::from_utf8_unchecked(self.bytes) },
381            }
382        }
383    }
384
385    /// Create a [Wtf8Buf] from a WTF-8 encoded byte vector.
386    ///
387    /// Returns `Ok(Wtf8Buf)` if the bytes are well-formed WTF-8, or
388    /// `Err(bytes)` with the original bytes if validation fails.
389    ///
390    /// This validates that:
391    /// - All bytes form valid UTF-8 sequences OR valid surrogate code point
392    ///   encodings
393    /// - Surrogate code points may appear unpaired and be encoded separately,
394    ///   but if they are paired, they must be encoded as a single 4-byte UTF-8
395    ///   sequence. For example, the byte sequence `[0xED, 0xA0, 0x80, 0xED,
396    ///   0xB0, 0x80]` is not valid WTF-8 because WTF-8 forbids encoding a
397    ///   surrogate pair as two separate 3-byte sequences.
398    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, Vec<u8>> {
399        if not_quite_std::validate_wtf8(&bytes) {
400            Ok(Self { bytes })
401        } else {
402            Err(bytes)
403        }
404    }
405
406    /// Create a [Wtf8Buf] from a WTF-8 encoded byte vector without checking
407    /// that the bytes contain valid WTF-8.
408    ///
409    /// For the safe version, see [Wtf8Buf::from_bytes].
410    ///
411    /// # Safety
412    ///
413    /// The bytes passed in must be valid WTF-8. See [Wtf8Buf::from_bytes] for
414    /// the requirements.
415    #[inline]
416    pub unsafe fn from_bytes_unchecked(bytes: Vec<u8>) -> Self {
417        Self { bytes }
418    }
419}
420
421/// Create a new WTF-8 string from an iterator of code points.
422///
423/// This replaces surrogate code point pairs with supplementary code points,
424/// like concatenating ill-formed UTF-16 strings effectively would.
425impl FromIterator<CodePoint> for Wtf8Buf {
426    fn from_iter<T: IntoIterator<Item = CodePoint>>(iterable: T) -> Wtf8Buf {
427        let mut string = Wtf8Buf::new();
428        string.extend(iterable);
429        string
430    }
431}
432
433/// Append code points from an iterator to the string.
434///
435/// This replaces surrogate code point pairs with supplementary code points,
436/// like concatenating ill-formed UTF-16 strings effectively would.
437impl Extend<CodePoint> for Wtf8Buf {
438    fn extend<T: IntoIterator<Item = CodePoint>>(&mut self, iterable: T) {
439        let iterator = iterable.into_iter();
440        let (low, _high) = iterator.size_hint();
441        // Lower bound of one byte per code point (ASCII only)
442        self.bytes.reserve(low);
443        for code_point in iterator {
444            self.push(code_point);
445        }
446    }
447}
448
449/// A borrowed slice of well-formed WTF-8 data.
450///
451/// Similar to `&str`, but can additionally contain surrogate code points
452/// if they're not in a surrogate pair.
453#[repr(transparent)]
454pub struct Wtf8 {
455    bytes: [u8],
456}
457
458// FIXME: https://github.com/rust-lang/rust/issues/18805
459impl PartialEq for Wtf8 {
460    fn eq(&self, other: &Wtf8) -> bool {
461        self.bytes.eq(&other.bytes)
462    }
463}
464
465// FIXME: https://github.com/rust-lang/rust/issues/18805
466impl Eq for Wtf8 {}
467
468// FIXME: https://github.com/rust-lang/rust/issues/18738
469impl PartialOrd for Wtf8 {
470    #[inline]
471    fn partial_cmp(&self, other: &Wtf8) -> Option<Ordering> {
472        Some(self.bytes.cmp(&other.bytes))
473    }
474
475    #[inline]
476    fn lt(&self, other: &Wtf8) -> bool {
477        self.bytes.lt(&other.bytes)
478    }
479
480    #[inline]
481    fn le(&self, other: &Wtf8) -> bool {
482        self.bytes.le(&other.bytes)
483    }
484
485    #[inline]
486    fn gt(&self, other: &Wtf8) -> bool {
487        self.bytes.gt(&other.bytes)
488    }
489
490    #[inline]
491    fn ge(&self, other: &Wtf8) -> bool {
492        self.bytes.ge(&other.bytes)
493    }
494}
495
496// FIXME: https://github.com/rust-lang/rust/issues/18738
497impl Ord for Wtf8 {
498    #[inline]
499    fn cmp(&self, other: &Wtf8) -> Ordering {
500        self.bytes.cmp(&other.bytes)
501    }
502}
503
504/// Format the slice with double quotes,
505/// and surrogates as `\u` followed by four hexadecimal digits.
506/// Example: `"a\u{D800}"` for a slice with code points [U+0061, U+D800]
507impl fmt::Debug for Wtf8 {
508    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
509        formatter.write_str("\"")?;
510        let mut pos = 0;
511        loop {
512            match self.next_surrogate(pos) {
513                None => break,
514                Some((surrogate_pos, surrogate)) => {
515                    formatter.write_str(unsafe {
516                        str::from_utf8_unchecked(&self.bytes[pos..surrogate_pos])
517                    })?;
518                    write!(formatter, "\\u{{{surrogate:X}}}")?;
519                    pos = surrogate_pos + 3;
520                }
521            }
522        }
523        formatter.write_str(unsafe { str::from_utf8_unchecked(&self.bytes[pos..]) })?;
524        formatter.write_str("\"")
525    }
526}
527
528impl Wtf8 {
529    /// Create a WTF-8 slice from a UTF-8 `&str` slice.
530    ///
531    /// Since WTF-8 is a superset of UTF-8, this always succeeds.
532    #[inline]
533    pub const fn from_str(value: &str) -> &Wtf8 {
534        unsafe { transmute(value.as_bytes()) }
535    }
536
537    /// Return the length, in WTF-8 bytes.
538    #[inline]
539    pub const fn len(&self) -> usize {
540        self.bytes.len()
541    }
542
543    /// Return `true` if the string has a length of zero bytes.
544    #[inline]
545    pub const fn is_empty(&self) -> bool {
546        self.bytes.is_empty()
547    }
548
549    /// Return `true` if the string contains only ASCII characters.
550    #[inline]
551    pub const fn is_ascii(&self) -> bool {
552        self.bytes.is_ascii()
553    }
554
555    /// Return a slice of the given string for the byte range [`begin`..`end`).
556    ///
557    /// # Failure
558    ///
559    /// Fails when `begin` and `end` do not point to code point boundaries,
560    /// or point beyond the end of the string.
561    #[inline]
562    pub fn slice(&self, begin: usize, end: usize) -> &Wtf8 {
563        // is_code_point_boundary checks that the index is in [0, .len()]
564        if begin <= end
565            && not_quite_std::is_code_point_boundary(self, begin)
566            && not_quite_std::is_code_point_boundary(self, end)
567        {
568            unsafe { not_quite_std::slice_unchecked(self, begin, end) }
569        } else {
570            not_quite_std::slice_error_fail(self, begin, end)
571        }
572    }
573
574    /// Return a slice of the given string from byte `begin` to its end.
575    ///
576    /// # Failure
577    ///
578    /// Fails when `begin` is not at a code point boundary,
579    /// or is beyond the end of the string.
580    #[inline]
581    pub fn slice_from(&self, begin: usize) -> &Wtf8 {
582        // is_code_point_boundary checks that the index is in [0, .len()]
583        if not_quite_std::is_code_point_boundary(self, begin) {
584            unsafe { not_quite_std::slice_unchecked(self, begin, self.len()) }
585        } else {
586            not_quite_std::slice_error_fail(self, begin, self.len())
587        }
588    }
589
590    /// Return a slice of the given string from its beginning to byte `end`.
591    ///
592    /// # Failure
593    ///
594    /// Fails when `end` is not at a code point boundary,
595    /// or is beyond the end of the string.
596    #[inline]
597    pub fn slice_to(&self, end: usize) -> &Wtf8 {
598        // is_code_point_boundary checks that the index is in [0, .len()]
599        if not_quite_std::is_code_point_boundary(self, end) {
600            unsafe { not_quite_std::slice_unchecked(self, 0, end) }
601        } else {
602            not_quite_std::slice_error_fail(self, 0, end)
603        }
604    }
605
606    /// Return the code point at `position` if it is in the ASCII range,
607    /// or `b'\xFF' otherwise.
608    ///
609    /// # Failure
610    ///
611    /// Fails if `position` is beyond the end of the string.
612    #[inline]
613    pub fn ascii_byte_at(&self, position: usize) -> u8 {
614        match self.bytes[position] {
615            ascii_byte @ 0x00..=0x7f => ascii_byte,
616            _ => 0xff,
617        }
618    }
619
620    /// Return an iterator for the string’s code points.
621    #[inline]
622    pub fn code_points(&self) -> Wtf8CodePoints {
623        Wtf8CodePoints {
624            bytes: self.bytes.iter(),
625        }
626    }
627
628    /// Returns `true` if this WTF-8 string contains the given character.
629    #[inline]
630    pub fn contains_char(&self, ch: char) -> bool {
631        let target = CodePoint::from_char(ch);
632        self.contains(target)
633    }
634
635    /// Returns `true` if this WTF-8 string contains the given code point.
636    #[inline]
637    pub fn contains(&self, code_point: CodePoint) -> bool {
638        self.code_points().any(|cp| cp == code_point)
639    }
640
641    /// Returns `true` if this WTF-8 string starts with the given UTF-8 string.
642    #[inline]
643    pub fn starts_with(&self, pattern: &str) -> bool {
644        if pattern.len() > self.len() {
645            return false;
646        }
647
648        let pattern_wtf8 = self.slice_to(pattern.len());
649        if let Some(pattern_str) = pattern_wtf8.as_str() {
650            pattern_str == pattern
651        } else {
652            false
653        }
654    }
655
656    /// Try to convert the string to UTF-8 and return a `&str` slice.
657    ///
658    /// Return `None` if the string contains surrogates.
659    ///
660    /// This does not copy the data.
661    #[inline]
662    pub fn as_str(&self) -> Option<&str> {
663        // Well-formed WTF-8 is also well-formed UTF-8
664        // if and only if it contains no surrogate.
665        match self.next_surrogate(0) {
666            None => Some(unsafe { str::from_utf8_unchecked(&self.bytes) }),
667            Some(_) => None,
668        }
669    }
670
671    /// Return the underlying WTF-8 bytes.
672    #[inline]
673    pub const fn as_bytes(&self) -> &[u8] {
674        &self.bytes
675    }
676
677    /// Lossily convert the string to UTF-8.
678    /// Return an UTF-8 `&str` slice if the contents are well-formed in UTF-8.
679    ///
680    /// Surrogates are replaced with `"\u{FFFD}"` (the replacement character
681    /// “�”).
682    ///
683    /// This only copies the data if necessary (if it contains any surrogate).
684    pub fn to_string_lossy(&self) -> Cow<str> {
685        let surrogate_pos = match self.next_surrogate(0) {
686            None => return Cow::Borrowed(unsafe { str::from_utf8_unchecked(&self.bytes) }),
687            Some((pos, _)) => pos,
688        };
689        let wtf8_bytes = &self.bytes;
690        let mut utf8_bytes = Vec::with_capacity(self.len());
691        utf8_bytes.extend_from_slice(&wtf8_bytes[..surrogate_pos]);
692        utf8_bytes.extend_from_slice(UTF8_REPLACEMENT_CHARACTER);
693        let mut pos = surrogate_pos + 3;
694        loop {
695            match self.next_surrogate(pos) {
696                Some((surrogate_pos, _)) => {
697                    utf8_bytes.extend_from_slice(&wtf8_bytes[pos..surrogate_pos]);
698                    utf8_bytes.extend_from_slice(UTF8_REPLACEMENT_CHARACTER);
699                    pos = surrogate_pos + 3;
700                }
701                None => {
702                    utf8_bytes.extend_from_slice(&wtf8_bytes[pos..]);
703                    return Cow::Owned(unsafe { String::from_utf8_unchecked(utf8_bytes) });
704                }
705            }
706        }
707    }
708
709    /// Convert the WTF-8 string to potentially ill-formed UTF-16
710    /// and return an iterator of 16-bit code units.
711    ///
712    /// This is lossless:
713    /// calling `Wtf8Buf::from_ill_formed_utf16` on the resulting code units
714    /// would always return the original WTF-8 string.
715    #[inline]
716    pub fn to_ill_formed_utf16(&self) -> IllFormedUtf16CodeUnits {
717        IllFormedUtf16CodeUnits {
718            code_points: self.code_points(),
719            extra: 0,
720        }
721    }
722
723    /// Returns the uppercase equivalent of this wtf8 slice, as a new [Wtf8Buf].
724    #[inline]
725    pub fn to_uppercase(&self) -> Wtf8Buf {
726        let mut result = Wtf8Buf::with_capacity(self.len());
727        for cp in self.code_points() {
728            if let Some(ch) = cp.to_char() {
729                for upper_ch in ch.to_uppercase() {
730                    result.push_char(upper_ch);
731                }
732            } else {
733                // Surrogates are known to be in the code point range.
734                let code_point = unsafe { CodePoint::from_u32_unchecked(cp.to_u32()) };
735                // Skip the WTF-8 concatenation check,
736                // surrogate pairs are already decoded by utf16_items
737                not_quite_std::push_code_point(&mut result, code_point)
738            }
739        }
740        result
741    }
742
743    /// Returns the lowercase equivalent of this wtf8 slice, as a new [Wtf8Buf].
744    #[inline]
745    pub fn to_lowercase(&self) -> Wtf8Buf {
746        let mut result = Wtf8Buf::with_capacity(self.len());
747        for cp in self.code_points() {
748            if let Some(ch) = cp.to_char() {
749                for lower_ch in ch.to_lowercase() {
750                    result.push_char(lower_ch);
751                }
752            } else {
753                // Surrogates are known to be in the code point range.
754                let code_point = unsafe { CodePoint::from_u32_unchecked(cp.to_u32()) };
755                // Skip the WTF-8 concatenation check,
756                // surrogate pairs are already decoded by utf16_items
757                not_quite_std::push_code_point(&mut result, code_point)
758            }
759        }
760        result
761    }
762
763    /// Create a WTF-8 slice from a WTF-8 encoded byte slice.
764    ///
765    /// Returns `Ok(&Wtf8)` if the bytes are well-formed WTF-8, or
766    /// `Err(bytes)` with the original byte slice if validation fails.
767    ///
768    /// This validates that:
769    /// - All bytes form valid UTF-8 sequences OR valid surrogate code point
770    ///   encodings
771    /// - Surrogate code points may appear unpaired and be encoded separately,
772    ///   but if they are paired, they must be encoded as a single 4-byte UTF-8
773    ///   sequence. For example, the byte sequence `[0xED, 0xA0, 0x80, 0xED,
774    ///   0xB0, 0x80]` is not valid WTF-8 because WTF-8 forbids encoding a
775    ///   surrogate pair as two separate 3-byte sequences.
776    pub fn from_bytes(bytes: &[u8]) -> Result<&Wtf8, &[u8]> {
777        if not_quite_std::validate_wtf8(bytes) {
778            Ok(unsafe { transmute::<&[u8], &Wtf8>(bytes) })
779        } else {
780            Err(bytes)
781        }
782    }
783
784    /// Create a WTF-8 slice from a WTF-8 encoded byte slice without checking
785    /// that the bytes contain valid WTF-8.
786    ///
787    /// For the safe version, see [Wtf8::from_bytes].
788    ///
789    /// # Safety
790    ///
791    /// The bytes passed in must be valid WTF-8. See [Wtf8::from_bytes] for
792    /// the requirements.
793    #[inline]
794    pub const unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Wtf8 {
795        unsafe { transmute(bytes) }
796    }
797
798    #[inline]
799    fn next_surrogate(&self, mut pos: usize) -> Option<(usize, u16)> {
800        let mut iter = self.bytes[pos..].iter();
801        loop {
802            let b = match iter.next() {
803                None => return None,
804                Some(&b) => b,
805            };
806            if b < 0x80 {
807                pos += 1;
808            } else if b < 0xe0 {
809                iter.next();
810                pos += 2;
811            } else if b == 0xed {
812                match (iter.next(), iter.next()) {
813                    (Some(&b2), Some(&b3)) if b2 >= 0xa0 => {
814                        return Some((pos, decode_surrogate(b2, b3)))
815                    }
816                    _ => pos += 3,
817                }
818            } else if b < 0xf0 {
819                iter.next();
820                iter.next();
821                pos += 3;
822            } else {
823                iter.next();
824                iter.next();
825                iter.next();
826                pos += 4;
827            }
828        }
829    }
830
831    #[inline]
832    fn final_lead_surrogate(&self) -> Option<u16> {
833        let len = self.len();
834        if len < 3 {
835            return None;
836        }
837        let seq = &self.bytes[len - 3..];
838        if seq[0] == 0xed && 0xa0 <= seq[1] && seq[1] <= 0xaf {
839            Some(decode_surrogate(seq[1], seq[2]))
840        } else {
841            None
842        }
843    }
844
845    #[inline]
846    fn initial_trail_surrogate(&self) -> Option<u16> {
847        let len = self.len();
848        if len < 3 {
849            return None;
850        }
851        let seq = &self.bytes[..3];
852        if seq[0] == 0xed && 0xb0 <= seq[1] && seq[1] <= 0xbf {
853            Some(decode_surrogate(seq[1], seq[2]))
854        } else {
855            None
856        }
857    }
858}
859
860#[inline]
861fn decode_surrogate(second_byte: u8, third_byte: u8) -> u16 {
862    // The first byte is assumed to be 0xED
863    0xd800 | (second_byte as u16 & 0x3f) << 6 | third_byte as u16 & 0x3f
864}
865
866#[inline]
867fn decode_surrogate_pair(lead: u16, trail: u16) -> char {
868    let code_point = 0x10000 + (((lead as u32 - 0xd800) << 10) | (trail as u32 - 0xdc00));
869    unsafe { char::from_u32_unchecked(code_point) }
870}
871
872/// Iterator for the code points of a WTF-8 string.
873///
874/// Created with the method `.code_points()`.
875#[derive(Clone)]
876pub struct Wtf8CodePoints<'a> {
877    bytes: slice::Iter<'a, u8>,
878}
879
880impl<'a> Iterator for Wtf8CodePoints<'a> {
881    type Item = CodePoint;
882
883    #[inline]
884    fn next(&mut self) -> Option<CodePoint> {
885        not_quite_std::next_code_point(&mut self.bytes).map(|value| {
886            // Wtf8 invariant says `value` is a valid code point
887            unsafe { CodePoint::from_u32_unchecked(value) }
888        })
889    }
890
891    #[inline]
892    fn size_hint(&self) -> (usize, Option<usize>) {
893        let (len, _) = self.bytes.size_hint();
894        (len.saturating_add(3) / 4, Some(len))
895    }
896}
897
898#[derive(Clone)]
899pub struct IllFormedUtf16CodeUnits<'a> {
900    code_points: Wtf8CodePoints<'a>,
901    extra: u16,
902}
903
904impl<'a> Iterator for IllFormedUtf16CodeUnits<'a> {
905    type Item = u16;
906
907    #[inline]
908    fn next(&mut self) -> Option<u16> {
909        not_quite_std::next_utf16_code_unit(self)
910    }
911
912    #[inline]
913    fn size_hint(&self) -> (usize, Option<usize>) {
914        let (low, high) = self.code_points.size_hint();
915        // every code point gets either one u16 or two u16,
916        // so this iterator is between 1 or 2 times as
917        // long as the underlying iterator.
918        (low, high.and_then(|n| n.checked_mul(2)))
919    }
920}
921
922impl PartialEq<&Wtf8> for Wtf8Buf {
923    fn eq(&self, other: &&Wtf8) -> bool {
924        **self == **other
925    }
926}
927
928impl PartialEq<Wtf8Buf> for &Wtf8 {
929    fn eq(&self, other: &Wtf8Buf) -> bool {
930        **self == **other
931    }
932}
933
934impl PartialEq<str> for &Wtf8 {
935    fn eq(&self, other: &str) -> bool {
936        match self.as_str() {
937            Some(s) => s == other,
938            None => false,
939        }
940    }
941}
942
943impl PartialEq<&str> for &Wtf8 {
944    fn eq(&self, other: &&str) -> bool {
945        match self.as_str() {
946            Some(s) => s == *other,
947            None => false,
948        }
949    }
950}
951
952impl hash::Hash for CodePoint {
953    #[inline]
954    fn hash<H: hash::Hasher>(&self, state: &mut H) {
955        self.value.hash(state)
956    }
957}
958
959impl hash::Hash for Wtf8Buf {
960    #[inline]
961    fn hash<H: hash::Hasher>(&self, state: &mut H) {
962        Wtf8::hash(self, state)
963    }
964}
965
966impl hash::Hash for Wtf8 {
967    #[inline]
968    fn hash<H: hash::Hasher>(&self, state: &mut H) {
969        state.write(&self.bytes);
970        0xfeu8.hash(state)
971    }
972}
973
974impl Borrow<Wtf8> for Wtf8Buf {
975    #[inline]
976    fn borrow(&self) -> &Wtf8 {
977        self
978    }
979}
980
981impl ToOwned for Wtf8 {
982    type Owned = Wtf8Buf;
983
984    #[inline]
985    fn to_owned(&self) -> Wtf8Buf {
986        Wtf8Buf {
987            bytes: self.bytes.to_vec(),
988        }
989    }
990}
991
992impl<'a> From<&'a Wtf8> for Cow<'a, Wtf8> {
993    #[inline]
994    fn from(s: &'a Wtf8) -> Cow<'a, Wtf8> {
995        Cow::Borrowed(s)
996    }
997}
998
999impl<'a> From<&'a str> for &'a Wtf8 {
1000    #[inline]
1001    fn from(s: &'a str) -> &'a Wtf8 {
1002        Wtf8::from_str(s)
1003    }
1004}
1005
1006impl<'a> From<Wtf8Buf> for Cow<'a, Wtf8> {
1007    #[inline]
1008    fn from(s: Wtf8Buf) -> Cow<'a, Wtf8> {
1009        Cow::Owned(s)
1010    }
1011}
1012
1013#[cfg(test)]
1014mod tests {
1015    use alloc::{format, vec};
1016    use core::mem::transmute;
1017
1018    use super::*;
1019
1020    #[test]
1021    fn code_point_from_u32() {
1022        assert!(CodePoint::from_u32(0).is_some());
1023        assert!(CodePoint::from_u32(0xd800).is_some());
1024        assert!(CodePoint::from_u32(0x10ffff).is_some());
1025        assert!(CodePoint::from_u32(0x110000).is_none());
1026    }
1027
1028    #[test]
1029    fn code_point_to_u32() {
1030        fn c(value: u32) -> CodePoint {
1031            CodePoint::from_u32(value).unwrap()
1032        }
1033        assert_eq!(c(0).to_u32(), 0);
1034        assert_eq!(c(0xd800).to_u32(), 0xd800);
1035        assert_eq!(c(0x10ffff).to_u32(), 0x10ffff);
1036    }
1037
1038    #[test]
1039    fn code_point_from_char() {
1040        assert_eq!(CodePoint::from_char('a').to_u32(), 0x61);
1041        assert_eq!(CodePoint::from_char('💩').to_u32(), 0x1f4a9);
1042    }
1043
1044    #[test]
1045    fn code_point_to_string() {
1046        let cp_a = CodePoint::from_char('a');
1047        assert_eq!(format!("{cp_a:?}"), "U+0061");
1048        let cp_poop = CodePoint::from_char('💩');
1049        assert_eq!(format!("{cp_poop:?}"), "U+1F4A9");
1050    }
1051
1052    #[test]
1053    fn code_point_to_char() {
1054        fn c(value: u32) -> CodePoint {
1055            CodePoint::from_u32(value).unwrap()
1056        }
1057        assert_eq!(c(0x61).to_char(), Some('a'));
1058        assert_eq!(c(0x1f4a9).to_char(), Some('💩'));
1059        assert_eq!(c(0xd800).to_char(), None);
1060    }
1061
1062    #[test]
1063    fn code_point_to_char_lossy() {
1064        fn c(value: u32) -> CodePoint {
1065            CodePoint::from_u32(value).unwrap()
1066        }
1067        assert_eq!(c(0x61).to_char_lossy(), 'a');
1068        assert_eq!(c(0x1f4a9).to_char_lossy(), '💩');
1069        assert_eq!(c(0xd800).to_char_lossy(), '\u{FFFD}');
1070    }
1071
1072    #[test]
1073    fn wtf8buf_new() {
1074        assert_eq!(Wtf8Buf::new().bytes, b"");
1075    }
1076
1077    #[test]
1078    fn wtf8buf_from_str() {
1079        assert_eq!(Wtf8Buf::from_str("").bytes, b"");
1080        assert_eq!(
1081            Wtf8Buf::from_str("aé 💩").bytes,
1082            b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1083        );
1084    }
1085
1086    #[test]
1087    fn wtf8buf_from_string() {
1088        assert_eq!(Wtf8Buf::from_string(String::from("")).bytes, b"");
1089        assert_eq!(
1090            Wtf8Buf::from_string(String::from("aé 💩")).bytes,
1091            b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1092        );
1093    }
1094
1095    #[test]
1096    fn wtf8buf_from_ill_formed_utf16() {
1097        assert_eq!(Wtf8Buf::from_ill_formed_utf16(&[]).bytes, b"");
1098        assert_eq!(
1099            Wtf8Buf::from_ill_formed_utf16(&[0x61, 0xe9, 0x20, 0xd83d, 0xd83d, 0xdca9]).bytes,
1100            b"a\xC3\xA9 \xED\xA0\xBD\xF0\x9F\x92\xA9"
1101        );
1102    }
1103
1104    #[test]
1105    fn wtf8buf_push_str() {
1106        let mut string = Wtf8Buf::new();
1107        assert_eq!(string.bytes, b"");
1108        string.push_str("aé 💩");
1109        assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
1110    }
1111
1112    #[test]
1113    fn wtf8buf_push_char() {
1114        let mut string = Wtf8Buf::from_str("aé ");
1115        assert_eq!(string.bytes, b"a\xC3\xA9 ");
1116        string.push_char('💩');
1117        assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
1118    }
1119
1120    #[test]
1121    fn wtf8buf_push() {
1122        let mut string = Wtf8Buf::from_str("aé ");
1123        assert_eq!(string.bytes, b"a\xC3\xA9 ");
1124        string.push(CodePoint::from_char('💩'));
1125        assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
1126
1127        fn c(value: u32) -> CodePoint {
1128            CodePoint::from_u32(value).unwrap()
1129        }
1130
1131        let mut string = Wtf8Buf::new();
1132        string.push(c(0xd83d)); // lead
1133        string.push(c(0xdca9)); // trail
1134        assert_eq!(string.bytes, b"\xF0\x9F\x92\xA9"); // Magic!
1135
1136        let mut string = Wtf8Buf::new();
1137        string.push(c(0xd83d)); // lead
1138        string.push(c(0x20)); // not surrogate
1139        string.push(c(0xdca9)); // trail
1140        assert_eq!(string.bytes, b"\xED\xA0\xBD \xED\xB2\xA9");
1141
1142        let mut string = Wtf8Buf::new();
1143        string.push(c(0xd800)); // lead
1144        string.push(c(0xdbff)); // lead
1145        assert_eq!(string.bytes, b"\xED\xA0\x80\xED\xAF\xBF");
1146
1147        let mut string = Wtf8Buf::new();
1148        string.push(c(0xd800)); // lead
1149        string.push(c(0xe000)); // not surrogate
1150        assert_eq!(string.bytes, b"\xED\xA0\x80\xEE\x80\x80");
1151
1152        let mut string = Wtf8Buf::new();
1153        string.push(c(0xd7ff)); // not surrogate
1154        string.push(c(0xdc00)); // trail
1155        assert_eq!(string.bytes, b"\xED\x9F\xBF\xED\xB0\x80");
1156
1157        let mut string = Wtf8Buf::new();
1158        string.push(c(0x61)); // not surrogate, < 3 bytes
1159        string.push(c(0xdc00)); // trail
1160        assert_eq!(string.bytes, b"\x61\xED\xB0\x80");
1161
1162        let mut string = Wtf8Buf::new();
1163        string.push(c(0xdc00)); // trail
1164        assert_eq!(string.bytes, b"\xED\xB0\x80");
1165    }
1166
1167    #[test]
1168    fn wtf8buf_push_wtf8() {
1169        let mut string = Wtf8Buf::from_str("aé");
1170        assert_eq!(string.bytes, b"a\xC3\xA9");
1171        string.push_wtf8(Wtf8::from_str(" 💩"));
1172        assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
1173
1174        fn w(value: &[u8]) -> &Wtf8 {
1175            unsafe { transmute(value) }
1176        }
1177
1178        let mut string = Wtf8Buf::new();
1179        string.push_wtf8(w(b"\xED\xA0\xBD")); // lead
1180        string.push_wtf8(w(b"\xED\xB2\xA9")); // trail
1181        assert_eq!(string.bytes, b"\xF0\x9F\x92\xA9"); // Magic!
1182
1183        let mut string = Wtf8Buf::new();
1184        string.push_wtf8(w(b"\xED\xA0\xBD")); // lead
1185        string.push_wtf8(w(b" ")); // not surrogate
1186        string.push_wtf8(w(b"\xED\xB2\xA9")); // trail
1187        assert_eq!(string.bytes, b"\xED\xA0\xBD \xED\xB2\xA9");
1188
1189        let mut string = Wtf8Buf::new();
1190        string.push_wtf8(w(b"\xED\xA0\x80")); // lead
1191        string.push_wtf8(w(b"\xED\xAF\xBF")); // lead
1192        assert_eq!(string.bytes, b"\xED\xA0\x80\xED\xAF\xBF");
1193
1194        let mut string = Wtf8Buf::new();
1195        string.push_wtf8(w(b"\xED\xA0\x80")); // lead
1196        string.push_wtf8(w(b"\xEE\x80\x80")); // not surrogate
1197        assert_eq!(string.bytes, b"\xED\xA0\x80\xEE\x80\x80");
1198
1199        let mut string = Wtf8Buf::new();
1200        string.push_wtf8(w(b"\xED\x9F\xBF")); // not surrogate
1201        string.push_wtf8(w(b"\xED\xB0\x80")); // trail
1202        assert_eq!(string.bytes, b"\xED\x9F\xBF\xED\xB0\x80");
1203
1204        let mut string = Wtf8Buf::new();
1205        string.push_wtf8(w(b"a")); // not surrogate, < 3 bytes
1206        string.push_wtf8(w(b"\xED\xB0\x80")); // trail
1207        assert_eq!(string.bytes, b"\x61\xED\xB0\x80");
1208
1209        let mut string = Wtf8Buf::new();
1210        string.push_wtf8(w(b"\xED\xB0\x80")); // trail
1211        assert_eq!(string.bytes, b"\xED\xB0\x80");
1212    }
1213
1214    #[test]
1215    fn wtf8buf_truncate() {
1216        let mut string = Wtf8Buf::from_str("aé");
1217        string.truncate(1);
1218        assert_eq!(string.bytes, b"a");
1219    }
1220
1221    #[test]
1222    #[should_panic]
1223    fn wtf8buf_truncate_fail_code_point_boundary() {
1224        let mut string = Wtf8Buf::from_str("aé");
1225        string.truncate(2);
1226    }
1227
1228    #[test]
1229    #[should_panic]
1230    fn wtf8buf_truncate_fail_longer() {
1231        let mut string = Wtf8Buf::from_str("aé");
1232        string.truncate(4);
1233    }
1234
1235    #[test]
1236    fn wtf8buf_into_string() {
1237        let mut string = Wtf8Buf::from_str("aé 💩");
1238        assert_eq!(string.clone().into_string(), Ok(String::from("aé 💩")));
1239        string.push(CodePoint::from_u32(0xd800).unwrap());
1240        assert_eq!(string.clone().into_string(), Err(string));
1241    }
1242
1243    #[test]
1244    fn wtf8buf_into_string_lossy() {
1245        let mut string = Wtf8Buf::from_str("aé 💩");
1246        assert_eq!(string.clone().into_string_lossy(), String::from("aé 💩"));
1247        string.push(CodePoint::from_u32(0xd800).unwrap());
1248        assert_eq!(string.clone().into_string_lossy(), String::from("aé 💩�"));
1249    }
1250
1251    #[test]
1252    fn wtf8buf_from_iterator() {
1253        fn f(values: &[u32]) -> Wtf8Buf {
1254            values
1255                .iter()
1256                .map(|&c| CodePoint::from_u32(c).unwrap())
1257                .collect::<Wtf8Buf>()
1258        }
1259        assert_eq!(
1260            f(&[0x61, 0xe9, 0x20, 0x1f4a9]).bytes,
1261            b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1262        );
1263
1264        assert_eq!(f(&[0xd83d, 0xdca9]).bytes, b"\xF0\x9F\x92\xA9"); // Magic!
1265        assert_eq!(
1266            f(&[0xd83d, 0x20, 0xdca9]).bytes,
1267            b"\xED\xA0\xBD \xED\xB2\xA9"
1268        );
1269        assert_eq!(f(&[0xd800, 0xdbff]).bytes, b"\xED\xA0\x80\xED\xAF\xBF");
1270        assert_eq!(f(&[0xd800, 0xe000]).bytes, b"\xED\xA0\x80\xEE\x80\x80");
1271        assert_eq!(f(&[0xd7ff, 0xdc00]).bytes, b"\xED\x9F\xBF\xED\xB0\x80");
1272        assert_eq!(f(&[0x61, 0xdc00]).bytes, b"\x61\xED\xB0\x80");
1273        assert_eq!(f(&[0xdc00]).bytes, b"\xED\xB0\x80");
1274    }
1275
1276    #[test]
1277    fn wtf8buf_extend() {
1278        fn e(initial: &[u32], extended: &[u32]) -> Wtf8Buf {
1279            fn c(value: &u32) -> CodePoint {
1280                CodePoint::from_u32(*value).unwrap()
1281            }
1282            let mut string = initial.iter().map(c).collect::<Wtf8Buf>();
1283            string.extend(extended.iter().map(c));
1284            string
1285        }
1286
1287        assert_eq!(
1288            e(&[0x61, 0xe9], &[0x20, 0x1f4a9]).bytes,
1289            b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1290        );
1291
1292        assert_eq!(e(&[0xd83d], &[0xdca9]).bytes, b"\xF0\x9F\x92\xA9"); // Magic!
1293        assert_eq!(
1294            e(&[0xd83d, 0x20], &[0xdca9]).bytes,
1295            b"\xED\xA0\xBD \xED\xB2\xA9"
1296        );
1297        assert_eq!(e(&[0xd800], &[0xdbff]).bytes, b"\xED\xA0\x80\xED\xAF\xBF");
1298        assert_eq!(e(&[0xd800], &[0xe000]).bytes, b"\xED\xA0\x80\xEE\x80\x80");
1299        assert_eq!(e(&[0xd7ff], &[0xdc00]).bytes, b"\xED\x9F\xBF\xED\xB0\x80");
1300        assert_eq!(e(&[0x61], &[0xdc00]).bytes, b"\x61\xED\xB0\x80");
1301        assert_eq!(e(&[], &[0xdc00]).bytes, b"\xED\xB0\x80");
1302    }
1303
1304    #[test]
1305    fn wtf8buf_debug() {
1306        let mut string = Wtf8Buf::from_str("aé 💩");
1307        string.push(CodePoint::from_u32(0xd800).unwrap());
1308        assert_eq!(format!("{string:?}"), r#""aé 💩\u{D800}""#);
1309    }
1310
1311    #[test]
1312    fn wtf8buf_as_slice() {
1313        assert_eq!(Wtf8Buf::from_str("aé"), Wtf8::from_str("aé"));
1314    }
1315
1316    #[test]
1317    fn wtf8_debug() {
1318        let mut string = Wtf8Buf::from_str("aé 💩");
1319        string.push(CodePoint::from_u32(0xd800).unwrap());
1320        let string_ref = &*string;
1321        assert_eq!(format!("{string_ref:?}"), r#""aé 💩\u{D800}""#);
1322    }
1323
1324    #[test]
1325    fn wtf8_from_str() {
1326        assert_eq!(&Wtf8::from_str("").bytes, b"");
1327        assert_eq!(
1328            &Wtf8::from_str("aé 💩").bytes,
1329            b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1330        );
1331    }
1332
1333    #[test]
1334    fn wtf8_as_bytes() {
1335        assert_eq!(Wtf8::from_str("").as_bytes(), b"");
1336        assert_eq!(
1337            Wtf8::from_str("aé 💩").as_bytes(),
1338            b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1339        );
1340    }
1341
1342    #[test]
1343    fn wtf8_from_bytes_unchecked() {
1344        assert_eq!(unsafe { &Wtf8::from_bytes_unchecked(b"").bytes }, b"");
1345        assert_eq!(
1346            unsafe { &Wtf8::from_bytes_unchecked(b"a\xC3\xA9 \xF0\x9F\x92\xA9").bytes },
1347            b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1348        );
1349        assert_eq!(
1350            unsafe { Wtf8::from_bytes_unchecked(b"a\xC3\xA9 \xF0\x9F\x92\xA9") },
1351            Wtf8::from_str("aé 💩")
1352        )
1353    }
1354
1355    #[test]
1356    fn wtf8_cow() {
1357        let s: Cow<Wtf8> = Cow::from(Wtf8::from_str("aé 💩"));
1358        assert!(matches!(s, Cow::Borrowed(_)));
1359        let owned: Wtf8Buf = s.into_owned();
1360        assert_eq!(owned, Wtf8Buf::from_str("aé 💩"));
1361    }
1362
1363    #[test]
1364    fn wtf8_len() {
1365        assert_eq!(Wtf8::from_str("").len(), 0);
1366        assert_eq!(Wtf8::from_str("aé 💩").len(), 8);
1367    }
1368
1369    #[test]
1370    fn wtf8_slice() {
1371        assert_eq!(&Wtf8::from_str("aé 💩").slice(1, 4).bytes, b"\xC3\xA9 ");
1372    }
1373
1374    #[test]
1375    #[should_panic]
1376    fn wtf8_slice_not_code_point_boundary() {
1377        Wtf8::from_str("aé 💩").slice(2, 4);
1378    }
1379
1380    #[test]
1381    fn wtf8_slice_from() {
1382        assert_eq!(
1383            &Wtf8::from_str("aé 💩").slice_from(1).bytes,
1384            b"\xC3\xA9 \xF0\x9F\x92\xA9"
1385        );
1386    }
1387
1388    #[test]
1389    #[should_panic]
1390    fn wtf8_slice_from_not_code_point_boundary() {
1391        Wtf8::from_str("aé 💩").slice_from(2);
1392    }
1393
1394    #[test]
1395    fn wtf8_slice_to() {
1396        assert_eq!(&Wtf8::from_str("aé 💩").slice_to(4).bytes, b"a\xC3\xA9 ");
1397    }
1398
1399    #[test]
1400    #[should_panic]
1401    fn wtf8_slice_to_not_code_point_boundary() {
1402        Wtf8::from_str("aé 💩").slice_from(5);
1403    }
1404
1405    #[test]
1406    fn wtf8_ascii_byte_at() {
1407        let slice = Wtf8::from_str("aé 💩");
1408        assert_eq!(slice.ascii_byte_at(0), b'a');
1409        assert_eq!(slice.ascii_byte_at(1), b'\xFF');
1410        assert_eq!(slice.ascii_byte_at(2), b'\xFF');
1411        assert_eq!(slice.ascii_byte_at(3), b' ');
1412        assert_eq!(slice.ascii_byte_at(4), b'\xFF');
1413    }
1414
1415    #[test]
1416    fn wtf8_code_points() {
1417        fn c(value: u32) -> CodePoint {
1418            CodePoint::from_u32(value).unwrap()
1419        }
1420        fn cp(string: &Wtf8Buf) -> Vec<Option<char>> {
1421            string
1422                .code_points()
1423                .map(|c| c.to_char())
1424                .collect::<Vec<_>>()
1425        }
1426        let mut string = Wtf8Buf::from_str("é ");
1427        assert_eq!(cp(&string), vec![Some('é'), Some(' ')]);
1428        string.push(c(0xd83d));
1429        assert_eq!(cp(&string), vec![Some('é'), Some(' '), None]);
1430        string.push(c(0xdca9));
1431        assert_eq!(cp(&string), vec![Some('é'), Some(' '), Some('💩')]);
1432    }
1433
1434    #[test]
1435    fn wtf8_as_str() {
1436        assert_eq!(Wtf8::from_str("").as_str(), Some(""));
1437        assert_eq!(Wtf8::from_str("aé 💩").as_str(), Some("aé 💩"));
1438        let mut string = Wtf8Buf::new();
1439        string.push(CodePoint::from_u32(0xd800).unwrap());
1440        assert_eq!(string.as_str(), None);
1441    }
1442
1443    #[test]
1444    fn wtf8_to_string_lossy() {
1445        assert_eq!(Wtf8::from_str("").to_string_lossy(), Cow::Borrowed(""));
1446        assert_eq!(
1447            Wtf8::from_str("aé 💩").to_string_lossy(),
1448            Cow::Borrowed("aé 💩")
1449        );
1450        let mut string = Wtf8Buf::from_str("aé 💩");
1451        string.push(CodePoint::from_u32(0xd800).unwrap());
1452        assert_eq!(string.to_string_lossy(), {
1453            let o: Cow<str> = Cow::Owned(String::from("aé 💩�"));
1454            o
1455        });
1456    }
1457
1458    #[test]
1459    fn wtf8_to_ill_formed_utf16() {
1460        let mut string = Wtf8Buf::from_str("aé ");
1461        string.push(CodePoint::from_u32(0xd83d).unwrap());
1462        string.push_char('💩');
1463        assert_eq!(
1464            string.to_ill_formed_utf16().collect::<Vec<_>>(),
1465            vec![0x61, 0xe9, 0x20, 0xd83d, 0xd83d, 0xdca9]
1466        );
1467    }
1468
1469    #[test]
1470    fn wtf8buf_wtf8_from_bytes_valid() {
1471        // Valid UTF-8
1472        assert!(Wtf8Buf::from_bytes(b"hello".to_vec()).is_ok());
1473        assert!(Wtf8Buf::from_bytes(b"a\xC3\xA9 \xF0\x9F\x92\xA9".to_vec()).is_ok());
1474        assert!(Wtf8::from_bytes(b"hello").is_ok());
1475        assert!(Wtf8::from_bytes(b"a\xC3\xA9 \xF0\x9F\x92\xA9").is_ok());
1476
1477        // Valid WTF-8 with unpaired surrogates
1478        assert!(Wtf8Buf::from_bytes(b"\xED\xA0\x80".to_vec()).is_ok()); // lead surrogate
1479        assert!(Wtf8Buf::from_bytes(b"\xED\xB0\x80".to_vec()).is_ok()); // trail surrogate
1480        assert!(Wtf8Buf::from_bytes(b"a\xED\xA0\xBD".to_vec()).is_ok()); // text + lead
1481        assert!(Wtf8Buf::from_bytes(b"\xED\xB2\xA9z".to_vec()).is_ok()); // trail + text
1482        assert!(Wtf8Buf::from_bytes(b"\xED\xB2\xA9\xED\xA0\xBD".to_vec()).is_ok());
1483        // trail + lead
1484    }
1485
1486    #[test]
1487    fn wtf8buf_wtf8_from_bytes_invalid() {
1488        // Invalid: surrogate pair encoded as two 3-byte sequences
1489        assert!(Wtf8Buf::from_bytes(b"\xED\xA0\x80\xED\xB0\x80".to_vec()).is_err());
1490        assert!(Wtf8Buf::from_bytes(b"\xED\xA0\xBD\xED\xB2\xA9".to_vec()).is_err());
1491        assert!(Wtf8::from_bytes(b"\xED\xA0\x80\xED\xB0\x80").is_err());
1492        assert!(Wtf8::from_bytes(b"\xED\xA0\xBD\xED\xB2\xA9").is_err());
1493
1494        // Invalid UTF-8
1495        assert!(Wtf8Buf::from_bytes(vec![0xff]).is_err());
1496        assert!(Wtf8Buf::from_bytes(vec![0xc0, 0x80]).is_err()); // overlong
1497        assert!(Wtf8Buf::from_bytes(vec![0xed, 0xa0]).is_err()); // truncated lead surrogate
1498        assert!(Wtf8Buf::from_bytes(vec![0xf4, 0x90, 0x80, 0x80]).is_err()); // > U+10FFFF
1499
1500        // Verify we can recover the original bytes on failure
1501        let original = vec![0xff, 0xfe];
1502        let result = Wtf8Buf::from_bytes(original.clone());
1503        assert_eq!(result.unwrap_err(), original);
1504
1505        let result = Wtf8::from_bytes(&original);
1506        assert_eq!(result.unwrap_err(), original);
1507    }
1508}