<commit_before>/*!

Implementation of [the WTF-8 encoding](https://simonsapin.github.io/wtf-8/).

This library uses Rust’s type system to maintain
[well-formedness](https://simonsapin.github.io/wtf-8/#well-formed),
like the `String` and `&str` types do for UTF-8.

Since [WTF-8 must not be used
for interchange](https://simonsapin.github.io/wtf-8/#intended-audience),
this library deliberately does not provide access to the underlying bytes
of WTF-8 strings,
nor can it decode WTF-8 from arbitrary bytes.
WTF-8 strings can be obtained from UTF-8, UTF-16, or code points.

*/

#![feature(globs, default_type_params, phase, macro_rules)]

#![no_std]

#[phase(link, plugin)]
extern crate core;

extern crate collections;

#[cfg(test)]
#[phase(link, plugin)]
extern crate std;

#[cfg(test)]
extern crate "native" as rt;  // provides the `start` lang item, compensate for #![no_std]

use core::prelude::*;

use collections::hash::{Hash, Writer};
use collections::slice::{CloneSliceAllocPrelude};
use collections::str;
use collections::string;
use collections::string::String;
use collections::vec::Vec;
use core::fmt;
use core::mem::transmute;
use core::num::Saturating;
use core::slice;

// Compensate for #[no_std]
#[cfg(not(test))]
mod std {
    pub use core::fmt;      // necessary for write!()
    pub use core::option;   // deriving(PartialOrd)
    pub use core::clone;    // deriving(Clone)
    pub use core::cmp;      // deriving(Eq, Ord, etc.)
}

mod not_quite_std;


static UTF8_REPLACEMENT_CHARACTER: &'static [u8] = b"\xEF\xBF\xBD";

/// A Unicode code point: from U+0000 to U+10FFFF.
///
/// Compare with the `char` type,
/// which represents a Unicode scalar value:
/// a code point that is not a surrogate (U+D800 to U+DFFF).
#[deriving(Eq, PartialEq, Ord, PartialOrd, Clone)]
pub struct CodePoint {
    value: u32
}

/// Format the code point as `U+` followed by four to six hexadecimal digits.
/// Example: `U+1F4A9`
impl fmt::Show for CodePoint {
    #[inline]
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::FormatError> {
        write!(formatter, "U+{:04X}", self.value)
    }
}


impl CodePoint {
    /// Unsafely create a new `CodePoint` without checking the value.
    ///
    /// Only use when `value` is known to be less than or equal to 0x10FFFF.
    #[inline]
    pub unsafe fn from_u32_unchecked(value: u32) -> CodePoint {
        CodePoint { value: value }
    }

    /// Create a new `CodePoint` if the value is a valid code point.
    ///
    /// Return `None` if `value` is above 0x10FFFF.
    #[inline]
    pub fn from_u32(value: u32) -> Option<CodePoint> {
        match value {
            0 ... 0x10FFFF => Some(CodePoint { value: value }),
            _ => None
        }
    }

    /// Create a new `CodePoint` from a `char`.
    ///
    /// Since all Unicode scalar values are code points, this always succeds.
    #[inline]
    pub fn from_char(value: char) -> CodePoint {
        CodePoint { value: value as u32 }
    }

    /// Return the numeric value of the code point.
    #[inline]
    pub fn to_u32(&self) -> u32 {
        self.value
    }

    /// Optionally return a Unicode scalar value for the code point.
    ///
    /// Return `None` if the code point is a surrogate (from U+D800 to U+DFFF).
    #[inline]
    pub fn to_char(&self) -> Option<char> {
        match self.value {
            0xD800 ... 0xDFFF => None,
            _ => Some(unsafe { transmute(self.value) })
        }
    }

    /// Return a Unicode scalar value for the code point.
    ///
    /// Return `'\uFFFD'` (the replacement character “�”)
    /// if the code point is a surrogate (from U+D800 to U+DFFF).
    #[inline]
    pub fn to_char_lossy(&self) -> char {
        self.to_char().unwrap_or('\uFFFD')
    }
}


/// An owned, growable string of well-formed WTF-8 data.
///
/// Similar to `String`, but can additionally contain surrogate code points
/// if they’re not in a surrogate pair.
#[deriving(Eq, PartialEq, Ord, PartialOrd, Clone)]
pub struct Wtf8String {
    bytes: Vec<u8>
}

impl Deref<Wtf8Slice> for Wtf8String {
    fn deref(&self) -> &Wtf8Slice {
        self.as_slice()
    }
}

/// Format the string with double quotes,
/// and surrogates as `\u` followed by four hexadecimal digits.
/// Example: `"a\uD800"` for a string with code points [U+0061, U+D800]
impl fmt::Show for Wtf8String {
    #[inline]
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::FormatError> {
        self.as_slice().fmt(formatter)
    }
}


impl Wtf8String {
    /// Create an new, empty WTF-8 string.
    #[inline]
    pub fn new() -> Wtf8String {
        Wtf8String { bytes: Vec::new() }
    }

    /// Create an new, empty WTF-8 string with pre-allocated capacity for `n` bytes.
    #[inline]
    pub fn with_capacity(n: uint) -> Wtf8String {
        Wtf8String { bytes: Vec::with_capacity(n) }
    }

    /// Create a WTF-8 string from an UTF-8 `String`.
    ///
    /// This takes ownership of the `String` and does not copy.
    ///
    /// Since WTF-8 is a superset of UTF-8, this always succeeds.
    #[inline]
    pub fn from_string(string: String) -> Wtf8String {
        Wtf8String { bytes: string.into_bytes() }
    }

    /// Create a WTF-8 string from an UTF-8 `&str` slice.
    ///
    /// This copies the content of the slice.
    ///
    /// Since WTF-8 is a superset of UTF-8, this always succeeds.
    #[inline]
    pub fn from_str(str: &str) -> Wtf8String {
        Wtf8String { bytes: str.as_bytes().to_vec() }
    }

    /// Create a WTF-8 string from a potentially ill-formed UTF-16 slice of 16-bit code units.
    ///
    /// This is lossless: calling `.to_ill_formed_utf16()` on the resulting string
    /// will always return the original code units.
    pub fn from_ill_formed_utf16(v: &[u16]) -> Wtf8String {
        let mut string = Wtf8String::with_capacity(v.len());
        for item in str::utf16_items(v) {
            match item {
                str::ScalarValue(c) => string.push_char(c),
                str::LoneSurrogate(s) => {
                    // Surrogates are known to be in the code point range.
                    let code_point = unsafe { CodePoint::from_u32_unchecked(s as u32) };
                    // Skip the WTF-8 concatenation check,
                    // surrogate pairs are already decoded by str::utf16_items
                    not_quite_std::push_code_point(&mut string, code_point)
                }
            }
        }
        string
    }

    #[inline]
    pub fn as_slice(&self) -> &Wtf8Slice {
        unsafe { transmute(self.bytes.as_slice()) }
    }

    /// Reserves capacity for at least `additional` more bytes to be inserted
    /// in the given `Wtf8String`.
    /// The collection may reserve more space to avoid frequent reallocations.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity overflows `uint`.
    ///
    /// # Example
    ///
    /// ```
    /// let mut s = Wtf8String::new();
    /// s.reserve(10);
    /// assert!(s.capacity() >= 10);
    /// ```
    #[inline]
    pub fn reserve(&mut self, additional: uint) {
        self.bytes.reserve(additional)
    }

    /// Returns the number of bytes that this string buffer can hold without reallocating.
    ///
    /// # Example
    ///
    /// ```
    /// let s = Wtf8String::with_capacity(10);
    /// assert!(s.capacity() >= 10);
    /// ```
    #[inline]
    pub fn capacity(&self) -> uint {
        self.bytes.capacity()
    }

    /// Append an UTF-8 slice at the end of the string.
    #[inline]
    pub fn push_str(&mut self, other: &str) {
        self.bytes.push_all(other.as_bytes())
    }

    /// Append a WTF-8 slice at the end of the string.
    ///
    /// This replaces newly paired surrogates at the boundary
    /// with a supplementary code point,
    /// like concatenating ill-formed UTF-16 strings effectively would.
    #[inline]
    pub fn push_wtf8(&mut self, other: &Wtf8Slice) {
        match ((&*self).final_lead_surrogate(), other.initial_trail_surrogate()) {
            // Replace newly paired surrogates by a supplementary code point.
            (Some(lead), Some(trail)) => {
                let len_without_lead_surrogate = self.len() - 3;
                self.bytes.truncate(len_without_lead_surrogate);
                let other_without_trail_surrogate = other.bytes.slice_from(3);
                // 4 bytes for the supplementary code point
                self.bytes.reserve(4 + other_without_trail_surrogate.len());
                self.push_char(decode_surrogate_pair(lead, trail));
                self.bytes.push_all(other_without_trail_surrogate);
            }
            _ => self.bytes.push_all(&other.bytes)
        }
    }

    /// Append a Unicode scalar value at the end of the string.
    #[inline]
    pub fn push_char(&mut self, c: char) {
        not_quite_std::push_code_point(self, CodePoint::from_char(c))
    }

    /// Append a code point at the end of the string.
    ///
    /// This replaces newly paired surrogates at the boundary
    /// with a supplementary code point,
    /// like concatenating ill-formed UTF-16 strings effectively would.
    #[inline]
    pub fn push(&mut self, code_point: CodePoint) {
        match code_point.to_u32() {
            trail @ 0xDC00...0xDFFF => {
                match (&*self).final_lead_surrogate() {
                    Some(lead) => {
                        let len_without_lead_surrogate = self.len() - 3;
                        self.bytes.truncate(len_without_lead_surrogate);
                        self.push_char(decode_surrogate_pair(lead, trail as u16));
                        return
                    }
                    _ => {}
                }
            }
            _ => {}
        }

        // No newly paired surrogates at the boundary.
        not_quite_std::push_code_point(self, code_point)
    }

    /// Shortens a string to the specified length.
    ///
    /// # Failure
    ///
    /// Fails if `new_len` > current length,
    /// or if `new_len` is not a code point boundary.
    #[inline]
    pub fn truncate(&mut self, new_len: uint) {
        assert!(not_quite_std::is_code_point_boundary(self.as_slice(), new_len));
        self.bytes.truncate(new_len)
    }

    /// Consume the WTF-8 string and try to convert it to UTF-8.
    ///
    /// This does not copy the data.
    ///
    /// If the contents are not well-formed UTF-8
    /// (that is, if the string contains surrogates),
    /// the original WTF-8 string is returned instead.
    pub fn into_string(self) -> Result<String, Wtf8String> {
        match self.next_surrogate(0) {
            None => Ok(unsafe { string::raw::from_utf8(self.bytes) }),
            Some(_) => Err(self),
        }
    }

    /// Consume the WTF-8 string and convert it lossily to UTF-8.
    ///
    /// This does not copy the data (but may overwrite parts of it in place).
    ///
    /// Surrogates are replaced with `"\uFFFD"` (the replacement character “�”)
    pub fn into_string_lossy(mut self) -> String {
        let mut pos = 0;
        loop {
            match self.next_surrogate(pos) {
                Some((surrogate_pos, _)) => {
                    pos = surrogate_pos + 3;
                    slice::bytes::copy_memory(
                        self.bytes.slice_mut(surrogate_pos, pos),
                        UTF8_REPLACEMENT_CHARACTER
                    );
                },
                None => return unsafe { string::raw::from_utf8(self.bytes) }
            }
        }
    }
}


/// Create a new WTF-8 string from an iterator of code points.
///
/// This replaces surrogate code point pairs with supplementary code points,
/// like concatenating ill-formed UTF-16 strings effectively would.
impl FromIterator<CodePoint> for Wtf8String {
    fn from_iter<T: Iterator<CodePoint>>(iterator: T) -> Wtf8String {
        let mut string = Wtf8String::new();
        string.extend(iterator);
        string
    }
}


/// Append code points from an iterator to the string.
///
/// This replaces surrogate code point pairs with supplementary code points,
/// like concatenating ill-formed UTF-16 strings effectively would.
impl Extend<CodePoint> for Wtf8String {
    fn extend<T: Iterator<CodePoint>>(&mut self, mut iterator: T) {
        let (low, _high) = iterator.size_hint();
        // Lower bound of one byte per code point (ASCII only)
        self.bytes.reserve(low);
        for code_point in iterator {
            self.push(code_point);
        }
    }
}

/// A borrowed slice of well-formed WTF-8 data.
///
/// Similar to `&str`, but can additionally contain surrogate code points
/// if they’re not in a surrogate pair.
pub struct Wtf8Slice {
    bytes: [u8]
}

// FIXME: https://github.com/rust-lang/rust/issues/18805
impl PartialEq for Wtf8Slice {
    fn eq(&self, other: &Wtf8Slice) -> bool { self.bytes.eq(&other.bytes) }
}

// FIXME: https://github.com/rust-lang/rust/issues/18805
impl Eq for Wtf8Slice {}

// FIXME: https://github.com/rust-lang/rust/issues/18738
impl PartialOrd for Wtf8Slice {
    #[inline]
    fn partial_cmp(&self, other: &Wtf8Slice) -> Option<Ordering> {
        self.bytes.partial_cmp(&other.bytes)
    }
    #[inline]
    fn lt(&self, other: &Wtf8Slice) -> bool { self.bytes.lt(&other.bytes) }
    #[inline]
    fn le(&self, other: &Wtf8Slice) -> bool { self.bytes.le(&other.bytes) }
    #[inline]
    fn gt(&self, other: &Wtf8Slice) -> bool { self.bytes.gt(&other.bytes) }
    #[inline]
    fn ge(&self, other: &Wtf8Slice) -> bool { self.bytes.ge(&other.bytes) }
}

// FIXME: https://github.com/rust-lang/rust/issues/18738
impl Ord for Wtf8Slice {
    #[inline]
    fn cmp(&self, other: &Wtf8Slice) -> Ordering { self.bytes.cmp(&other.bytes) }
}


/// Format the slice with double quotes,
/// and surrogates as `\u` followed by four hexadecimal digits.
/// Example: `"a\uD800"` for a slice with code points [U+0061, U+D800]
impl fmt::Show for Wtf8Slice {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::FormatError> {
        try!(formatter.write(b"\""))
        let mut pos = 0;
        loop {
            match self.next_surrogate(pos) {
                None => break,
                Some((surrogate_pos, surrogate)) => {
                    try!(formatter.write(self.bytes.slice(pos, surrogate_pos)));
                    try!(write!(formatter, "\\u{:X}", surrogate));
                    pos = surrogate_pos + 3;
                }
            }
        }
        try!(formatter.write(self.bytes.slice_from(pos)));
        formatter.write(b"\"")
    }
}


impl Wtf8Slice {
    /// Create a WTF-8 slice from a UTF-8 `&str` slice.
    ///
    /// Since WTF-8 is a superset of UTF-8, this always succeeds.
    #[inline]
    pub fn from_str(value: &str) -> &Wtf8Slice {
        unsafe { transmute(value.as_bytes()) }
    }

    /// Return the length, in WTF-8 bytes.
    #[inline]
    pub fn len(&self) -> uint {
        self.bytes.len()
    }

    /// Return a slice of the given string for the byte range [`begin`..`end`).
    ///
    /// # Failure
    ///
    /// Fails when `begin` and `end` do not point to code point boundaries,
    /// or point beyond the end of the string.
    #[inline]
    pub fn slice(&self, begin: uint, end: uint) -> &Wtf8Slice {
        // is_code_point_boundary checks that the index is in [0, .len()]
        if begin <= end &&
           not_quite_std::is_code_point_boundary(self, begin) &&
           not_quite_std::is_code_point_boundary(self, end) {
            unsafe { not_quite_std::slice_unchecked(self, begin, end) }
        } else {
            not_quite_std::slice_error_fail(self, begin, end)
        }
    }

    /// Return a slice of the given string from byte `begin` to its end.
    ///
    /// # Failure
    ///
    /// Fails when `begin` is not at a code point boundary,
    /// or is beyond the end of the string.
    #[inline]
    pub fn slice_from(&self, begin: uint) -> &Wtf8Slice {
        // is_code_point_boundary checks that the index is in [0, .len()]
        if not_quite_std::is_code_point_boundary(self, begin) {
            unsafe { not_quite_std::slice_unchecked(self, begin, self.len()) }
        } else {
            not_quite_std::slice_error_fail(self, begin, self.len())
        }
    }

    /// Return a slice of the given string from its beginning to byte `end`.
    ///
    /// # Failure
    ///
    /// Fails when `end` is not at a code point boundary,
    /// or is beyond the end of the string.
    #[inline]
    pub fn slice_to(&self, end: uint) -> &Wtf8Slice {
        // is_code_point_boundary checks that the index is in [0, .len()]
        if not_quite_std::is_code_point_boundary(self, end) {
            unsafe { not_quite_std::slice_unchecked(self, 0, end) }
        } else {
            not_quite_std::slice_error_fail(self, 0, end)
        }
    }

    /// Return the code point at `position` if it is in the ASCII range,
    /// or `b'\xFF' otherwise.
    ///
    /// # Failure
    ///
    /// Fails if `position` is beyond the end of the string.
    #[inline]
    pub fn ascii_byte_at(&self, position: uint) -> u8 {
        match self.bytes[position] {
            ascii_byte @ 0x00 ... 0x7F => ascii_byte,
            _ => 0xFF
        }
    }

    /// Return the code point at `position`.
    ///
    /// # Failure
    ///
    /// Fails if `position` is not at a code point boundary,
    /// or is beyond the end of the string.
    #[inline]
    pub fn code_point_at(&self, position: uint) -> CodePoint {
        let (code_point, _) = self.code_point_range_at(position);
        code_point
    }

    /// Return the code point at `position`
    /// and the position of the next code point.
    ///
    /// # Failure
    ///
    /// Fails if `position` is not at a code point boundary,
    /// or is beyond the end of the string.
    #[inline]
    pub fn code_point_range_at(&self, position: uint) -> (CodePoint, uint) {
        not_quite_std::code_point_range_at(self, position)
    }

    /// Return an iterator for the string’s code points.
    #[inline]
    pub fn code_points(&self) -> Wtf8CodePoints {
        Wtf8CodePoints { bytes: self.bytes.iter() }
    }

    /// Try to convert the string to UTF-8 and return a `&str` slice.
    ///
    /// Return `None` if the string contains surrogates.
    ///
    /// This does not copy the data.
    #[inline]
    pub fn as_str(&self) -> Option<&str> {
        // Well-formed WTF-8 is also well-formed UTF-8
        // if and only if it contains no surrogate.
        match self.next_surrogate(0) {
            None => Some(unsafe { str::raw::from_utf8(&self.bytes) }),
            Some(_) => None,
        }
    }

    /// Lossily convert the string to UTF-8.
    /// Return an UTF-8 `&str` slice if the contents are well-formed in UTF-8.
    ///
    /// Surrogates are replaced with `"\uFFFD"` (the replacement character “�”).
    ///
    /// This only copies the data if necessary (if it contains any surrogate).
    pub fn to_string_lossy(&self) -> str::MaybeOwned {
        let surrogate_pos = match self.next_surrogate(0) {
            None => return str::Slice(unsafe { str::raw::from_utf8(&self.bytes) }),
            Some((pos, _)) => pos,
        };
        let wtf8_bytes = &self.bytes;
        let mut utf8_bytes = Vec::with_capacity(self.len());
        utf8_bytes.push_all(wtf8_bytes.slice_to(surrogate_pos));
        utf8_bytes.push_all(UTF8_REPLACEMENT_CHARACTER);
        let mut pos = surrogate_pos + 3;
        loop {
            match self.next_surrogate(pos) {
                Some((surrogate_pos, _)) => {
                    utf8_bytes.push_all(wtf8_bytes.slice(pos, surrogate_pos));
                    utf8_bytes.push_all(UTF8_REPLACEMENT_CHARACTER);
                    pos = surrogate_pos + 3;
                },
                None => {
                    utf8_bytes.push_all(wtf8_bytes.slice_from(pos));
                    return str::Owned(unsafe { string::raw::from_utf8(utf8_bytes) })
                }
            }
        }
    }

    /// Convert the WTF-8 string to potentially ill-formed UTF-16
    /// and return an iterator of 16-bit code units.
    ///
    /// This is lossless:
    /// calling `Wtf8String::from_ill_formed_utf16` on the resulting code units
    /// would always return the original WTF-8 string.
    #[inline]
    pub fn to_ill_formed_utf16(&self) -> IllFormedUtf16CodeUnits {
        IllFormedUtf16CodeUnits { code_points: self.code_points(), extra: 0 }
    }

    #[inline]
    fn next_surrogate(&self, mut pos: uint) -> Option<(uint, u16)> {
        let mut iter = self.bytes.slice_from(pos).iter();
        loop {
            let b = match iter.next() {
                None => return None,
                Some(&b) => b,
            };
            if b < 0x80 {
                pos += 1;
            } else if b < 0xE0 {
                iter.next();
                pos += 2;
            } else if b == 0xED {
                match (iter.next(), iter.next()) {
                    (Some(&b2), Some(&b3)) if b2 >= 0xA0 => {
                        return Some((pos, decode_surrogate(b2, b3)))
                    }
                    _ => pos += 3
                }
            } else if b < 0xF0 {
                iter.next();
                iter.next();
                pos += 3;
            } else {
                iter.next();
                iter.next();
                iter.next();
                pos += 4;
            }
        }
    }

    #[inline]
    fn final_lead_surrogate(&self) -> Option<u16> {
        let len = self.len();
        if len < 3 {
            return None
        }
        match self.bytes.slice_from(len - 3) {
            [0xED, b2 @ 0xA0...0xAF, b3] => Some(decode_surrogate(b2, b3)),
            _ => None
        }
    }

    #[inline]
    fn initial_trail_surrogate(&self) -> Option<u16> {
        let len = self.len();
        if len < 3 {
            return None
        }
        match self.bytes.slice_to(3) {
            [0xED, b2 @ 0xB0...0xBF, b3] => Some(decode_surrogate(b2, b3)),
            _ => None
        }
    }
}


#[inline]
fn decode_surrogate(second_byte: u8, third_byte: u8) -> u16 {
    // The first byte is assumed to be 0xED
    0xD800 | (second_byte as u16 & 0x3F) << 6 | third_byte as u16 & 0x3F
}

#[inline]
fn decode_surrogate_pair(lead: u16, trail: u16) -> char {
    let code_point = 0x10000 + (((lead - 0xD800) as u32 << 10) | (trail - 0xDC00) as u32);
    unsafe { transmute(code_point) }
}


/// Iterator for the code points of a WTF-8 string.
///
/// Created with the method `.code_points()`.
#[deriving(Clone)]
pub struct Wtf8CodePoints<'a> {
    bytes: slice::Items<'a, u8>
}

impl<'a> Iterator<CodePoint> for Wtf8CodePoints<'a> {
    #[inline]
    fn next(&mut self) -> Option<CodePoint> {
        not_quite_std::next_code_point(&mut self.bytes)
    }

    #[inline]
    fn size_hint(&self) -> (uint, Option<uint>) {
        let (len, _) = self.bytes.size_hint();
        (len.saturating_add(3) / 4, Some(len))
    }
}

#[deriving(Clone)]
pub struct IllFormedUtf16CodeUnits<'a> {
    code_points: Wtf8CodePoints<'a>,
    extra: u16
}

impl<'a> Iterator<u16> for IllFormedUtf16CodeUnits<'a> {
    #[inline]
    fn next(&mut self) -> Option<u16> {
        not_quite_std::next_utf16_code_unit(self)
    }

    #[inline]
    fn size_hint(&self) -> (uint, Option<uint>) {
        let (low, high) = self.code_points.size_hint();
        // every code point gets either one u16 or two u16,
        // so this iterator is between 1 or 2 times as
        // long as the underlying iterator.
        (low, high.and_then(|n| n.checked_mul(&2)))
    }
}


impl<S: Writer> Hash<S> for CodePoint {
    #[inline]
    fn hash(&self, state: &mut S) {
        self.value.hash(state)
    }
}

impl<S: Writer> Hash<S> for Wtf8String {
    #[inline]
    fn hash(&self, state: &mut S) {
        state.write(self.bytes.as_slice());
        0xfeu8.hash(state)
    }
}

impl<'a, S: Writer> Hash<S> for Wtf8Slice {
    #[inline]
    fn hash(&self, state: &mut S) {
        state.write(&self.bytes);
        0xfeu8.hash(state)
    }
}


#[cfg(test)]
mod tests {
    use std::prelude::*;
    use std::str;
    use super::*;
    use std::mem::transmute;

    #[test]
    fn code_point_from_u32() {
        assert!(CodePoint::from_u32(0).is_some())
        assert!(CodePoint::from_u32(0xD800).is_some())
        assert!(CodePoint::from_u32(0x10FFFF).is_some())
        assert!(CodePoint::from_u32(0x110000).is_none())
    }

    #[test]
    fn code_point_to_u32() {
        fn c(value: u32) -> CodePoint { CodePoint::from_u32(value).unwrap() }
        assert_eq!(c(0).to_u32(), 0)
        assert_eq!(c(0xD800).to_u32(), 0xD800)
        assert_eq!(c(0x10FFFF).to_u32(), 0x10FFFF)
    }

    #[test]
    fn code_point_from_char() {
        assert_eq!(CodePoint::from_char('a').to_u32(), 0x61)
        assert_eq!(CodePoint::from_char('💩').to_u32(), 0x1F4A9)
    }

    #[test]
    fn code_point_to_string() {
        assert_eq!(format!("{}", CodePoint::from_char('a')).as_slice(), "U+0061")
        assert_eq!(format!("{}", CodePoint::from_char('💩')).as_slice(), "U+1F4A9")
    }

    #[test]
    fn code_point_to_char() {
        fn c(value: u32) -> CodePoint { CodePoint::from_u32(value).unwrap() }
        assert_eq!(c(0x61).to_char(), Some('a'))
        assert_eq!(c(0x1F4A9).to_char(), Some('💩'))
        assert_eq!(c(0xD800).to_char(), None)
    }

    #[test]
    fn code_point_to_char_lossy() {
        fn c(value: u32) -> CodePoint { CodePoint::from_u32(value).unwrap() }
        assert_eq!(c(0x61).to_char_lossy(), 'a')
        assert_eq!(c(0x1F4A9).to_char_lossy(), '💩')
        assert_eq!(c(0xD800).to_char_lossy(), '\uFFFD')
    }

    #[test]
    fn wtf8string_new() {
        assert_eq!(Wtf8String::new().bytes.as_slice(), b"");
    }

    #[test]
    fn wtf8string_from_str() {
        assert_eq!(Wtf8String::from_str("").bytes.as_slice(), b"");
        assert_eq!(Wtf8String::from_str("aé 💩").bytes.as_slice(),
                   b"a\xC3\xA9 \xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_from_string() {
        assert_eq!(Wtf8String::from_string(String::from_str("")).bytes.as_slice(), b"");
        assert_eq!(Wtf8String::from_string(String::from_str("aé 💩")).bytes.as_slice(),
                   b"a\xC3\xA9 \xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_from_ill_formed_utf16() {
        assert_eq!(Wtf8String::from_ill_formed_utf16([]).bytes.as_slice(), b"");
        assert_eq!(Wtf8String::from_ill_formed_utf16(
                       [0x61, 0xE9, 0x20, 0xD83D, 0xD83D, 0xDCA9]).bytes.as_slice(),
                   b"a\xC3\xA9 \xED\xA0\xBD\xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_push_str() {
        let mut string = Wtf8String::new();
        assert_eq!(string.bytes.as_slice(), b"");
        string.push_str("aé 💩");
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_push_char() {
        let mut string = Wtf8String::from_str("aé ");
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 ");
        string.push_char('💩');
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_push() {
        let mut string = Wtf8String::from_str("aé ");
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 ");
        string.push(CodePoint::from_char('💩'));
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9");

        fn c(value: u32) -> CodePoint { CodePoint::from_u32(value).unwrap() }

        let mut string = Wtf8String::new();
        string.push(c(0xD83D));  // lead
        string.push(c(0xDCA9));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xF0\x9F\x92\xA9");  // Magic!

        let mut string = Wtf8String::new();
        string.push(c(0xD83D));  // lead
        string.push(c(0x20));  // not surrogate
        string.push(c(0xDCA9));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\xBD \xED\xB2\xA9");

        let mut string = Wtf8String::new();
        string.push(c(0xD800));  // lead
        string.push(c(0xDBFF));  // lead
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\x80\xED\xAF\xBF");

        let mut string = Wtf8String::new();
        string.push(c(0xD800));  // lead
        string.push(c(0xE000));  // not surrogate
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\x80\xEE\x80\x80");

        let mut string = Wtf8String::new();
        string.push(c(0xD7FF));  // not surrogate
        string.push(c(0xDC00));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\x9F\xBF\xED\xB0\x80");

        let mut string = Wtf8String::new();
        string.push(c(0x61));  // not surrogate, < 3 bytes
        string.push(c(0xDC00));  // trail
        assert_eq!(string.bytes.as_slice(), b"\x61\xED\xB0\x80");

        let mut string = Wtf8String::new();
        string.push(c(0xDC00));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\xB0\x80");
    }

    #[test]
    fn wtf8string_push_wtf8() {
        let mut string = Wtf8String::from_str("aé");
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9");
        string.push_wtf8(Wtf8Slice::from_str(" 💩"));
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9");

        fn w(value: &[u8]) -> &Wtf8Slice { unsafe { transmute(value) } }

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xA0\xBD"));  // lead
        string.push_wtf8(w(b"\xED\xB2\xA9"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xF0\x9F\x92\xA9");  // Magic!

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xA0\xBD"));  // lead
        string.push_wtf8(w(b" "));  // not surrogate
        string.push_wtf8(w(b"\xED\xB2\xA9"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\xBD \xED\xB2\xA9");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xA0\x80"));  // lead
        string.push_wtf8(w(b"\xED\xAF\xBF"));  // lead
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\x80\xED\xAF\xBF");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xA0\x80"));  // lead
        string.push_wtf8(w(b"\xEE\x80\x80"));  // not surrogate
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\x80\xEE\x80\x80");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\x9F\xBF"));  // not surrogate
        string.push_wtf8(w(b"\xED\xB0\x80"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\x9F\xBF\xED\xB0\x80");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"a"));  // not surrogate, < 3 bytes
        string.push_wtf8(w(b"\xED\xB0\x80"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\x61\xED\xB0\x80");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xB0\x80"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\xB0\x80");
    }

    #[test]
    fn wtf8string_truncate() {
        let mut string = Wtf8String::from_str("aé");
        string.truncate(1);
        assert_eq!(string.bytes.as_slice(), b"a");
    }

    #[test]
    #[should_fail]
    fn wtf8string_truncate_fail_code_point_boundary() {
        let mut string = Wtf8String::from_str("aé");
        string.truncate(2);
    }

    #[test]
    #[should_fail]
    fn wtf8string_truncate_fail_longer() {
        let mut string = Wtf8String::from_str("aé");
        string.truncate(4);
    }

    #[test]
    fn wtf8string_into_string() {
        let mut string = Wtf8String::from_str("aé 💩");
        assert_eq!(string.clone().into_string(), Ok(String::from_str("aé 💩")));
        string.push(CodePoint::from_u32(0xD800).unwrap());
        assert_eq!(string.clone().into_string(), Err(string));
    }

    #[test]
    fn wtf8string_into_string_lossy() {
        let mut string = Wtf8String::from_str("aé 💩");
        assert_eq!(string.clone().into_string_lossy(), String::from_str("aé 💩"));
        string.push(CodePoint::from_u32(0xD800).unwrap());
        assert_eq!(string.clone().into_string_lossy(), String::from_str("aé 💩�"));
    }

    #[test]
    fn wtf8string_from_iterator() {
        fn f(values: &[u32]) -> Wtf8String {
            values.iter().map(|&c| CodePoint::from_u32(c).unwrap()).collect::<Wtf8String>()
        };
        assert_eq!(f([0x61, 0xE9, 0x20, 0x1F4A9]).bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9")

        assert_eq!(f([0xD83D, 0xDCA9]).bytes.as_slice(), b"\xF0\x9F\x92\xA9");  // Magic!
        assert_eq!(f([0xD83D, 0x20, 0xDCA9]).bytes.as_slice(), b"\xED\xA0\xBD \xED\xB2\xA9");
        assert_eq!(f([0xD800, 0xDBFF]).bytes.as_slice(), b"\xED\xA0\x80\xED\xAF\xBF");
        assert_eq!(f([0xD800, 0xE000]).bytes.as_slice(), b"\xED\xA0\x80\xEE\x80\x80");
        assert_eq!(f([0xD7FF, 0xDC00]).bytes.as_slice(), b"\xED\x9F\xBF\xED\xB0\x80");
        assert_eq!(f([0x61, 0xDC00]).bytes.as_slice(), b"\x61\xED\xB0\x80");
        assert_eq!(f([0xDC00]).bytes.as_slice(), b"\xED\xB0\x80");
    }

    #[test]
    fn wtf8string_extend() {
        fn e(initial: &[u32], extended: &[u32]) -> Wtf8String {
            fn c(value: &u32) -> CodePoint { CodePoint::from_u32(*value).unwrap() }
            let mut string = initial.iter().map(c).collect::<Wtf8String>();
            string.extend(extended.iter().map(c));
            string
        };

        assert_eq!(e([0x61, 0xE9], [0x20, 0x1F4A9]).bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9")

        assert_eq!(e([0xD83D], [0xDCA9]).bytes.as_slice(), b"\xF0\x9F\x92\xA9");  // Magic!
        assert_eq!(e([0xD83D, 0x20], [0xDCA9]).bytes.as_slice(), b"\xED\xA0\xBD \xED\xB2\xA9");
        assert_eq!(e([0xD800], [0xDBFF]).bytes.as_slice(), b"\xED\xA0\x80\xED\xAF\xBF");
        assert_eq!(e([0xD800], [0xE000]).bytes.as_slice(), b"\xED\xA0\x80\xEE\x80\x80");
        assert_eq!(e([0xD7FF], [0xDC00]).bytes.as_slice(), b"\xED\x9F\xBF\xED\xB0\x80");
        assert_eq!(e([0x61], [0xDC00]).bytes.as_slice(), b"\x61\xED\xB0\x80");
        assert_eq!(e([], [0xDC00]).bytes.as_slice(), b"\xED\xB0\x80");
    }

    #[test]
    fn wtf8string_show() {
        let mut string = Wtf8String::from_str("aé 💩");
        string.push(CodePoint::from_u32(0xD800).unwrap());
        assert_eq!(format!("{}", string).as_slice(), r#""aé 💩\uD800""#);
    }<commit_msg>Upgrade to rustc e09d98603 2014-11-18.<commit_after>/*!

Implementation of [the WTF-8 encoding](https://simonsapin.github.io/wtf-8/).

This library uses Rust’s type system to maintain
[well-formedness](https://simonsapin.github.io/wtf-8/#well-formed),
like the `String` and `&str` types do for UTF-8.

Since [WTF-8 must not be used
for interchange](https://simonsapin.github.io/wtf-8/#intended-audience),
this library deliberately does not provide access to the underlying bytes
of WTF-8 strings,
nor can it decode WTF-8 from arbitrary bytes.
WTF-8 strings can be obtained from UTF-8, UTF-16, or code points.

*/

#![feature(globs, default_type_params, phase, macro_rules)]

#![no_std]

#[phase(link, plugin)]
extern crate core;

extern crate collections;

#[cfg(test)]
#[phase(link, plugin)]
extern crate std;

#[cfg(test)]
extern crate "native" as rt;  // provides the `start` lang item, compensate for #![no_std]

use core::prelude::*;

use collections::hash::{Hash, Writer};
use collections::slice::{CloneSliceAllocPrelude};
use collections::str;
use collections::string;
use collections::string::String;
use collections::vec::Vec;
use core::fmt;
use core::mem::transmute;
use core::num::Int;
use core::slice;

// Compensate for #[no_std]
#[cfg(not(test))]
mod std {
    pub use core::fmt;      // necessary for write!()
    pub use core::option;   // deriving(PartialOrd)
    pub use core::clone;    // deriving(Clone)
    pub use core::cmp;      // deriving(Eq, Ord, etc.)
}

mod not_quite_std;


static UTF8_REPLACEMENT_CHARACTER: &'static [u8] = b"\xEF\xBF\xBD";

/// A Unicode code point: from U+0000 to U+10FFFF.
///
/// Compare with the `char` type,
/// which represents a Unicode scalar value:
/// a code point that is not a surrogate (U+D800 to U+DFFF).
#[deriving(Eq, PartialEq, Ord, PartialOrd, Clone)]
pub struct CodePoint {
    value: u32
}

/// Format the code point as `U+` followed by four to six hexadecimal digits.
/// Example: `U+1F4A9`
impl fmt::Show for CodePoint {
    #[inline]
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::FormatError> {
        write!(formatter, "U+{:04X}", self.value)
    }
}


impl CodePoint {
    /// Unsafely create a new `CodePoint` without checking the value.
    ///
    /// Only use when `value` is known to be less than or equal to 0x10FFFF.
    #[inline]
    pub unsafe fn from_u32_unchecked(value: u32) -> CodePoint {
        CodePoint { value: value }
    }

    /// Create a new `CodePoint` if the value is a valid code point.
    ///
    /// Return `None` if `value` is above 0x10FFFF.
    #[inline]
    pub fn from_u32(value: u32) -> Option<CodePoint> {
        match value {
            0 ... 0x10FFFF => Some(CodePoint { value: value }),
            _ => None
        }
    }

    /// Create a new `CodePoint` from a `char`.
    ///
    /// Since all Unicode scalar values are code points, this always succeds.
    #[inline]
    pub fn from_char(value: char) -> CodePoint {
        CodePoint { value: value as u32 }
    }

    /// Return the numeric value of the code point.
    #[inline]
    pub fn to_u32(&self) -> u32 {
        self.value
    }

    /// Optionally return a Unicode scalar value for the code point.
    ///
    /// Return `None` if the code point is a surrogate (from U+D800 to U+DFFF).
    #[inline]
    pub fn to_char(&self) -> Option<char> {
        match self.value {
            0xD800 ... 0xDFFF => None,
            _ => Some(unsafe { transmute(self.value) })
        }
    }

    /// Return a Unicode scalar value for the code point.
    ///
    /// Return `'\uFFFD'` (the replacement character “�”)
    /// if the code point is a surrogate (from U+D800 to U+DFFF).
    #[inline]
    pub fn to_char_lossy(&self) -> char {
        self.to_char().unwrap_or('\uFFFD')
    }
}


/// An owned, growable string of well-formed WTF-8 data.
///
/// Similar to `String`, but can additionally contain surrogate code points
/// if they’re not in a surrogate pair.
#[deriving(Eq, PartialEq, Ord, PartialOrd, Clone)]
pub struct Wtf8String {
    bytes: Vec<u8>
}

impl Deref<Wtf8Slice> for Wtf8String {
    fn deref(&self) -> &Wtf8Slice {
        self.as_slice()
    }
}

/// Format the string with double quotes,
/// and surrogates as `\u` followed by four hexadecimal digits.
/// Example: `"a\uD800"` for a string with code points [U+0061, U+D800]
impl fmt::Show for Wtf8String {
    #[inline]
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::FormatError> {
        self.as_slice().fmt(formatter)
    }
}


impl Wtf8String {
    /// Create an new, empty WTF-8 string.
    #[inline]
    pub fn new() -> Wtf8String {
        Wtf8String { bytes: Vec::new() }
    }

    /// Create an new, empty WTF-8 string with pre-allocated capacity for `n` bytes.
    #[inline]
    pub fn with_capacity(n: uint) -> Wtf8String {
        Wtf8String { bytes: Vec::with_capacity(n) }
    }

    /// Create a WTF-8 string from an UTF-8 `String`.
    ///
    /// This takes ownership of the `String` and does not copy.
    ///
    /// Since WTF-8 is a superset of UTF-8, this always succeeds.
    #[inline]
    pub fn from_string(string: String) -> Wtf8String {
        Wtf8String { bytes: string.into_bytes() }
    }

    /// Create a WTF-8 string from an UTF-8 `&str` slice.
    ///
    /// This copies the content of the slice.
    ///
    /// Since WTF-8 is a superset of UTF-8, this always succeeds.
    #[inline]
    pub fn from_str(str: &str) -> Wtf8String {
        Wtf8String { bytes: str.as_bytes().to_vec() }
    }

    /// Create a WTF-8 string from a potentially ill-formed UTF-16 slice of 16-bit code units.
    ///
    /// This is lossless: calling `.to_ill_formed_utf16()` on the resulting string
    /// will always return the original code units.
    pub fn from_ill_formed_utf16(v: &[u16]) -> Wtf8String {
        let mut string = Wtf8String::with_capacity(v.len());
        for item in str::utf16_items(v) {
            match item {
                str::ScalarValue(c) => string.push_char(c),
                str::LoneSurrogate(s) => {
                    // Surrogates are known to be in the code point range.
                    let code_point = unsafe { CodePoint::from_u32_unchecked(s as u32) };
                    // Skip the WTF-8 concatenation check,
                    // surrogate pairs are already decoded by str::utf16_items
                    not_quite_std::push_code_point(&mut string, code_point)
                }
            }
        }
        string
    }

    #[inline]
    pub fn as_slice(&self) -> &Wtf8Slice {
        unsafe { transmute(self.bytes.as_slice()) }
    }

    /// Reserves capacity for at least `additional` more bytes to be inserted
    /// in the given `Wtf8String`.
    /// The collection may reserve more space to avoid frequent reallocations.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity overflows `uint`.
    ///
    /// # Example
    ///
    /// ```
    /// let mut s = Wtf8String::new();
    /// s.reserve(10);
    /// assert!(s.capacity() >= 10);
    /// ```
    #[inline]
    pub fn reserve(&mut self, additional: uint) {
        self.bytes.reserve(additional)
    }

    /// Returns the number of bytes that this string buffer can hold without reallocating.
    ///
    /// # Example
    ///
    /// ```
    /// let s = Wtf8String::with_capacity(10);
    /// assert!(s.capacity() >= 10);
    /// ```
    #[inline]
    pub fn capacity(&self) -> uint {
        self.bytes.capacity()
    }

    /// Append an UTF-8 slice at the end of the string.
    #[inline]
    pub fn push_str(&mut self, other: &str) {
        self.bytes.push_all(other.as_bytes())
    }

    /// Append a WTF-8 slice at the end of the string.
    ///
    /// This replaces newly paired surrogates at the boundary
    /// with a supplementary code point,
    /// like concatenating ill-formed UTF-16 strings effectively would.
    #[inline]
    pub fn push_wtf8(&mut self, other: &Wtf8Slice) {
        match ((&*self).final_lead_surrogate(), other.initial_trail_surrogate()) {
            // Replace newly paired surrogates by a supplementary code point.
            (Some(lead), Some(trail)) => {
                let len_without_lead_surrogate = self.len() - 3;
                self.bytes.truncate(len_without_lead_surrogate);
                let other_without_trail_surrogate = other.bytes.slice_from(3);
                // 4 bytes for the supplementary code point
                self.bytes.reserve(4 + other_without_trail_surrogate.len());
                self.push_char(decode_surrogate_pair(lead, trail));
                self.bytes.push_all(other_without_trail_surrogate);
            }
            _ => self.bytes.push_all(&other.bytes)
        }
    }

    /// Append a Unicode scalar value at the end of the string.
    #[inline]
    pub fn push_char(&mut self, c: char) {
        not_quite_std::push_code_point(self, CodePoint::from_char(c))
    }

    /// Append a code point at the end of the string.
    ///
    /// This replaces newly paired surrogates at the boundary
    /// with a supplementary code point,
    /// like concatenating ill-formed UTF-16 strings effectively would.
    #[inline]
    pub fn push(&mut self, code_point: CodePoint) {
        match code_point.to_u32() {
            trail @ 0xDC00...0xDFFF => {
                match (&*self).final_lead_surrogate() {
                    Some(lead) => {
                        let len_without_lead_surrogate = self.len() - 3;
                        self.bytes.truncate(len_without_lead_surrogate);
                        self.push_char(decode_surrogate_pair(lead, trail as u16));
                        return
                    }
                    _ => {}
                }
            }
            _ => {}
        }

        // No newly paired surrogates at the boundary.
        not_quite_std::push_code_point(self, code_point)
    }

    /// Shortens a string to the specified length.
    ///
    /// # Failure
    ///
    /// Fails if `new_len` > current length,
    /// or if `new_len` is not a code point boundary.
    #[inline]
    pub fn truncate(&mut self, new_len: uint) {
        assert!(not_quite_std::is_code_point_boundary(self.as_slice(), new_len));
        self.bytes.truncate(new_len)
    }

    /// Consume the WTF-8 string and try to convert it to UTF-8.
    ///
    /// This does not copy the data.
    ///
    /// If the contents are not well-formed UTF-8
    /// (that is, if the string contains surrogates),
    /// the original WTF-8 string is returned instead.
    pub fn into_string(self) -> Result<String, Wtf8String> {
        match self.next_surrogate(0) {
            None => Ok(unsafe { string::raw::from_utf8(self.bytes) }),
            Some(_) => Err(self),
        }
    }

    /// Consume the WTF-8 string and convert it lossily to UTF-8.
    ///
    /// This does not copy the data (but may overwrite parts of it in place).
    ///
    /// Surrogates are replaced with `"\uFFFD"` (the replacement character “�”)
    pub fn into_string_lossy(mut self) -> String {
        let mut pos = 0;
        loop {
            match self.next_surrogate(pos) {
                Some((surrogate_pos, _)) => {
                    pos = surrogate_pos + 3;
                    slice::bytes::copy_memory(
                        self.bytes.slice_mut(surrogate_pos, pos),
                        UTF8_REPLACEMENT_CHARACTER
                    );
                },
                None => return unsafe { string::raw::from_utf8(self.bytes) }
            }
        }
    }
}


/// Create a new WTF-8 string from an iterator of code points.
///
/// This replaces surrogate code point pairs with supplementary code points,
/// like concatenating ill-formed UTF-16 strings effectively would.
impl FromIterator<CodePoint> for Wtf8String {
    fn from_iter<T: Iterator<CodePoint>>(iterator: T) -> Wtf8String {
        let mut string = Wtf8String::new();
        string.extend(iterator);
        string
    }
}


/// Append code points from an iterator to the string.
///
/// This replaces surrogate code point pairs with supplementary code points,
/// like concatenating ill-formed UTF-16 strings effectively would.
impl Extend<CodePoint> for Wtf8String {
    fn extend<T: Iterator<CodePoint>>(&mut self, mut iterator: T) {
        let (low, _high) = iterator.size_hint();
        // Lower bound of one byte per code point (ASCII only)
        self.bytes.reserve(low);
        for code_point in iterator {
            self.push(code_point);
        }
    }
}

/// A borrowed slice of well-formed WTF-8 data.
///
/// Similar to `&str`, but can additionally contain surrogate code points
/// if they’re not in a surrogate pair.
pub struct Wtf8Slice {
    bytes: [u8]
}

// FIXME: https://github.com/rust-lang/rust/issues/18805
impl PartialEq for Wtf8Slice {
    fn eq(&self, other: &Wtf8Slice) -> bool { self.bytes.eq(&other.bytes) }
}

// FIXME: https://github.com/rust-lang/rust/issues/18805
impl Eq for Wtf8Slice {}

// FIXME: https://github.com/rust-lang/rust/issues/18738
impl PartialOrd for Wtf8Slice {
    #[inline]
    fn partial_cmp(&self, other: &Wtf8Slice) -> Option<Ordering> {
        self.bytes.partial_cmp(&other.bytes)
    }
    #[inline]
    fn lt(&self, other: &Wtf8Slice) -> bool { self.bytes.lt(&other.bytes) }
    #[inline]
    fn le(&self, other: &Wtf8Slice) -> bool { self.bytes.le(&other.bytes) }
    #[inline]
    fn gt(&self, other: &Wtf8Slice) -> bool { self.bytes.gt(&other.bytes) }
    #[inline]
    fn ge(&self, other: &Wtf8Slice) -> bool { self.bytes.ge(&other.bytes) }
}

// FIXME: https://github.com/rust-lang/rust/issues/18738
impl Ord for Wtf8Slice {
    #[inline]
    fn cmp(&self, other: &Wtf8Slice) -> Ordering { self.bytes.cmp(&other.bytes) }
}


/// Format the slice with double quotes,
/// and surrogates as `\u` followed by four hexadecimal digits.
/// Example: `"a\uD800"` for a slice with code points [U+0061, U+D800]
impl fmt::Show for Wtf8Slice {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::FormatError> {
        try!(formatter.write(b"\""))
        let mut pos = 0;
        loop {
            match self.next_surrogate(pos) {
                None => break,
                Some((surrogate_pos, surrogate)) => {
                    try!(formatter.write(self.bytes.slice(pos, surrogate_pos)));
                    try!(write!(formatter, "\\u{:X}", surrogate));
                    pos = surrogate_pos + 3;
                }
            }
        }
        try!(formatter.write(self.bytes.slice_from(pos)));
        formatter.write(b"\"")
    }
}


impl Wtf8Slice {
    /// Create a WTF-8 slice from a UTF-8 `&str` slice.
    ///
    /// Since WTF-8 is a superset of UTF-8, this always succeeds.
    #[inline]
    pub fn from_str(value: &str) -> &Wtf8Slice {
        unsafe { transmute(value.as_bytes()) }
    }

    /// Return the length, in WTF-8 bytes.
    #[inline]
    pub fn len(&self) -> uint {
        self.bytes.len()
    }

    /// Return a slice of the given string for the byte range [`begin`..`end`).
    ///
    /// # Failure
    ///
    /// Fails when `begin` and `end` do not point to code point boundaries,
    /// or point beyond the end of the string.
    #[inline]
    pub fn slice(&self, begin: uint, end: uint) -> &Wtf8Slice {
        // is_code_point_boundary checks that the index is in [0, .len()]
        if begin <= end &&
           not_quite_std::is_code_point_boundary(self, begin) &&
           not_quite_std::is_code_point_boundary(self, end) {
            unsafe { not_quite_std::slice_unchecked(self, begin, end) }
        } else {
            not_quite_std::slice_error_fail(self, begin, end)
        }
    }

    /// Return a slice of the given string from byte `begin` to its end.
    ///
    /// # Failure
    ///
    /// Fails when `begin` is not at a code point boundary,
    /// or is beyond the end of the string.
    #[inline]
    pub fn slice_from(&self, begin: uint) -> &Wtf8Slice {
        // is_code_point_boundary checks that the index is in [0, .len()]
        if not_quite_std::is_code_point_boundary(self, begin) {
            unsafe { not_quite_std::slice_unchecked(self, begin, self.len()) }
        } else {
            not_quite_std::slice_error_fail(self, begin, self.len())
        }
    }

    /// Return a slice of the given string from its beginning to byte `end`.
    ///
    /// # Failure
    ///
    /// Fails when `end` is not at a code point boundary,
    /// or is beyond the end of the string.
    #[inline]
    pub fn slice_to(&self, end: uint) -> &Wtf8Slice {
        // is_code_point_boundary checks that the index is in [0, .len()]
        if not_quite_std::is_code_point_boundary(self, end) {
            unsafe { not_quite_std::slice_unchecked(self, 0, end) }
        } else {
            not_quite_std::slice_error_fail(self, 0, end)
        }
    }

    /// Return the code point at `position` if it is in the ASCII range,
    /// or `b'\xFF' otherwise.
    ///
    /// # Failure
    ///
    /// Fails if `position` is beyond the end of the string.
    #[inline]
    pub fn ascii_byte_at(&self, position: uint) -> u8 {
        match self.bytes[position] {
            ascii_byte @ 0x00 ... 0x7F => ascii_byte,
            _ => 0xFF
        }
    }

    /// Return the code point at `position`.
    ///
    /// # Failure
    ///
    /// Fails if `position` is not at a code point boundary,
    /// or is beyond the end of the string.
    #[inline]
    pub fn code_point_at(&self, position: uint) -> CodePoint {
        let (code_point, _) = self.code_point_range_at(position);
        code_point
    }

    /// Return the code point at `position`
    /// and the position of the next code point.
    ///
    /// # Failure
    ///
    /// Fails if `position` is not at a code point boundary,
    /// or is beyond the end of the string.
    #[inline]
    pub fn code_point_range_at(&self, position: uint) -> (CodePoint, uint) {
        not_quite_std::code_point_range_at(self, position)
    }

    /// Return an iterator for the string’s code points.
    #[inline]
    pub fn code_points(&self) -> Wtf8CodePoints {
        Wtf8CodePoints { bytes: self.bytes.iter() }
    }

    /// Try to convert the string to UTF-8 and return a `&str` slice.
    ///
    /// Return `None` if the string contains surrogates.
    ///
    /// This does not copy the data.
    #[inline]
    pub fn as_str(&self) -> Option<&str> {
        // Well-formed WTF-8 is also well-formed UTF-8
        // if and only if it contains no surrogate.
        match self.next_surrogate(0) {
            None => Some(unsafe { str::raw::from_utf8(&self.bytes) }),
            Some(_) => None,
        }
    }

    /// Lossily convert the string to UTF-8.
    /// Return an UTF-8 `&str` slice if the contents are well-formed in UTF-8.
    ///
    /// Surrogates are replaced with `"\uFFFD"` (the replacement character “�”).
    ///
    /// This only copies the data if necessary (if it contains any surrogate).
    pub fn to_string_lossy(&self) -> str::MaybeOwned {
        let surrogate_pos = match self.next_surrogate(0) {
            None => return str::Slice(unsafe { str::raw::from_utf8(&self.bytes) }),
            Some((pos, _)) => pos,
        };
        let wtf8_bytes = &self.bytes;
        let mut utf8_bytes = Vec::with_capacity(self.len());
        utf8_bytes.push_all(wtf8_bytes.slice_to(surrogate_pos));
        utf8_bytes.push_all(UTF8_REPLACEMENT_CHARACTER);
        let mut pos = surrogate_pos + 3;
        loop {
            match self.next_surrogate(pos) {
                Some((surrogate_pos, _)) => {
                    utf8_bytes.push_all(wtf8_bytes.slice(pos, surrogate_pos));
                    utf8_bytes.push_all(UTF8_REPLACEMENT_CHARACTER);
                    pos = surrogate_pos + 3;
                },
                None => {
                    utf8_bytes.push_all(wtf8_bytes.slice_from(pos));
                    return str::Owned(unsafe { string::raw::from_utf8(utf8_bytes) })
                }
            }
        }
    }

    /// Convert the WTF-8 string to potentially ill-formed UTF-16
    /// and return an iterator of 16-bit code units.
    ///
    /// This is lossless:
    /// calling `Wtf8String::from_ill_formed_utf16` on the resulting code units
    /// would always return the original WTF-8 string.
    #[inline]
    pub fn to_ill_formed_utf16(&self) -> IllFormedUtf16CodeUnits {
        IllFormedUtf16CodeUnits { code_points: self.code_points(), extra: 0 }
    }

    #[inline]
    fn next_surrogate(&self, mut pos: uint) -> Option<(uint, u16)> {
        let mut iter = self.bytes.slice_from(pos).iter();
        loop {
            let b = match iter.next() {
                None => return None,
                Some(&b) => b,
            };
            if b < 0x80 {
                pos += 1;
            } else if b < 0xE0 {
                iter.next();
                pos += 2;
            } else if b == 0xED {
                match (iter.next(), iter.next()) {
                    (Some(&b2), Some(&b3)) if b2 >= 0xA0 => {
                        return Some((pos, decode_surrogate(b2, b3)))
                    }
                    _ => pos += 3
                }
            } else if b < 0xF0 {
                iter.next();
                iter.next();
                pos += 3;
            } else {
                iter.next();
                iter.next();
                iter.next();
                pos += 4;
            }
        }
    }

    #[inline]
    fn final_lead_surrogate(&self) -> Option<u16> {
        let len = self.len();
        if len < 3 {
            return None
        }
        match self.bytes.slice_from(len - 3) {
            [0xED, b2 @ 0xA0...0xAF, b3] => Some(decode_surrogate(b2, b3)),
            _ => None
        }
    }

    #[inline]
    fn initial_trail_surrogate(&self) -> Option<u16> {
        let len = self.len();
        if len < 3 {
            return None
        }
        match self.bytes.slice_to(3) {
            [0xED, b2 @ 0xB0...0xBF, b3] => Some(decode_surrogate(b2, b3)),
            _ => None
        }
    }
}


#[inline]
fn decode_surrogate(second_byte: u8, third_byte: u8) -> u16 {
    // The first byte is assumed to be 0xED
    0xD800 | (second_byte as u16 & 0x3F) << 6 | third_byte as u16 & 0x3F
}

#[inline]
fn decode_surrogate_pair(lead: u16, trail: u16) -> char {
    let code_point = 0x10000 + (((lead - 0xD800) as u32 << 10) | (trail - 0xDC00) as u32);
    unsafe { transmute(code_point) }
}


/// Iterator for the code points of a WTF-8 string.
///
/// Created with the method `.code_points()`.
#[deriving(Clone)]
pub struct Wtf8CodePoints<'a> {
    bytes: slice::Items<'a, u8>
}

impl<'a> Iterator<CodePoint> for Wtf8CodePoints<'a> {
    #[inline]
    fn next(&mut self) -> Option<CodePoint> {
        not_quite_std::next_code_point(&mut self.bytes)
    }

    #[inline]
    fn size_hint(&self) -> (uint, Option<uint>) {
        let (len, _) = self.bytes.size_hint();
        (len.saturating_add(3) / 4, Some(len))
    }
}

#[deriving(Clone)]
pub struct IllFormedUtf16CodeUnits<'a> {
    code_points: Wtf8CodePoints<'a>,
    extra: u16
}

impl<'a> Iterator<u16> for IllFormedUtf16CodeUnits<'a> {
    #[inline]
    fn next(&mut self) -> Option<u16> {
        not_quite_std::next_utf16_code_unit(self)
    }

    #[inline]
    fn size_hint(&self) -> (uint, Option<uint>) {
        let (low, high) = self.code_points.size_hint();
        // every code point gets either one u16 or two u16,
        // so this iterator is between 1 or 2 times as
        // long as the underlying iterator.
        (low, high.and_then(|n| n.checked_mul(2)))
    }
}


impl<S: Writer> Hash<S> for CodePoint {
    #[inline]
    fn hash(&self, state: &mut S) {
        self.value.hash(state)
    }
}

impl<S: Writer> Hash<S> for Wtf8String {
    #[inline]
    fn hash(&self, state: &mut S) {
        state.write(self.bytes.as_slice());
        0xfeu8.hash(state)
    }
}

impl<'a, S: Writer> Hash<S> for Wtf8Slice {
    #[inline]
    fn hash(&self, state: &mut S) {
        state.write(&self.bytes);
        0xfeu8.hash(state)
    }
}


#[cfg(test)]
mod tests {
    use std::prelude::*;
    use std::str;
    use super::*;
    use std::mem::transmute;

    #[test]
    fn code_point_from_u32() {
        assert!(CodePoint::from_u32(0).is_some())
        assert!(CodePoint::from_u32(0xD800).is_some())
        assert!(CodePoint::from_u32(0x10FFFF).is_some())
        assert!(CodePoint::from_u32(0x110000).is_none())
    }

    #[test]
    fn code_point_to_u32() {
        fn c(value: u32) -> CodePoint { CodePoint::from_u32(value).unwrap() }
        assert_eq!(c(0).to_u32(), 0)
        assert_eq!(c(0xD800).to_u32(), 0xD800)
        assert_eq!(c(0x10FFFF).to_u32(), 0x10FFFF)
    }

    #[test]
    fn code_point_from_char() {
        assert_eq!(CodePoint::from_char('a').to_u32(), 0x61)
        assert_eq!(CodePoint::from_char('💩').to_u32(), 0x1F4A9)
    }

    #[test]
    fn code_point_to_string() {
        assert_eq!(format!("{}", CodePoint::from_char('a')).as_slice(), "U+0061")
        assert_eq!(format!("{}", CodePoint::from_char('💩')).as_slice(), "U+1F4A9")
    }

    #[test]
    fn code_point_to_char() {
        fn c(value: u32) -> CodePoint { CodePoint::from_u32(value).unwrap() }
        assert_eq!(c(0x61).to_char(), Some('a'))
        assert_eq!(c(0x1F4A9).to_char(), Some('💩'))
        assert_eq!(c(0xD800).to_char(), None)
    }

    #[test]
    fn code_point_to_char_lossy() {
        fn c(value: u32) -> CodePoint { CodePoint::from_u32(value).unwrap() }
        assert_eq!(c(0x61).to_char_lossy(), 'a')
        assert_eq!(c(0x1F4A9).to_char_lossy(), '💩')
        assert_eq!(c(0xD800).to_char_lossy(), '\uFFFD')
    }

    #[test]
    fn wtf8string_new() {
        assert_eq!(Wtf8String::new().bytes.as_slice(), b"");
    }

    #[test]
    fn wtf8string_from_str() {
        assert_eq!(Wtf8String::from_str("").bytes.as_slice(), b"");
        assert_eq!(Wtf8String::from_str("aé 💩").bytes.as_slice(),
                   b"a\xC3\xA9 \xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_from_string() {
        assert_eq!(Wtf8String::from_string(String::from_str("")).bytes.as_slice(), b"");
        assert_eq!(Wtf8String::from_string(String::from_str("aé 💩")).bytes.as_slice(),
                   b"a\xC3\xA9 \xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_from_ill_formed_utf16() {
        assert_eq!(Wtf8String::from_ill_formed_utf16(&[]).bytes.as_slice(), b"");
        assert_eq!(Wtf8String::from_ill_formed_utf16(
                      &[0x61, 0xE9, 0x20, 0xD83D, 0xD83D, 0xDCA9]).bytes.as_slice(),
                   b"a\xC3\xA9 \xED\xA0\xBD\xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_push_str() {
        let mut string = Wtf8String::new();
        assert_eq!(string.bytes.as_slice(), b"");
        string.push_str("aé 💩");
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_push_char() {
        let mut string = Wtf8String::from_str("aé ");
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 ");
        string.push_char('💩');
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9");
    }

    #[test]
    fn wtf8string_push() {
        let mut string = Wtf8String::from_str("aé ");
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 ");
        string.push(CodePoint::from_char('💩'));
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9");

        fn c(value: u32) -> CodePoint { CodePoint::from_u32(value).unwrap() }

        let mut string = Wtf8String::new();
        string.push(c(0xD83D));  // lead
        string.push(c(0xDCA9));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xF0\x9F\x92\xA9");  // Magic!

        let mut string = Wtf8String::new();
        string.push(c(0xD83D));  // lead
        string.push(c(0x20));  // not surrogate
        string.push(c(0xDCA9));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\xBD \xED\xB2\xA9");

        let mut string = Wtf8String::new();
        string.push(c(0xD800));  // lead
        string.push(c(0xDBFF));  // lead
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\x80\xED\xAF\xBF");

        let mut string = Wtf8String::new();
        string.push(c(0xD800));  // lead
        string.push(c(0xE000));  // not surrogate
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\x80\xEE\x80\x80");

        let mut string = Wtf8String::new();
        string.push(c(0xD7FF));  // not surrogate
        string.push(c(0xDC00));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\x9F\xBF\xED\xB0\x80");

        let mut string = Wtf8String::new();
        string.push(c(0x61));  // not surrogate, < 3 bytes
        string.push(c(0xDC00));  // trail
        assert_eq!(string.bytes.as_slice(), b"\x61\xED\xB0\x80");

        let mut string = Wtf8String::new();
        string.push(c(0xDC00));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\xB0\x80");
    }

    #[test]
    fn wtf8string_push_wtf8() {
        let mut string = Wtf8String::from_str("aé");
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9");
        string.push_wtf8(Wtf8Slice::from_str(" 💩"));
        assert_eq!(string.bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9");

        fn w(value: &[u8]) -> &Wtf8Slice { unsafe { transmute(value) } }

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xA0\xBD"));  // lead
        string.push_wtf8(w(b"\xED\xB2\xA9"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xF0\x9F\x92\xA9");  // Magic!

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xA0\xBD"));  // lead
        string.push_wtf8(w(b" "));  // not surrogate
        string.push_wtf8(w(b"\xED\xB2\xA9"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\xBD \xED\xB2\xA9");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xA0\x80"));  // lead
        string.push_wtf8(w(b"\xED\xAF\xBF"));  // lead
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\x80\xED\xAF\xBF");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xA0\x80"));  // lead
        string.push_wtf8(w(b"\xEE\x80\x80"));  // not surrogate
        assert_eq!(string.bytes.as_slice(), b"\xED\xA0\x80\xEE\x80\x80");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\x9F\xBF"));  // not surrogate
        string.push_wtf8(w(b"\xED\xB0\x80"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\x9F\xBF\xED\xB0\x80");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"a"));  // not surrogate, < 3 bytes
        string.push_wtf8(w(b"\xED\xB0\x80"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\x61\xED\xB0\x80");

        let mut string = Wtf8String::new();
        string.push_wtf8(w(b"\xED\xB0\x80"));  // trail
        assert_eq!(string.bytes.as_slice(), b"\xED\xB0\x80");
    }

    #[test]
    fn wtf8string_truncate() {
        let mut string = Wtf8String::from_str("aé");
        string.truncate(1);
        assert_eq!(string.bytes.as_slice(), b"a");
    }

    #[test]
    #[should_fail]
    fn wtf8string_truncate_fail_code_point_boundary() {
        let mut string = Wtf8String::from_str("aé");
        string.truncate(2);
    }

    #[test]
    #[should_fail]
    fn wtf8string_truncate_fail_longer() {
        let mut string = Wtf8String::from_str("aé");
        string.truncate(4);
    }

    #[test]
    fn wtf8string_into_string() {
        let mut string = Wtf8String::from_str("aé 💩");
        assert_eq!(string.clone().into_string(), Ok(String::from_str("aé 💩")));
        string.push(CodePoint::from_u32(0xD800).unwrap());
        assert_eq!(string.clone().into_string(), Err(string));
    }

    #[test]
    fn wtf8string_into_string_lossy() {
        let mut string = Wtf8String::from_str("aé 💩");
        assert_eq!(string.clone().into_string_lossy(), String::from_str("aé 💩"));
        string.push(CodePoint::from_u32(0xD800).unwrap());
        assert_eq!(string.clone().into_string_lossy(), String::from_str("aé 💩�"));
    }

    #[test]
    fn wtf8string_from_iterator() {
        fn f(values: &[u32]) -> Wtf8String {
            values.iter().map(|&c| CodePoint::from_u32(c).unwrap()).collect::<Wtf8String>()
        };
        assert_eq!(f(&[0x61, 0xE9, 0x20, 0x1F4A9]).bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9")

        assert_eq!(f(&[0xD83D, 0xDCA9]).bytes.as_slice(), b"\xF0\x9F\x92\xA9");  // Magic!
        assert_eq!(f(&[0xD83D, 0x20, 0xDCA9]).bytes.as_slice(), b"\xED\xA0\xBD \xED\xB2\xA9");
        assert_eq!(f(&[0xD800, 0xDBFF]).bytes.as_slice(), b"\xED\xA0\x80\xED\xAF\xBF");
        assert_eq!(f(&[0xD800, 0xE000]).bytes.as_slice(), b"\xED\xA0\x80\xEE\x80\x80");
        assert_eq!(f(&[0xD7FF, 0xDC00]).bytes.as_slice(), b"\xED\x9F\xBF\xED\xB0\x80");
        assert_eq!(f(&[0x61, 0xDC00]).bytes.as_slice(), b"\x61\xED\xB0\x80");
        assert_eq!(f(&[0xDC00]).bytes.as_slice(), b"\xED\xB0\x80");
    }

    #[test]
    fn wtf8string_extend() {
        fn e(initial: &[u32], extended: &[u32]) -> Wtf8String {
            fn c(value: &u32) -> CodePoint { CodePoint::from_u32(*value).unwrap() }
            let mut string = initial.iter().map(c).collect::<Wtf8String>();
            string.extend(extended.iter().map(c));
            string
        };

        assert_eq!(e(&[0x61, 0xE9], &[0x20, 0x1F4A9]).bytes.as_slice(), b"a\xC3\xA9 \xF0\x9F\x92\xA9")

        assert_eq!(e(&[0xD83D], &[0xDCA9]).bytes.as_slice(), b"\xF0\x9F\x92\xA9");  // Magic!
        assert_eq!(e(&[0xD83D, 0x20], &[0xDCA9]).bytes.as_slice(), b"\xED\xA0\xBD \xED\xB2\xA9");
        assert_eq!(e(&[0xD800], &[0xDBFF]).bytes.as_slice(), b"\xED\xA0\x80\xED\xAF\xBF");
        assert_eq!(e(&[0xD800], &[0xE000]).bytes.as_slice(), b"\xED\xA0\x80\xEE\x80\x80");
        assert_eq!(e(&[0xD7FF], &[0xDC00]).bytes.as_slice(), b"\xED\x9F\xBF\xED\xB0\x80");
        assert_eq!(e(&[0x61], &[0xDC00]).bytes.as_slice(), b"\x61\xED\xB0\x80");
        assert_eq!(e(&[], &[0xDC00]).bytes.as_slice(), b"\xED\xB0\x80");
    }

    #[test]
    fn wtf8string_show() {
        let mut string = Wtf8String::from_str("aé 💩");
        string.push(CodePoint::from_u32(0xD800).unwrap());
        assert_eq!(format!("{}", string).as_slice(), r#""aé 💩\uD800""#);
    }
{'fraction_non_alphanumeric': 0.15483852179737326, 'fraction_numerical': 0.02948542473572323, 'mean_word_length': 2.2248203972390477, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 273, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1937685', 'n_tokens_mistral': 24958, 'n_tokens_neox': 23693, 'n_words': 5294}