extern crate alloc;
use alloc::{
borrow::{Borrow, Cow},
string::String,
vec::Vec,
};
use core::{
fmt, hash,
iter::{FromIterator, IntoIterator},
mem::transmute,
ops::Deref,
slice, str,
str::FromStr,
};
use std::ops::Add;
mod not_quite_std;
static UTF8_REPLACEMENT_CHARACTER: &[u8] = b"\xEF\xBF\xBD";
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
pub struct CodePoint {
value: u32,
}
impl Copy for CodePoint {}
impl fmt::Debug for CodePoint {
#[inline]
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(formatter, "U+{:04X}", self.value)
}
}
impl CodePoint {
#[inline]
pub const unsafe fn from_u32_unchecked(value: u32) -> CodePoint {
CodePoint { value }
}
#[inline]
pub const fn from_u32(value: u32) -> Option<CodePoint> {
match value {
0..=0x10ffff => Some(CodePoint { value }),
_ => None,
}
}
#[inline]
pub const fn from_char(value: char) -> CodePoint {
CodePoint {
value: value as u32,
}
}
#[inline]
pub fn to_u32(&self) -> u32 {
self.value
}
#[inline]
pub fn to_char(&self) -> Option<char> {
match self.value {
0xd800..=0xdfff => None,
_ => Some(unsafe { char::from_u32_unchecked(self.value) }),
}
}
#[inline]
pub fn to_char_lossy(&self) -> char {
self.to_char().unwrap_or('\u{FFFD}')
}
#[inline]
pub fn is_ascii(&self) -> bool {
self.value <= 0x7f
}
}
impl PartialEq<char> for CodePoint {
fn eq(&self, other: &char) -> bool {
self.value == *other as u32
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
pub struct Wtf8Buf {
bytes: Vec<u8>,
}
impl Deref for Wtf8Buf {
type Target = Wtf8;
fn deref(&self) -> &Wtf8 {
unsafe { transmute(&*self.bytes) }
}
}
impl fmt::Debug for Wtf8Buf {
#[inline]
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
Wtf8::fmt(self, formatter)
}
}
impl Default for Wtf8Buf {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl FromStr for Wtf8Buf {
type Err = core::convert::Infallible;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Wtf8Buf {
bytes: s.as_bytes().to_vec(),
})
}
}
impl fmt::Write for Wtf8Buf {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
self.push_str(s);
Ok(())
}
}
impl Add<&Wtf8> for Wtf8Buf {
type Output = Wtf8Buf;
fn add(self, rhs: &Wtf8) -> Self::Output {
let mut result = self;
result.push_wtf8(rhs);
result
}
}
impl Wtf8Buf {
#[inline]
pub fn new() -> Wtf8Buf {
Wtf8Buf { bytes: Vec::new() }
}
#[inline]
pub fn with_capacity(n: usize) -> Wtf8Buf {
Wtf8Buf {
bytes: Vec::with_capacity(n),
}
}
#[inline]
pub fn from_string(string: String) -> Wtf8Buf {
Wtf8Buf {
bytes: string.into_bytes(),
}
}
#[inline]
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Wtf8Buf {
Wtf8Buf {
bytes: s.as_bytes().to_vec(),
}
}
pub fn from_ill_formed_utf16(v: &[u16]) -> Wtf8Buf {
let mut string = Wtf8Buf::with_capacity(v.len());
for item in not_quite_std::decode_utf16(v.iter().cloned()) {
match item {
Ok(c) => string.push_char(c),
Err(s) => {
let code_point = unsafe { CodePoint::from_u32_unchecked(s as u32) };
not_quite_std::push_code_point(&mut string, code_point)
}
}
}
string
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.bytes.reserve(additional)
}
#[inline]
pub fn capacity(&self) -> usize {
self.bytes.capacity()
}
#[inline]
pub fn push_str(&mut self, other: &str) {
self.bytes.extend_from_slice(other.as_bytes())
}
#[inline]
pub fn push_wtf8(&mut self, other: &Wtf8) {
match (self.final_lead_surrogate(), other.initial_trail_surrogate()) {
(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[3..];
self.bytes.reserve(4 + other_without_trail_surrogate.len());
self.push_char(decode_surrogate_pair(lead, trail));
self.bytes.extend_from_slice(other_without_trail_surrogate);
}
_ => self.bytes.extend_from_slice(&other.bytes),
}
}
#[inline]
pub fn push_char(&mut self, c: char) {
not_quite_std::push_code_point(self, CodePoint::from_char(c))
}
#[inline]
pub fn push(&mut self, code_point: CodePoint) {
if let trail @ 0xdc00..=0xdfff = code_point.to_u32() {
if let Some(lead) = self.final_lead_surrogate() {
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;
}
}
not_quite_std::push_code_point(self, code_point)
}
#[inline]
pub fn truncate(&mut self, new_len: usize) {
assert!(not_quite_std::is_code_point_boundary(self, new_len));
self.bytes.truncate(new_len)
}
#[inline]
pub fn clear(&mut self) {
self.bytes.clear();
}
pub fn into_string(self) -> Result<String, Wtf8Buf> {
match self.next_surrogate(0) {
None => Ok(unsafe { String::from_utf8_unchecked(self.bytes) }),
Some(_) => Err(self),
}
}
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;
self.bytes[surrogate_pos..pos].copy_from_slice(UTF8_REPLACEMENT_CHARACTER);
}
None => return unsafe { String::from_utf8_unchecked(self.bytes) },
}
}
}
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, Vec<u8>> {
if not_quite_std::validate_wtf8(&bytes) {
Ok(Self { bytes })
} else {
Err(bytes)
}
}
#[inline]
pub unsafe fn from_bytes_unchecked(bytes: Vec<u8>) -> Self {
Self { bytes }
}
}
impl FromIterator<CodePoint> for Wtf8Buf {
fn from_iter<T: IntoIterator<Item = CodePoint>>(iterable: T) -> Wtf8Buf {
let mut string = Wtf8Buf::new();
string.extend(iterable);
string
}
}
impl Extend<CodePoint> for Wtf8Buf {
fn extend<T: IntoIterator<Item = CodePoint>>(&mut self, iterable: T) {
let iterator = iterable.into_iter();
let (low, _high) = iterator.size_hint();
self.bytes.reserve(low);
for code_point in iterator {
self.push(code_point);
}
}
}
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct Wtf8 {
bytes: [u8],
}
impl fmt::Debug for Wtf8 {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
formatter.write_str("\"")?;
let mut pos = 0;
loop {
match self.next_surrogate(pos) {
None => break,
Some((surrogate_pos, surrogate)) => {
formatter.write_str(unsafe {
str::from_utf8_unchecked(&self.bytes[pos..surrogate_pos])
})?;
write!(formatter, "\\u{{{surrogate:X}}}")?;
pos = surrogate_pos + 3;
}
}
}
formatter.write_str(unsafe { str::from_utf8_unchecked(&self.bytes[pos..]) })?;
formatter.write_str("\"")
}
}
impl Wtf8 {
#[inline]
pub const fn from_str(value: &str) -> &Wtf8 {
unsafe { transmute(value.as_bytes()) }
}
#[inline]
pub const fn len(&self) -> usize {
self.bytes.len()
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
#[inline]
pub const fn is_ascii(&self) -> bool {
self.bytes.is_ascii()
}
#[inline]
pub fn slice(&self, begin: usize, end: usize) -> &Wtf8 {
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)
}
}
#[inline]
pub fn slice_from(&self, begin: usize) -> &Wtf8 {
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())
}
}
#[inline]
pub fn slice_to(&self, end: usize) -> &Wtf8 {
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)
}
}
#[inline]
pub fn ascii_byte_at(&self, position: usize) -> u8 {
match self.bytes[position] {
ascii_byte @ 0x00..=0x7f => ascii_byte,
_ => 0xff,
}
}
#[inline]
pub fn code_points(&self) -> Wtf8CodePoints {
Wtf8CodePoints {
bytes: self.bytes.iter(),
}
}
#[inline]
pub fn contains_char(&self, ch: char) -> bool {
let target = CodePoint::from_char(ch);
self.contains(target)
}
#[inline]
pub fn contains(&self, code_point: CodePoint) -> bool {
self.code_points().any(|cp| cp == code_point)
}
#[inline]
pub fn starts_with(&self, pattern: &str) -> bool {
if pattern.len() > self.len() {
return false;
}
let pattern_wtf8 = self.slice_to(pattern.len());
if let Some(pattern_str) = pattern_wtf8.as_str() {
pattern_str == pattern
} else {
false
}
}
#[inline]
pub fn as_str(&self) -> Option<&str> {
match self.next_surrogate(0) {
None => Some(unsafe { str::from_utf8_unchecked(&self.bytes) }),
Some(_) => None,
}
}
#[inline]
pub const fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub fn to_string_lossy(&self) -> Cow<str> {
let surrogate_pos = match self.next_surrogate(0) {
None => return Cow::Borrowed(unsafe { str::from_utf8_unchecked(&self.bytes) }),
Some((pos, _)) => pos,
};
let wtf8_bytes = &self.bytes;
let mut utf8_bytes = Vec::with_capacity(self.len());
utf8_bytes.extend_from_slice(&wtf8_bytes[..surrogate_pos]);
utf8_bytes.extend_from_slice(UTF8_REPLACEMENT_CHARACTER);
let mut pos = surrogate_pos + 3;
loop {
match self.next_surrogate(pos) {
Some((surrogate_pos, _)) => {
utf8_bytes.extend_from_slice(&wtf8_bytes[pos..surrogate_pos]);
utf8_bytes.extend_from_slice(UTF8_REPLACEMENT_CHARACTER);
pos = surrogate_pos + 3;
}
None => {
utf8_bytes.extend_from_slice(&wtf8_bytes[pos..]);
return Cow::Owned(unsafe { String::from_utf8_unchecked(utf8_bytes) });
}
}
}
}
#[inline]
pub fn to_ill_formed_utf16(&self) -> IllFormedUtf16CodeUnits {
IllFormedUtf16CodeUnits {
code_points: self.code_points(),
extra: 0,
}
}
#[inline]
pub fn to_uppercase(&self) -> Wtf8Buf {
let mut result = Wtf8Buf::with_capacity(self.len());
for cp in self.code_points() {
if let Some(ch) = cp.to_char() {
for upper_ch in ch.to_uppercase() {
result.push_char(upper_ch);
}
} else {
let code_point = unsafe { CodePoint::from_u32_unchecked(cp.to_u32()) };
not_quite_std::push_code_point(&mut result, code_point)
}
}
result
}
#[inline]
pub fn to_lowercase(&self) -> Wtf8Buf {
let mut result = Wtf8Buf::with_capacity(self.len());
for cp in self.code_points() {
if let Some(ch) = cp.to_char() {
for lower_ch in ch.to_lowercase() {
result.push_char(lower_ch);
}
} else {
let code_point = unsafe { CodePoint::from_u32_unchecked(cp.to_u32()) };
not_quite_std::push_code_point(&mut result, code_point)
}
}
result
}
pub fn from_bytes(bytes: &[u8]) -> Result<&Wtf8, &[u8]> {
if not_quite_std::validate_wtf8(bytes) {
Ok(unsafe { transmute::<&[u8], &Wtf8>(bytes) })
} else {
Err(bytes)
}
}
#[inline]
pub const unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Wtf8 {
unsafe { transmute(bytes) }
}
#[inline]
fn next_surrogate(&self, mut pos: usize) -> Option<(usize, u16)> {
let mut iter = self.bytes[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;
}
let seq = &self.bytes[len - 3..];
if seq[0] == 0xed && 0xa0 <= seq[1] && seq[1] <= 0xaf {
Some(decode_surrogate(seq[1], seq[2]))
} else {
None
}
}
#[inline]
fn initial_trail_surrogate(&self) -> Option<u16> {
let len = self.len();
if len < 3 {
return None;
}
let seq = &self.bytes[..3];
if seq[0] == 0xed && 0xb0 <= seq[1] && seq[1] <= 0xbf {
Some(decode_surrogate(seq[1], seq[2]))
} else {
None
}
}
}
#[inline]
fn decode_surrogate(second_byte: u8, third_byte: u8) -> u16 {
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 as u32 - 0xd800) << 10) | (trail as u32 - 0xdc00));
unsafe { char::from_u32_unchecked(code_point) }
}
#[derive(Clone)]
pub struct Wtf8CodePoints<'a> {
bytes: slice::Iter<'a, u8>,
}
impl<'a> Iterator for Wtf8CodePoints<'a> {
type Item = CodePoint;
#[inline]
fn next(&mut self) -> Option<CodePoint> {
not_quite_std::next_code_point(&mut self.bytes).map(|value| {
unsafe { CodePoint::from_u32_unchecked(value) }
})
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (len, _) = self.bytes.size_hint();
(len.saturating_add(3) / 4, Some(len))
}
}
#[derive(Clone)]
pub struct IllFormedUtf16CodeUnits<'a> {
code_points: Wtf8CodePoints<'a>,
extra: u16,
}
impl<'a> Iterator for IllFormedUtf16CodeUnits<'a> {
type Item = u16;
#[inline]
fn next(&mut self) -> Option<u16> {
not_quite_std::next_utf16_code_unit(self)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (low, high) = self.code_points.size_hint();
(low, high.and_then(|n| n.checked_mul(2)))
}
}
impl PartialEq<&Wtf8> for Wtf8Buf {
fn eq(&self, other: &&Wtf8) -> bool {
**self == **other
}
}
impl PartialEq<Wtf8Buf> for &Wtf8 {
fn eq(&self, other: &Wtf8Buf) -> bool {
**self == **other
}
}
impl PartialEq<str> for &Wtf8 {
fn eq(&self, other: &str) -> bool {
match self.as_str() {
Some(s) => s == other,
None => false,
}
}
}
impl PartialEq<&str> for &Wtf8 {
fn eq(&self, other: &&str) -> bool {
match self.as_str() {
Some(s) => s == *other,
None => false,
}
}
}
impl hash::Hash for CodePoint {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.value.hash(state)
}
}
impl hash::Hash for Wtf8Buf {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
Wtf8::hash(self, state)
}
}
impl hash::Hash for Wtf8 {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
state.write(&self.bytes);
0xfeu8.hash(state)
}
}
impl Borrow<Wtf8> for Wtf8Buf {
#[inline]
fn borrow(&self) -> &Wtf8 {
self
}
}
impl ToOwned for Wtf8 {
type Owned = Wtf8Buf;
#[inline]
fn to_owned(&self) -> Wtf8Buf {
Wtf8Buf {
bytes: self.bytes.to_vec(),
}
}
}
impl<'a> From<&'a Wtf8> for Cow<'a, Wtf8> {
#[inline]
fn from(s: &'a Wtf8) -> Cow<'a, Wtf8> {
Cow::Borrowed(s)
}
}
impl<'a> From<&'a str> for &'a Wtf8 {
#[inline]
fn from(s: &'a str) -> &'a Wtf8 {
Wtf8::from_str(s)
}
}
impl<'a> From<Wtf8Buf> for Cow<'a, Wtf8> {
#[inline]
fn from(s: Wtf8Buf) -> Cow<'a, Wtf8> {
Cow::Owned(s)
}
}
#[cfg(test)]
mod tests {
use alloc::{format, vec};
use core::mem::transmute;
use super::*;
#[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() {
let cp_a = CodePoint::from_char('a');
assert_eq!(format!("{cp_a:?}"), "U+0061");
let cp_poop = CodePoint::from_char('💩');
assert_eq!(format!("{cp_poop:?}"), "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(), '\u{FFFD}');
}
#[test]
fn wtf8buf_new() {
assert_eq!(Wtf8Buf::new().bytes, b"");
}
#[test]
fn wtf8buf_from_str() {
assert_eq!(Wtf8Buf::from_str("").bytes, b"");
assert_eq!(
Wtf8Buf::from_str("aé 💩").bytes,
b"a\xC3\xA9 \xF0\x9F\x92\xA9"
);
}
#[test]
fn wtf8buf_from_string() {
assert_eq!(Wtf8Buf::from_string(String::from("")).bytes, b"");
assert_eq!(
Wtf8Buf::from_string(String::from("aé 💩")).bytes,
b"a\xC3\xA9 \xF0\x9F\x92\xA9"
);
}
#[test]
fn wtf8buf_from_ill_formed_utf16() {
assert_eq!(Wtf8Buf::from_ill_formed_utf16(&[]).bytes, b"");
assert_eq!(
Wtf8Buf::from_ill_formed_utf16(&[0x61, 0xe9, 0x20, 0xd83d, 0xd83d, 0xdca9]).bytes,
b"a\xC3\xA9 \xED\xA0\xBD\xF0\x9F\x92\xA9"
);
}
#[test]
fn wtf8buf_push_str() {
let mut string = Wtf8Buf::new();
assert_eq!(string.bytes, b"");
string.push_str("aé 💩");
assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
}
#[test]
fn wtf8buf_push_char() {
let mut string = Wtf8Buf::from_str("aé ");
assert_eq!(string.bytes, b"a\xC3\xA9 ");
string.push_char('💩');
assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
}
#[test]
fn wtf8buf_push() {
let mut string = Wtf8Buf::from_str("aé ");
assert_eq!(string.bytes, b"a\xC3\xA9 ");
string.push(CodePoint::from_char('💩'));
assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
fn c(value: u32) -> CodePoint {
CodePoint::from_u32(value).unwrap()
}
let mut string = Wtf8Buf::new();
string.push(c(0xd83d)); string.push(c(0xdca9)); assert_eq!(string.bytes, b"\xF0\x9F\x92\xA9");
let mut string = Wtf8Buf::new();
string.push(c(0xd83d)); string.push(c(0x20)); string.push(c(0xdca9)); assert_eq!(string.bytes, b"\xED\xA0\xBD \xED\xB2\xA9");
let mut string = Wtf8Buf::new();
string.push(c(0xd800)); string.push(c(0xdbff)); assert_eq!(string.bytes, b"\xED\xA0\x80\xED\xAF\xBF");
let mut string = Wtf8Buf::new();
string.push(c(0xd800)); string.push(c(0xe000)); assert_eq!(string.bytes, b"\xED\xA0\x80\xEE\x80\x80");
let mut string = Wtf8Buf::new();
string.push(c(0xd7ff)); string.push(c(0xdc00)); assert_eq!(string.bytes, b"\xED\x9F\xBF\xED\xB0\x80");
let mut string = Wtf8Buf::new();
string.push(c(0x61)); string.push(c(0xdc00)); assert_eq!(string.bytes, b"\x61\xED\xB0\x80");
let mut string = Wtf8Buf::new();
string.push(c(0xdc00)); assert_eq!(string.bytes, b"\xED\xB0\x80");
}
#[test]
fn wtf8buf_push_wtf8() {
let mut string = Wtf8Buf::from_str("aé");
assert_eq!(string.bytes, b"a\xC3\xA9");
string.push_wtf8(Wtf8::from_str(" 💩"));
assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
fn w(value: &[u8]) -> &Wtf8 {
unsafe { transmute(value) }
}
let mut string = Wtf8Buf::new();
string.push_wtf8(w(b"\xED\xA0\xBD")); string.push_wtf8(w(b"\xED\xB2\xA9")); assert_eq!(string.bytes, b"\xF0\x9F\x92\xA9");
let mut string = Wtf8Buf::new();
string.push_wtf8(w(b"\xED\xA0\xBD")); string.push_wtf8(w(b" ")); string.push_wtf8(w(b"\xED\xB2\xA9")); assert_eq!(string.bytes, b"\xED\xA0\xBD \xED\xB2\xA9");
let mut string = Wtf8Buf::new();
string.push_wtf8(w(b"\xED\xA0\x80")); string.push_wtf8(w(b"\xED\xAF\xBF")); assert_eq!(string.bytes, b"\xED\xA0\x80\xED\xAF\xBF");
let mut string = Wtf8Buf::new();
string.push_wtf8(w(b"\xED\xA0\x80")); string.push_wtf8(w(b"\xEE\x80\x80")); assert_eq!(string.bytes, b"\xED\xA0\x80\xEE\x80\x80");
let mut string = Wtf8Buf::new();
string.push_wtf8(w(b"\xED\x9F\xBF")); string.push_wtf8(w(b"\xED\xB0\x80")); assert_eq!(string.bytes, b"\xED\x9F\xBF\xED\xB0\x80");
let mut string = Wtf8Buf::new();
string.push_wtf8(w(b"a")); string.push_wtf8(w(b"\xED\xB0\x80")); assert_eq!(string.bytes, b"\x61\xED\xB0\x80");
let mut string = Wtf8Buf::new();
string.push_wtf8(w(b"\xED\xB0\x80")); assert_eq!(string.bytes, b"\xED\xB0\x80");
}
#[test]
fn wtf8buf_truncate() {
let mut string = Wtf8Buf::from_str("aé");
string.truncate(1);
assert_eq!(string.bytes, b"a");
}
#[test]
#[should_panic]
fn wtf8buf_truncate_fail_code_point_boundary() {
let mut string = Wtf8Buf::from_str("aé");
string.truncate(2);
}
#[test]
#[should_panic]
fn wtf8buf_truncate_fail_longer() {
let mut string = Wtf8Buf::from_str("aé");
string.truncate(4);
}
#[test]
fn wtf8buf_into_string() {
let mut string = Wtf8Buf::from_str("aé 💩");
assert_eq!(string.clone().into_string(), Ok(String::from("aé 💩")));
string.push(CodePoint::from_u32(0xd800).unwrap());
assert_eq!(string.clone().into_string(), Err(string));
}
#[test]
fn wtf8buf_into_string_lossy() {
let mut string = Wtf8Buf::from_str("aé 💩");
assert_eq!(string.clone().into_string_lossy(), String::from("aé 💩"));
string.push(CodePoint::from_u32(0xd800).unwrap());
assert_eq!(string.clone().into_string_lossy(), String::from("aé 💩�"));
}
#[test]
fn wtf8buf_from_iterator() {
fn f(values: &[u32]) -> Wtf8Buf {
values
.iter()
.map(|&c| CodePoint::from_u32(c).unwrap())
.collect::<Wtf8Buf>()
}
assert_eq!(
f(&[0x61, 0xe9, 0x20, 0x1f4a9]).bytes,
b"a\xC3\xA9 \xF0\x9F\x92\xA9"
);
assert_eq!(f(&[0xd83d, 0xdca9]).bytes, b"\xF0\x9F\x92\xA9"); assert_eq!(
f(&[0xd83d, 0x20, 0xdca9]).bytes,
b"\xED\xA0\xBD \xED\xB2\xA9"
);
assert_eq!(f(&[0xd800, 0xdbff]).bytes, b"\xED\xA0\x80\xED\xAF\xBF");
assert_eq!(f(&[0xd800, 0xe000]).bytes, b"\xED\xA0\x80\xEE\x80\x80");
assert_eq!(f(&[0xd7ff, 0xdc00]).bytes, b"\xED\x9F\xBF\xED\xB0\x80");
assert_eq!(f(&[0x61, 0xdc00]).bytes, b"\x61\xED\xB0\x80");
assert_eq!(f(&[0xdc00]).bytes, b"\xED\xB0\x80");
}
#[test]
fn wtf8buf_extend() {
fn e(initial: &[u32], extended: &[u32]) -> Wtf8Buf {
fn c(value: &u32) -> CodePoint {
CodePoint::from_u32(*value).unwrap()
}
let mut string = initial.iter().map(c).collect::<Wtf8Buf>();
string.extend(extended.iter().map(c));
string
}
assert_eq!(
e(&[0x61, 0xe9], &[0x20, 0x1f4a9]).bytes,
b"a\xC3\xA9 \xF0\x9F\x92\xA9"
);
assert_eq!(e(&[0xd83d], &[0xdca9]).bytes, b"\xF0\x9F\x92\xA9"); assert_eq!(
e(&[0xd83d, 0x20], &[0xdca9]).bytes,
b"\xED\xA0\xBD \xED\xB2\xA9"
);
assert_eq!(e(&[0xd800], &[0xdbff]).bytes, b"\xED\xA0\x80\xED\xAF\xBF");
assert_eq!(e(&[0xd800], &[0xe000]).bytes, b"\xED\xA0\x80\xEE\x80\x80");
assert_eq!(e(&[0xd7ff], &[0xdc00]).bytes, b"\xED\x9F\xBF\xED\xB0\x80");
assert_eq!(e(&[0x61], &[0xdc00]).bytes, b"\x61\xED\xB0\x80");
assert_eq!(e(&[], &[0xdc00]).bytes, b"\xED\xB0\x80");
}
#[test]
fn wtf8buf_debug() {
let mut string = Wtf8Buf::from_str("aé 💩");
string.push(CodePoint::from_u32(0xd800).unwrap());
assert_eq!(format!("{string:?}"), r#""aé 💩\u{D800}""#);
}
#[test]
fn wtf8buf_as_slice() {
assert_eq!(Wtf8Buf::from_str("aé"), Wtf8::from_str("aé"));
}
#[test]
fn wtf8_debug() {
let mut string = Wtf8Buf::from_str("aé 💩");
string.push(CodePoint::from_u32(0xd800).unwrap());
let string_ref = &*string;
assert_eq!(format!("{string_ref:?}"), r#""aé 💩\u{D800}""#);
}
#[test]
fn wtf8_from_str() {
assert_eq!(&Wtf8::from_str("").bytes, b"");
assert_eq!(
&Wtf8::from_str("aé 💩").bytes,
b"a\xC3\xA9 \xF0\x9F\x92\xA9"
);
}
#[test]
fn wtf8_as_bytes() {
assert_eq!(Wtf8::from_str("").as_bytes(), b"");
assert_eq!(
Wtf8::from_str("aé 💩").as_bytes(),
b"a\xC3\xA9 \xF0\x9F\x92\xA9"
);
}
#[test]
fn wtf8_from_bytes_unchecked() {
assert_eq!(unsafe { &Wtf8::from_bytes_unchecked(b"").bytes }, b"");
assert_eq!(
unsafe { &Wtf8::from_bytes_unchecked(b"a\xC3\xA9 \xF0\x9F\x92\xA9").bytes },
b"a\xC3\xA9 \xF0\x9F\x92\xA9"
);
assert_eq!(
unsafe { Wtf8::from_bytes_unchecked(b"a\xC3\xA9 \xF0\x9F\x92\xA9") },
Wtf8::from_str("aé 💩")
)
}
#[test]
fn wtf8_cow() {
let s: Cow<Wtf8> = Cow::from(Wtf8::from_str("aé 💩"));
assert!(matches!(s, Cow::Borrowed(_)));
let owned: Wtf8Buf = s.into_owned();
assert_eq!(owned, Wtf8Buf::from_str("aé 💩"));
}
#[test]
fn wtf8_len() {
assert_eq!(Wtf8::from_str("").len(), 0);
assert_eq!(Wtf8::from_str("aé 💩").len(), 8);
}
#[test]
fn wtf8_slice() {
assert_eq!(&Wtf8::from_str("aé 💩").slice(1, 4).bytes, b"\xC3\xA9 ");
}
#[test]
#[should_panic]
fn wtf8_slice_not_code_point_boundary() {
Wtf8::from_str("aé 💩").slice(2, 4);
}
#[test]
fn wtf8_slice_from() {
assert_eq!(
&Wtf8::from_str("aé 💩").slice_from(1).bytes,
b"\xC3\xA9 \xF0\x9F\x92\xA9"
);
}
#[test]
#[should_panic]
fn wtf8_slice_from_not_code_point_boundary() {
Wtf8::from_str("aé 💩").slice_from(2);
}
#[test]
fn wtf8_slice_to() {
assert_eq!(&Wtf8::from_str("aé 💩").slice_to(4).bytes, b"a\xC3\xA9 ");
}
#[test]
#[should_panic]
fn wtf8_slice_to_not_code_point_boundary() {
Wtf8::from_str("aé 💩").slice_from(5);
}
#[test]
fn wtf8_ascii_byte_at() {
let slice = Wtf8::from_str("aé 💩");
assert_eq!(slice.ascii_byte_at(0), b'a');
assert_eq!(slice.ascii_byte_at(1), b'\xFF');
assert_eq!(slice.ascii_byte_at(2), b'\xFF');
assert_eq!(slice.ascii_byte_at(3), b' ');
assert_eq!(slice.ascii_byte_at(4), b'\xFF');
}
#[test]
fn wtf8_code_points() {
fn c(value: u32) -> CodePoint {
CodePoint::from_u32(value).unwrap()
}
fn cp(string: &Wtf8Buf) -> Vec<Option<char>> {
string
.code_points()
.map(|c| c.to_char())
.collect::<Vec<_>>()
}
let mut string = Wtf8Buf::from_str("é ");
assert_eq!(cp(&string), vec![Some('é'), Some(' ')]);
string.push(c(0xd83d));
assert_eq!(cp(&string), vec![Some('é'), Some(' '), None]);
string.push(c(0xdca9));
assert_eq!(cp(&string), vec![Some('é'), Some(' '), Some('💩')]);
}
#[test]
fn wtf8_as_str() {
assert_eq!(Wtf8::from_str("").as_str(), Some(""));
assert_eq!(Wtf8::from_str("aé 💩").as_str(), Some("aé 💩"));
let mut string = Wtf8Buf::new();
string.push(CodePoint::from_u32(0xd800).unwrap());
assert_eq!(string.as_str(), None);
}
#[test]
fn wtf8_to_string_lossy() {
assert_eq!(Wtf8::from_str("").to_string_lossy(), Cow::Borrowed(""));
assert_eq!(
Wtf8::from_str("aé 💩").to_string_lossy(),
Cow::Borrowed("aé 💩")
);
let mut string = Wtf8Buf::from_str("aé 💩");
string.push(CodePoint::from_u32(0xd800).unwrap());
assert_eq!(string.to_string_lossy(), {
let o: Cow<str> = Cow::Owned(String::from("aé 💩�"));
o
});
}
#[test]
fn wtf8_to_ill_formed_utf16() {
let mut string = Wtf8Buf::from_str("aé ");
string.push(CodePoint::from_u32(0xd83d).unwrap());
string.push_char('💩');
assert_eq!(
string.to_ill_formed_utf16().collect::<Vec<_>>(),
vec![0x61, 0xe9, 0x20, 0xd83d, 0xd83d, 0xdca9]
);
}
#[test]
fn wtf8buf_wtf8_from_bytes_valid() {
assert!(Wtf8Buf::from_bytes(b"hello".to_vec()).is_ok());
assert!(Wtf8Buf::from_bytes(b"a\xC3\xA9 \xF0\x9F\x92\xA9".to_vec()).is_ok());
assert!(Wtf8::from_bytes(b"hello").is_ok());
assert!(Wtf8::from_bytes(b"a\xC3\xA9 \xF0\x9F\x92\xA9").is_ok());
assert!(Wtf8Buf::from_bytes(b"\xED\xA0\x80".to_vec()).is_ok()); assert!(Wtf8Buf::from_bytes(b"\xED\xB0\x80".to_vec()).is_ok()); assert!(Wtf8Buf::from_bytes(b"a\xED\xA0\xBD".to_vec()).is_ok()); assert!(Wtf8Buf::from_bytes(b"\xED\xB2\xA9z".to_vec()).is_ok()); assert!(Wtf8Buf::from_bytes(b"\xED\xB2\xA9\xED\xA0\xBD".to_vec()).is_ok());
}
#[test]
fn wtf8buf_wtf8_from_bytes_invalid() {
assert!(Wtf8Buf::from_bytes(b"\xED\xA0\x80\xED\xB0\x80".to_vec()).is_err());
assert!(Wtf8Buf::from_bytes(b"\xED\xA0\xBD\xED\xB2\xA9".to_vec()).is_err());
assert!(Wtf8::from_bytes(b"\xED\xA0\x80\xED\xB0\x80").is_err());
assert!(Wtf8::from_bytes(b"\xED\xA0\xBD\xED\xB2\xA9").is_err());
assert!(Wtf8Buf::from_bytes(vec![0xff]).is_err());
assert!(Wtf8Buf::from_bytes(vec![0xc0, 0x80]).is_err()); assert!(Wtf8Buf::from_bytes(vec![0xed, 0xa0]).is_err()); assert!(Wtf8Buf::from_bytes(vec![0xf4, 0x90, 0x80, 0x80]).is_err());
let original = vec![0xff, 0xfe];
let result = Wtf8Buf::from_bytes(original.clone());
assert_eq!(result.unwrap_err(), original);
let result = Wtf8::from_bytes(&original);
assert_eq!(result.unwrap_err(), original);
}
}