1#![unstable(
3    feature = "wtf8_internals",
4    issue = "none",
5    reason = "this is internal code for representing OsStr on some platforms and not a public API"
6)]
7#![doc(hidden)]
10
11#[cfg(test)]
15mod tests;
16
17use core::char::{MAX_LEN_UTF8, encode_utf8_raw};
18use core::hash::{Hash, Hasher};
19pub use core::wtf8::{CodePoint, Wtf8};
20#[cfg(not(test))]
21pub use core::wtf8::{EncodeWide, Wtf8CodePoints};
22use core::{fmt, mem, ops, str};
23
24use crate::borrow::{Cow, ToOwned};
25use crate::boxed::Box;
26use crate::collections::TryReserveError;
27#[cfg(not(test))]
28use crate::rc::Rc;
29use crate::string::String;
30#[cfg(all(not(test), target_has_atomic = "ptr"))]
31use crate::sync::Arc;
32use crate::vec::Vec;
33
34#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
39#[doc(hidden)]
40pub struct Wtf8Buf {
41    bytes: Vec<u8>,
42
43    is_known_utf8: bool,
50}
51
52impl ops::Deref for Wtf8Buf {
53    type Target = Wtf8;
54
55    fn deref(&self) -> &Wtf8 {
56        self.as_slice()
57    }
58}
59
60impl ops::DerefMut for Wtf8Buf {
61    fn deref_mut(&mut self) -> &mut Wtf8 {
62        self.as_mut_slice()
63    }
64}
65
66impl fmt::Debug for Wtf8Buf {
73    #[inline]
74    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
75        fmt::Debug::fmt(&**self, formatter)
76    }
77}
78
79impl fmt::Display for Wtf8Buf {
82    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
83        if let Some(s) = self.as_known_utf8() {
84            fmt::Display::fmt(s, formatter)
85        } else {
86            fmt::Display::fmt(&**self, formatter)
87        }
88    }
89}
90
91#[cfg_attr(test, allow(dead_code))]
92impl Wtf8Buf {
93    #[inline]
95    pub fn new() -> Wtf8Buf {
96        Wtf8Buf { bytes: Vec::new(), is_known_utf8: true }
97    }
98
99    #[inline]
101    pub fn with_capacity(capacity: usize) -> Wtf8Buf {
102        Wtf8Buf { bytes: Vec::with_capacity(capacity), is_known_utf8: true }
103    }
104
105    #[inline]
110    pub unsafe fn from_bytes_unchecked(value: Vec<u8>) -> Wtf8Buf {
111        Wtf8Buf { bytes: value, is_known_utf8: false }
112    }
113
114    #[inline]
120    pub const fn from_string(string: String) -> Wtf8Buf {
121        Wtf8Buf { bytes: string.into_bytes(), is_known_utf8: true }
122    }
123
124    #[inline]
130    pub fn from_str(s: &str) -> Wtf8Buf {
131        Wtf8Buf { bytes: s.as_bytes().to_vec(), is_known_utf8: true }
132    }
133
134    pub fn clear(&mut self) {
135        self.bytes.clear();
136        self.is_known_utf8 = true;
137    }
138
139    pub fn from_wide(v: &[u16]) -> Wtf8Buf {
144        let mut string = Wtf8Buf::with_capacity(v.len());
145        for item in char::decode_utf16(v.iter().cloned()) {
146            match item {
147                Ok(ch) => string.push_char(ch),
148                Err(surrogate) => {
149                    let surrogate = surrogate.unpaired_surrogate();
150                    let code_point = unsafe { CodePoint::from_u32_unchecked(surrogate as u32) };
152                    string.is_known_utf8 = false;
154                    unsafe {
157                        string.push_code_point_unchecked(code_point);
158                    }
159                }
160            }
161        }
162        string
163    }
164
165    unsafe fn push_code_point_unchecked(&mut self, code_point: CodePoint) {
169        let mut bytes = [0; MAX_LEN_UTF8];
170        let bytes = encode_utf8_raw(code_point.to_u32(), &mut bytes);
171        self.bytes.extend_from_slice(bytes)
172    }
173
174    #[inline]
175    pub fn as_slice(&self) -> &Wtf8 {
176        unsafe { Wtf8::from_bytes_unchecked(&self.bytes) }
177    }
178
179    #[inline]
180    pub fn as_mut_slice(&mut self) -> &mut Wtf8 {
181        unsafe { Wtf8::from_mut_bytes_unchecked(&mut self.bytes) }
185    }
186
187    #[inline]
190    fn as_known_utf8(&self) -> Option<&str> {
191        if self.is_known_utf8 {
192            Some(unsafe { str::from_utf8_unchecked(self.as_bytes()) })
194        } else {
195            None
196        }
197    }
198
199    #[inline]
207    pub fn reserve(&mut self, additional: usize) {
208        self.bytes.reserve(additional)
209    }
210
211    #[inline]
223    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
224        self.bytes.try_reserve(additional)
225    }
226
227    #[inline]
228    pub fn reserve_exact(&mut self, additional: usize) {
229        self.bytes.reserve_exact(additional)
230    }
231
232    #[inline]
249    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
250        self.bytes.try_reserve_exact(additional)
251    }
252
253    #[inline]
254    pub fn shrink_to_fit(&mut self) {
255        self.bytes.shrink_to_fit()
256    }
257
258    #[inline]
259    pub fn shrink_to(&mut self, min_capacity: usize) {
260        self.bytes.shrink_to(min_capacity)
261    }
262
263    #[inline]
264    pub fn leak<'a>(self) -> &'a mut Wtf8 {
265        unsafe { Wtf8::from_mut_bytes_unchecked(self.bytes.leak()) }
266    }
267
268    #[inline]
270    pub fn capacity(&self) -> usize {
271        self.bytes.capacity()
272    }
273
274    #[inline]
276    pub fn push_str(&mut self, other: &str) {
277        self.bytes.extend_from_slice(other.as_bytes())
278    }
279
280    #[inline]
286    pub fn push_wtf8(&mut self, other: &Wtf8) {
287        match ((&*self).final_lead_surrogate(), other.initial_trail_surrogate()) {
288            (Some(lead), Some(trail)) => {
290                let len_without_lead_surrogate = self.len() - 3;
291                self.bytes.truncate(len_without_lead_surrogate);
292                let other_without_trail_surrogate = &other.as_bytes()[3..];
293                self.bytes.reserve(4 + other_without_trail_surrogate.len());
295                self.push_char(decode_surrogate_pair(lead, trail));
296                self.bytes.extend_from_slice(other_without_trail_surrogate);
297            }
298            _ => {
299                if self.is_known_utf8 && other.next_surrogate(0).is_some() {
302                    self.is_known_utf8 = false;
303                }
304
305                self.bytes.extend_from_slice(other.as_bytes());
306            }
307        }
308    }
309
310    #[inline]
312    pub fn push_char(&mut self, c: char) {
313        unsafe { self.push_code_point_unchecked(CodePoint::from_char(c)) }
315    }
316
317    #[inline]
323    pub fn push(&mut self, code_point: CodePoint) {
324        if let Some(trail) = code_point.to_trail_surrogate() {
325            if let Some(lead) = (&*self).final_lead_surrogate() {
326                let len_without_lead_surrogate = self.len() - 3;
327                self.bytes.truncate(len_without_lead_surrogate);
328                self.push_char(decode_surrogate_pair(lead, trail));
329                return;
330            }
331
332            self.is_known_utf8 = false;
334        } else if code_point.to_lead_surrogate().is_some() {
335            self.is_known_utf8 = false;
337        }
338
339        unsafe { self.push_code_point_unchecked(code_point) }
341    }
342
343    #[inline]
350    pub fn truncate(&mut self, new_len: usize) {
351        assert!(self.is_code_point_boundary(new_len));
352        self.bytes.truncate(new_len)
353    }
354
355    #[inline]
357    pub fn into_bytes(self) -> Vec<u8> {
358        self.bytes
359    }
360
361    pub fn into_string(self) -> Result<String, Wtf8Buf> {
369        if self.is_known_utf8 || self.next_surrogate(0).is_none() {
370            Ok(unsafe { String::from_utf8_unchecked(self.bytes) })
371        } else {
372            Err(self)
373        }
374    }
375
376    pub fn into_string_lossy(mut self) -> String {
382        if !self.is_known_utf8 {
383            let mut pos = 0;
384            while let Some((surrogate_pos, _)) = self.next_surrogate(pos) {
385                pos = surrogate_pos + 3;
386                self.bytes[surrogate_pos..pos].copy_from_slice("\u{FFFD}".as_bytes());
389            }
390        }
391        unsafe { String::from_utf8_unchecked(self.bytes) }
392    }
393
394    #[inline]
396    pub fn into_box(self) -> Box<Wtf8> {
397        unsafe { mem::transmute(self.bytes.into_boxed_slice()) }
399    }
400
401    pub fn from_box(boxed: Box<Wtf8>) -> Wtf8Buf {
403        let bytes: Box<[u8]> = unsafe { mem::transmute(boxed) };
404        Wtf8Buf { bytes: bytes.into_vec(), is_known_utf8: false }
405    }
406
407    #[inline]
411    pub unsafe fn extend_from_slice_unchecked(&mut self, other: &[u8]) {
412        self.bytes.extend_from_slice(other);
413        self.is_known_utf8 = false;
414    }
415}
416
417impl FromIterator<CodePoint> for Wtf8Buf {
422    fn from_iter<T: IntoIterator<Item = CodePoint>>(iter: T) -> Wtf8Buf {
423        let mut string = Wtf8Buf::new();
424        string.extend(iter);
425        string
426    }
427}
428
429impl Extend<CodePoint> for Wtf8Buf {
434    fn extend<T: IntoIterator<Item = CodePoint>>(&mut self, iter: T) {
435        let iterator = iter.into_iter();
436        let (low, _high) = iterator.size_hint();
437        self.bytes.reserve(low);
439        iterator.for_each(move |code_point| self.push(code_point));
440    }
441
442    #[inline]
443    fn extend_one(&mut self, code_point: CodePoint) {
444        self.push(code_point);
445    }
446
447    #[inline]
448    fn extend_reserve(&mut self, additional: usize) {
449        self.bytes.reserve(additional);
451    }
452}
453
454pub(super) fn to_owned(slice: &Wtf8) -> Wtf8Buf {
456    Wtf8Buf { bytes: slice.as_bytes().to_vec(), is_known_utf8: false }
457}
458
459pub(super) fn to_string_lossy(slice: &Wtf8) -> Cow<'_, str> {
466    let Some((surrogate_pos, _)) = slice.next_surrogate(0) else {
467        return Cow::Borrowed(unsafe { str::from_utf8_unchecked(slice.as_bytes()) });
468    };
469    let wtf8_bytes = slice.as_bytes();
470    let mut utf8_bytes = Vec::with_capacity(slice.len());
471    utf8_bytes.extend_from_slice(&wtf8_bytes[..surrogate_pos]);
472    utf8_bytes.extend_from_slice("\u{FFFD}".as_bytes());
473    let mut pos = surrogate_pos + 3;
474    loop {
475        match slice.next_surrogate(pos) {
476            Some((surrogate_pos, _)) => {
477                utf8_bytes.extend_from_slice(&wtf8_bytes[pos..surrogate_pos]);
478                utf8_bytes.extend_from_slice("\u{FFFD}".as_bytes());
479                pos = surrogate_pos + 3;
480            }
481            None => {
482                utf8_bytes.extend_from_slice(&wtf8_bytes[pos..]);
483                return Cow::Owned(unsafe { String::from_utf8_unchecked(utf8_bytes) });
484            }
485        }
486    }
487}
488
489#[inline]
490pub(super) fn clone_into(slice: &Wtf8, buf: &mut Wtf8Buf) {
491    buf.is_known_utf8 = false;
492    slice.as_bytes().clone_into(&mut buf.bytes);
493}
494
495#[cfg(not(test))]
496impl Wtf8 {
497    #[rustc_allow_incoherent_impl]
498    pub fn to_owned(&self) -> Wtf8Buf {
499        to_owned(self)
500    }
501
502    #[rustc_allow_incoherent_impl]
503    pub fn clone_into(&self, buf: &mut Wtf8Buf) {
504        clone_into(self, buf)
505    }
506
507    #[rustc_allow_incoherent_impl]
508    pub fn to_string_lossy(&self) -> Cow<'_, str> {
509        to_string_lossy(self)
510    }
511
512    #[rustc_allow_incoherent_impl]
513    pub fn into_box(&self) -> Box<Wtf8> {
514        let boxed: Box<[u8]> = self.as_bytes().into();
515        unsafe { mem::transmute(boxed) }
516    }
517
518    #[rustc_allow_incoherent_impl]
519    pub fn empty_box() -> Box<Wtf8> {
520        let boxed: Box<[u8]> = Default::default();
521        unsafe { mem::transmute(boxed) }
522    }
523
524    #[cfg(target_has_atomic = "ptr")]
525    #[rustc_allow_incoherent_impl]
526    pub fn into_arc(&self) -> Arc<Wtf8> {
527        let arc: Arc<[u8]> = Arc::from(self.as_bytes());
528        unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Wtf8) }
529    }
530
531    #[rustc_allow_incoherent_impl]
532    pub fn into_rc(&self) -> Rc<Wtf8> {
533        let rc: Rc<[u8]> = Rc::from(self.as_bytes());
534        unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Wtf8) }
535    }
536
537    #[inline]
538    #[rustc_allow_incoherent_impl]
539    pub fn to_ascii_lowercase(&self) -> Wtf8Buf {
540        Wtf8Buf { bytes: self.as_bytes().to_ascii_lowercase(), is_known_utf8: false }
541    }
542
543    #[inline]
544    #[rustc_allow_incoherent_impl]
545    pub fn to_ascii_uppercase(&self) -> Wtf8Buf {
546        Wtf8Buf { bytes: self.as_bytes().to_ascii_uppercase(), is_known_utf8: false }
547    }
548}
549
550#[inline]
551fn decode_surrogate_pair(lead: u16, trail: u16) -> char {
552    let code_point = 0x10000 + ((((lead - 0xD800) as u32) << 10) | (trail - 0xDC00) as u32);
553    unsafe { char::from_u32_unchecked(code_point) }
554}
555
556impl Hash for Wtf8Buf {
557    #[inline]
558    fn hash<H: Hasher>(&self, state: &mut H) {
559        state.write(&self.bytes);
560        0xfeu8.hash(state)
561    }
562}