1extern crate alloc;
23
24use alloc::{
25 borrow::{Borrow, Cow},
26 string::String,
27 vec::Vec,
28};
29use core::{
30 cmp::Ordering,
31 fmt, hash,
32 iter::{FromIterator, IntoIterator},
33 mem::transmute,
34 ops::Deref,
35 slice, str,
36 str::FromStr,
37};
38use std::ops::Add;
39
40mod not_quite_std;
41
42static UTF8_REPLACEMENT_CHARACTER: &[u8] = b"\xEF\xBF\xBD";
43
44#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
50pub struct CodePoint {
51 value: u32,
52}
53
54impl Copy for CodePoint {}
55
56impl fmt::Debug for CodePoint {
59 #[inline]
60 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
61 write!(formatter, "U+{:04X}", self.value)
62 }
63}
64
65impl CodePoint {
66 #[inline]
72 pub const unsafe fn from_u32_unchecked(value: u32) -> CodePoint {
73 CodePoint { value }
74 }
75
76 #[inline]
80 pub const fn from_u32(value: u32) -> Option<CodePoint> {
81 match value {
82 0..=0x10ffff => Some(CodePoint { value }),
83 _ => None,
84 }
85 }
86
87 #[inline]
91 pub const fn from_char(value: char) -> CodePoint {
92 CodePoint {
93 value: value as u32,
94 }
95 }
96
97 #[inline]
99 pub fn to_u32(&self) -> u32 {
100 self.value
101 }
102
103 #[inline]
107 pub fn to_char(&self) -> Option<char> {
108 match self.value {
109 0xd800..=0xdfff => None,
110 _ => Some(unsafe { char::from_u32_unchecked(self.value) }),
111 }
112 }
113
114 #[inline]
119 pub fn to_char_lossy(&self) -> char {
120 self.to_char().unwrap_or('\u{FFFD}')
121 }
122
123 #[inline]
125 pub fn is_ascii(&self) -> bool {
126 self.value <= 0x7f
127 }
128}
129
130impl PartialEq<char> for CodePoint {
131 fn eq(&self, other: &char) -> bool {
132 self.value == *other as u32
133 }
134}
135
136#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
141pub struct Wtf8Buf {
142 bytes: Vec<u8>,
143}
144
145impl Deref for Wtf8Buf {
146 type Target = Wtf8;
147
148 fn deref(&self) -> &Wtf8 {
149 unsafe { transmute(&*self.bytes) }
150 }
151}
152
153impl fmt::Debug for Wtf8Buf {
157 #[inline]
158 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
159 Wtf8::fmt(self, formatter)
160 }
161}
162
163impl Default for Wtf8Buf {
164 #[inline]
165 fn default() -> Self {
166 Self::new()
167 }
168}
169
170impl FromStr for Wtf8Buf {
171 type Err = core::convert::Infallible;
172
173 #[inline]
174 fn from_str(s: &str) -> Result<Self, Self::Err> {
175 Ok(Wtf8Buf {
176 bytes: s.as_bytes().to_vec(),
177 })
178 }
179}
180
181impl fmt::Write for Wtf8Buf {
182 fn write_str(&mut self, s: &str) -> std::fmt::Result {
183 self.push_str(s);
184 Ok(())
185 }
186}
187
188impl Add<&Wtf8> for Wtf8Buf {
189 type Output = Wtf8Buf;
190
191 fn add(self, rhs: &Wtf8) -> Self::Output {
192 let mut result = self;
193 result.push_wtf8(rhs);
194 result
195 }
196}
197
198impl Wtf8Buf {
199 #[inline]
201 pub fn new() -> Wtf8Buf {
202 Wtf8Buf { bytes: Vec::new() }
203 }
204
205 #[inline]
208 pub fn with_capacity(n: usize) -> Wtf8Buf {
209 Wtf8Buf {
210 bytes: Vec::with_capacity(n),
211 }
212 }
213
214 #[inline]
220 pub fn from_string(string: String) -> Wtf8Buf {
221 Wtf8Buf {
222 bytes: string.into_bytes(),
223 }
224 }
225
226 #[inline]
232 #[allow(clippy::should_implement_trait)]
233 pub fn from_str(s: &str) -> Wtf8Buf {
234 Wtf8Buf {
235 bytes: s.as_bytes().to_vec(),
236 }
237 }
238
239 pub fn from_ill_formed_utf16(v: &[u16]) -> Wtf8Buf {
245 let mut string = Wtf8Buf::with_capacity(v.len());
246 for item in not_quite_std::decode_utf16(v.iter().cloned()) {
247 match item {
248 Ok(c) => string.push_char(c),
249 Err(s) => {
250 let code_point = unsafe { CodePoint::from_u32_unchecked(s as u32) };
252 not_quite_std::push_code_point(&mut string, code_point)
255 }
256 }
257 }
258 string
259 }
260
261 #[inline]
269 pub fn reserve(&mut self, additional: usize) {
270 self.bytes.reserve(additional)
271 }
272
273 #[inline]
276 pub fn capacity(&self) -> usize {
277 self.bytes.capacity()
278 }
279
280 #[inline]
282 pub fn push_str(&mut self, other: &str) {
283 self.bytes.extend_from_slice(other.as_bytes())
284 }
285
286 #[inline]
292 pub fn push_wtf8(&mut self, other: &Wtf8) {
293 match (self.final_lead_surrogate(), other.initial_trail_surrogate()) {
294 (Some(lead), Some(trail)) => {
296 let len_without_lead_surrogate = self.len() - 3;
297 self.bytes.truncate(len_without_lead_surrogate);
298 let other_without_trail_surrogate = &other.bytes[3..];
299 self.bytes.reserve(4 + other_without_trail_surrogate.len());
301 self.push_char(decode_surrogate_pair(lead, trail));
302 self.bytes.extend_from_slice(other_without_trail_surrogate);
303 }
304 _ => self.bytes.extend_from_slice(&other.bytes),
305 }
306 }
307
308 #[inline]
310 pub fn push_char(&mut self, c: char) {
311 not_quite_std::push_code_point(self, CodePoint::from_char(c))
312 }
313
314 #[inline]
320 pub fn push(&mut self, code_point: CodePoint) {
321 if let trail @ 0xdc00..=0xdfff = code_point.to_u32() {
322 if let Some(lead) = self.final_lead_surrogate() {
323 let len_without_lead_surrogate = self.len() - 3;
324 self.bytes.truncate(len_without_lead_surrogate);
325 self.push_char(decode_surrogate_pair(lead, trail as u16));
326 return;
327 }
328 }
329
330 not_quite_std::push_code_point(self, code_point)
332 }
333
334 #[inline]
341 pub fn truncate(&mut self, new_len: usize) {
342 assert!(not_quite_std::is_code_point_boundary(self, new_len));
343 self.bytes.truncate(new_len)
344 }
345
346 #[inline]
348 pub fn clear(&mut self) {
349 self.bytes.clear();
350 }
351
352 pub fn into_string(self) -> Result<String, Wtf8Buf> {
360 match self.next_surrogate(0) {
361 None => Ok(unsafe { String::from_utf8_unchecked(self.bytes) }),
362 Some(_) => Err(self),
363 }
364 }
365
366 pub fn into_string_lossy(mut self) -> String {
373 let mut pos = 0;
374 loop {
375 match self.next_surrogate(pos) {
376 Some((surrogate_pos, _)) => {
377 pos = surrogate_pos + 3;
378 self.bytes[surrogate_pos..pos].copy_from_slice(UTF8_REPLACEMENT_CHARACTER);
379 }
380 None => return unsafe { String::from_utf8_unchecked(self.bytes) },
381 }
382 }
383 }
384
385 pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, Vec<u8>> {
399 if not_quite_std::validate_wtf8(&bytes) {
400 Ok(Self { bytes })
401 } else {
402 Err(bytes)
403 }
404 }
405
406 #[inline]
416 pub unsafe fn from_bytes_unchecked(bytes: Vec<u8>) -> Self {
417 Self { bytes }
418 }
419}
420
421impl FromIterator<CodePoint> for Wtf8Buf {
426 fn from_iter<T: IntoIterator<Item = CodePoint>>(iterable: T) -> Wtf8Buf {
427 let mut string = Wtf8Buf::new();
428 string.extend(iterable);
429 string
430 }
431}
432
433impl Extend<CodePoint> for Wtf8Buf {
438 fn extend<T: IntoIterator<Item = CodePoint>>(&mut self, iterable: T) {
439 let iterator = iterable.into_iter();
440 let (low, _high) = iterator.size_hint();
441 self.bytes.reserve(low);
443 for code_point in iterator {
444 self.push(code_point);
445 }
446 }
447}
448
449#[repr(transparent)]
454pub struct Wtf8 {
455 bytes: [u8],
456}
457
458impl PartialEq for Wtf8 {
460 fn eq(&self, other: &Wtf8) -> bool {
461 self.bytes.eq(&other.bytes)
462 }
463}
464
465impl Eq for Wtf8 {}
467
468impl PartialOrd for Wtf8 {
470 #[inline]
471 fn partial_cmp(&self, other: &Wtf8) -> Option<Ordering> {
472 Some(self.bytes.cmp(&other.bytes))
473 }
474
475 #[inline]
476 fn lt(&self, other: &Wtf8) -> bool {
477 self.bytes.lt(&other.bytes)
478 }
479
480 #[inline]
481 fn le(&self, other: &Wtf8) -> bool {
482 self.bytes.le(&other.bytes)
483 }
484
485 #[inline]
486 fn gt(&self, other: &Wtf8) -> bool {
487 self.bytes.gt(&other.bytes)
488 }
489
490 #[inline]
491 fn ge(&self, other: &Wtf8) -> bool {
492 self.bytes.ge(&other.bytes)
493 }
494}
495
496impl Ord for Wtf8 {
498 #[inline]
499 fn cmp(&self, other: &Wtf8) -> Ordering {
500 self.bytes.cmp(&other.bytes)
501 }
502}
503
504impl fmt::Debug for Wtf8 {
508 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
509 formatter.write_str("\"")?;
510 let mut pos = 0;
511 loop {
512 match self.next_surrogate(pos) {
513 None => break,
514 Some((surrogate_pos, surrogate)) => {
515 formatter.write_str(unsafe {
516 str::from_utf8_unchecked(&self.bytes[pos..surrogate_pos])
517 })?;
518 write!(formatter, "\\u{{{surrogate:X}}}")?;
519 pos = surrogate_pos + 3;
520 }
521 }
522 }
523 formatter.write_str(unsafe { str::from_utf8_unchecked(&self.bytes[pos..]) })?;
524 formatter.write_str("\"")
525 }
526}
527
528impl Wtf8 {
529 #[inline]
533 pub const fn from_str(value: &str) -> &Wtf8 {
534 unsafe { transmute(value.as_bytes()) }
535 }
536
537 #[inline]
539 pub const fn len(&self) -> usize {
540 self.bytes.len()
541 }
542
543 #[inline]
545 pub const fn is_empty(&self) -> bool {
546 self.bytes.is_empty()
547 }
548
549 #[inline]
551 pub const fn is_ascii(&self) -> bool {
552 self.bytes.is_ascii()
553 }
554
555 #[inline]
562 pub fn slice(&self, begin: usize, end: usize) -> &Wtf8 {
563 if begin <= end
565 && not_quite_std::is_code_point_boundary(self, begin)
566 && not_quite_std::is_code_point_boundary(self, end)
567 {
568 unsafe { not_quite_std::slice_unchecked(self, begin, end) }
569 } else {
570 not_quite_std::slice_error_fail(self, begin, end)
571 }
572 }
573
574 #[inline]
581 pub fn slice_from(&self, begin: usize) -> &Wtf8 {
582 if not_quite_std::is_code_point_boundary(self, begin) {
584 unsafe { not_quite_std::slice_unchecked(self, begin, self.len()) }
585 } else {
586 not_quite_std::slice_error_fail(self, begin, self.len())
587 }
588 }
589
590 #[inline]
597 pub fn slice_to(&self, end: usize) -> &Wtf8 {
598 if not_quite_std::is_code_point_boundary(self, end) {
600 unsafe { not_quite_std::slice_unchecked(self, 0, end) }
601 } else {
602 not_quite_std::slice_error_fail(self, 0, end)
603 }
604 }
605
606 #[inline]
613 pub fn ascii_byte_at(&self, position: usize) -> u8 {
614 match self.bytes[position] {
615 ascii_byte @ 0x00..=0x7f => ascii_byte,
616 _ => 0xff,
617 }
618 }
619
620 #[inline]
622 pub fn code_points(&self) -> Wtf8CodePoints {
623 Wtf8CodePoints {
624 bytes: self.bytes.iter(),
625 }
626 }
627
628 #[inline]
630 pub fn contains_char(&self, ch: char) -> bool {
631 let target = CodePoint::from_char(ch);
632 self.contains(target)
633 }
634
635 #[inline]
637 pub fn contains(&self, code_point: CodePoint) -> bool {
638 self.code_points().any(|cp| cp == code_point)
639 }
640
641 #[inline]
643 pub fn starts_with(&self, pattern: &str) -> bool {
644 if pattern.len() > self.len() {
645 return false;
646 }
647
648 let pattern_wtf8 = self.slice_to(pattern.len());
649 if let Some(pattern_str) = pattern_wtf8.as_str() {
650 pattern_str == pattern
651 } else {
652 false
653 }
654 }
655
656 #[inline]
662 pub fn as_str(&self) -> Option<&str> {
663 match self.next_surrogate(0) {
666 None => Some(unsafe { str::from_utf8_unchecked(&self.bytes) }),
667 Some(_) => None,
668 }
669 }
670
671 #[inline]
673 pub const fn as_bytes(&self) -> &[u8] {
674 &self.bytes
675 }
676
677 pub fn to_string_lossy(&self) -> Cow<str> {
685 let surrogate_pos = match self.next_surrogate(0) {
686 None => return Cow::Borrowed(unsafe { str::from_utf8_unchecked(&self.bytes) }),
687 Some((pos, _)) => pos,
688 };
689 let wtf8_bytes = &self.bytes;
690 let mut utf8_bytes = Vec::with_capacity(self.len());
691 utf8_bytes.extend_from_slice(&wtf8_bytes[..surrogate_pos]);
692 utf8_bytes.extend_from_slice(UTF8_REPLACEMENT_CHARACTER);
693 let mut pos = surrogate_pos + 3;
694 loop {
695 match self.next_surrogate(pos) {
696 Some((surrogate_pos, _)) => {
697 utf8_bytes.extend_from_slice(&wtf8_bytes[pos..surrogate_pos]);
698 utf8_bytes.extend_from_slice(UTF8_REPLACEMENT_CHARACTER);
699 pos = surrogate_pos + 3;
700 }
701 None => {
702 utf8_bytes.extend_from_slice(&wtf8_bytes[pos..]);
703 return Cow::Owned(unsafe { String::from_utf8_unchecked(utf8_bytes) });
704 }
705 }
706 }
707 }
708
709 #[inline]
716 pub fn to_ill_formed_utf16(&self) -> IllFormedUtf16CodeUnits {
717 IllFormedUtf16CodeUnits {
718 code_points: self.code_points(),
719 extra: 0,
720 }
721 }
722
723 #[inline]
725 pub fn to_uppercase(&self) -> Wtf8Buf {
726 let mut result = Wtf8Buf::with_capacity(self.len());
727 for cp in self.code_points() {
728 if let Some(ch) = cp.to_char() {
729 for upper_ch in ch.to_uppercase() {
730 result.push_char(upper_ch);
731 }
732 } else {
733 let code_point = unsafe { CodePoint::from_u32_unchecked(cp.to_u32()) };
735 not_quite_std::push_code_point(&mut result, code_point)
738 }
739 }
740 result
741 }
742
743 #[inline]
745 pub fn to_lowercase(&self) -> Wtf8Buf {
746 let mut result = Wtf8Buf::with_capacity(self.len());
747 for cp in self.code_points() {
748 if let Some(ch) = cp.to_char() {
749 for lower_ch in ch.to_lowercase() {
750 result.push_char(lower_ch);
751 }
752 } else {
753 let code_point = unsafe { CodePoint::from_u32_unchecked(cp.to_u32()) };
755 not_quite_std::push_code_point(&mut result, code_point)
758 }
759 }
760 result
761 }
762
763 pub fn from_bytes(bytes: &[u8]) -> Result<&Wtf8, &[u8]> {
777 if not_quite_std::validate_wtf8(bytes) {
778 Ok(unsafe { transmute::<&[u8], &Wtf8>(bytes) })
779 } else {
780 Err(bytes)
781 }
782 }
783
784 #[inline]
794 pub const unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Wtf8 {
795 unsafe { transmute(bytes) }
796 }
797
798 #[inline]
799 fn next_surrogate(&self, mut pos: usize) -> Option<(usize, u16)> {
800 let mut iter = self.bytes[pos..].iter();
801 loop {
802 let b = match iter.next() {
803 None => return None,
804 Some(&b) => b,
805 };
806 if b < 0x80 {
807 pos += 1;
808 } else if b < 0xe0 {
809 iter.next();
810 pos += 2;
811 } else if b == 0xed {
812 match (iter.next(), iter.next()) {
813 (Some(&b2), Some(&b3)) if b2 >= 0xa0 => {
814 return Some((pos, decode_surrogate(b2, b3)))
815 }
816 _ => pos += 3,
817 }
818 } else if b < 0xf0 {
819 iter.next();
820 iter.next();
821 pos += 3;
822 } else {
823 iter.next();
824 iter.next();
825 iter.next();
826 pos += 4;
827 }
828 }
829 }
830
831 #[inline]
832 fn final_lead_surrogate(&self) -> Option<u16> {
833 let len = self.len();
834 if len < 3 {
835 return None;
836 }
837 let seq = &self.bytes[len - 3..];
838 if seq[0] == 0xed && 0xa0 <= seq[1] && seq[1] <= 0xaf {
839 Some(decode_surrogate(seq[1], seq[2]))
840 } else {
841 None
842 }
843 }
844
845 #[inline]
846 fn initial_trail_surrogate(&self) -> Option<u16> {
847 let len = self.len();
848 if len < 3 {
849 return None;
850 }
851 let seq = &self.bytes[..3];
852 if seq[0] == 0xed && 0xb0 <= seq[1] && seq[1] <= 0xbf {
853 Some(decode_surrogate(seq[1], seq[2]))
854 } else {
855 None
856 }
857 }
858}
859
860#[inline]
861fn decode_surrogate(second_byte: u8, third_byte: u8) -> u16 {
862 0xd800 | (second_byte as u16 & 0x3f) << 6 | third_byte as u16 & 0x3f
864}
865
866#[inline]
867fn decode_surrogate_pair(lead: u16, trail: u16) -> char {
868 let code_point = 0x10000 + (((lead as u32 - 0xd800) << 10) | (trail as u32 - 0xdc00));
869 unsafe { char::from_u32_unchecked(code_point) }
870}
871
872#[derive(Clone)]
876pub struct Wtf8CodePoints<'a> {
877 bytes: slice::Iter<'a, u8>,
878}
879
880impl<'a> Iterator for Wtf8CodePoints<'a> {
881 type Item = CodePoint;
882
883 #[inline]
884 fn next(&mut self) -> Option<CodePoint> {
885 not_quite_std::next_code_point(&mut self.bytes).map(|value| {
886 unsafe { CodePoint::from_u32_unchecked(value) }
888 })
889 }
890
891 #[inline]
892 fn size_hint(&self) -> (usize, Option<usize>) {
893 let (len, _) = self.bytes.size_hint();
894 (len.saturating_add(3) / 4, Some(len))
895 }
896}
897
898#[derive(Clone)]
899pub struct IllFormedUtf16CodeUnits<'a> {
900 code_points: Wtf8CodePoints<'a>,
901 extra: u16,
902}
903
904impl<'a> Iterator for IllFormedUtf16CodeUnits<'a> {
905 type Item = u16;
906
907 #[inline]
908 fn next(&mut self) -> Option<u16> {
909 not_quite_std::next_utf16_code_unit(self)
910 }
911
912 #[inline]
913 fn size_hint(&self) -> (usize, Option<usize>) {
914 let (low, high) = self.code_points.size_hint();
915 (low, high.and_then(|n| n.checked_mul(2)))
919 }
920}
921
922impl PartialEq<&Wtf8> for Wtf8Buf {
923 fn eq(&self, other: &&Wtf8) -> bool {
924 **self == **other
925 }
926}
927
928impl PartialEq<Wtf8Buf> for &Wtf8 {
929 fn eq(&self, other: &Wtf8Buf) -> bool {
930 **self == **other
931 }
932}
933
934impl PartialEq<str> for &Wtf8 {
935 fn eq(&self, other: &str) -> bool {
936 match self.as_str() {
937 Some(s) => s == other,
938 None => false,
939 }
940 }
941}
942
943impl PartialEq<&str> for &Wtf8 {
944 fn eq(&self, other: &&str) -> bool {
945 match self.as_str() {
946 Some(s) => s == *other,
947 None => false,
948 }
949 }
950}
951
952impl hash::Hash for CodePoint {
953 #[inline]
954 fn hash<H: hash::Hasher>(&self, state: &mut H) {
955 self.value.hash(state)
956 }
957}
958
959impl hash::Hash for Wtf8Buf {
960 #[inline]
961 fn hash<H: hash::Hasher>(&self, state: &mut H) {
962 Wtf8::hash(self, state)
963 }
964}
965
966impl hash::Hash for Wtf8 {
967 #[inline]
968 fn hash<H: hash::Hasher>(&self, state: &mut H) {
969 state.write(&self.bytes);
970 0xfeu8.hash(state)
971 }
972}
973
974impl Borrow<Wtf8> for Wtf8Buf {
975 #[inline]
976 fn borrow(&self) -> &Wtf8 {
977 self
978 }
979}
980
981impl ToOwned for Wtf8 {
982 type Owned = Wtf8Buf;
983
984 #[inline]
985 fn to_owned(&self) -> Wtf8Buf {
986 Wtf8Buf {
987 bytes: self.bytes.to_vec(),
988 }
989 }
990}
991
992impl<'a> From<&'a Wtf8> for Cow<'a, Wtf8> {
993 #[inline]
994 fn from(s: &'a Wtf8) -> Cow<'a, Wtf8> {
995 Cow::Borrowed(s)
996 }
997}
998
999impl<'a> From<&'a str> for &'a Wtf8 {
1000 #[inline]
1001 fn from(s: &'a str) -> &'a Wtf8 {
1002 Wtf8::from_str(s)
1003 }
1004}
1005
1006impl<'a> From<Wtf8Buf> for Cow<'a, Wtf8> {
1007 #[inline]
1008 fn from(s: Wtf8Buf) -> Cow<'a, Wtf8> {
1009 Cow::Owned(s)
1010 }
1011}
1012
1013#[cfg(test)]
1014mod tests {
1015 use alloc::{format, vec};
1016 use core::mem::transmute;
1017
1018 use super::*;
1019
1020 #[test]
1021 fn code_point_from_u32() {
1022 assert!(CodePoint::from_u32(0).is_some());
1023 assert!(CodePoint::from_u32(0xd800).is_some());
1024 assert!(CodePoint::from_u32(0x10ffff).is_some());
1025 assert!(CodePoint::from_u32(0x110000).is_none());
1026 }
1027
1028 #[test]
1029 fn code_point_to_u32() {
1030 fn c(value: u32) -> CodePoint {
1031 CodePoint::from_u32(value).unwrap()
1032 }
1033 assert_eq!(c(0).to_u32(), 0);
1034 assert_eq!(c(0xd800).to_u32(), 0xd800);
1035 assert_eq!(c(0x10ffff).to_u32(), 0x10ffff);
1036 }
1037
1038 #[test]
1039 fn code_point_from_char() {
1040 assert_eq!(CodePoint::from_char('a').to_u32(), 0x61);
1041 assert_eq!(CodePoint::from_char('💩').to_u32(), 0x1f4a9);
1042 }
1043
1044 #[test]
1045 fn code_point_to_string() {
1046 let cp_a = CodePoint::from_char('a');
1047 assert_eq!(format!("{cp_a:?}"), "U+0061");
1048 let cp_poop = CodePoint::from_char('💩');
1049 assert_eq!(format!("{cp_poop:?}"), "U+1F4A9");
1050 }
1051
1052 #[test]
1053 fn code_point_to_char() {
1054 fn c(value: u32) -> CodePoint {
1055 CodePoint::from_u32(value).unwrap()
1056 }
1057 assert_eq!(c(0x61).to_char(), Some('a'));
1058 assert_eq!(c(0x1f4a9).to_char(), Some('💩'));
1059 assert_eq!(c(0xd800).to_char(), None);
1060 }
1061
1062 #[test]
1063 fn code_point_to_char_lossy() {
1064 fn c(value: u32) -> CodePoint {
1065 CodePoint::from_u32(value).unwrap()
1066 }
1067 assert_eq!(c(0x61).to_char_lossy(), 'a');
1068 assert_eq!(c(0x1f4a9).to_char_lossy(), '💩');
1069 assert_eq!(c(0xd800).to_char_lossy(), '\u{FFFD}');
1070 }
1071
1072 #[test]
1073 fn wtf8buf_new() {
1074 assert_eq!(Wtf8Buf::new().bytes, b"");
1075 }
1076
1077 #[test]
1078 fn wtf8buf_from_str() {
1079 assert_eq!(Wtf8Buf::from_str("").bytes, b"");
1080 assert_eq!(
1081 Wtf8Buf::from_str("aé 💩").bytes,
1082 b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1083 );
1084 }
1085
1086 #[test]
1087 fn wtf8buf_from_string() {
1088 assert_eq!(Wtf8Buf::from_string(String::from("")).bytes, b"");
1089 assert_eq!(
1090 Wtf8Buf::from_string(String::from("aé 💩")).bytes,
1091 b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1092 );
1093 }
1094
1095 #[test]
1096 fn wtf8buf_from_ill_formed_utf16() {
1097 assert_eq!(Wtf8Buf::from_ill_formed_utf16(&[]).bytes, b"");
1098 assert_eq!(
1099 Wtf8Buf::from_ill_formed_utf16(&[0x61, 0xe9, 0x20, 0xd83d, 0xd83d, 0xdca9]).bytes,
1100 b"a\xC3\xA9 \xED\xA0\xBD\xF0\x9F\x92\xA9"
1101 );
1102 }
1103
1104 #[test]
1105 fn wtf8buf_push_str() {
1106 let mut string = Wtf8Buf::new();
1107 assert_eq!(string.bytes, b"");
1108 string.push_str("aé 💩");
1109 assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
1110 }
1111
1112 #[test]
1113 fn wtf8buf_push_char() {
1114 let mut string = Wtf8Buf::from_str("aé ");
1115 assert_eq!(string.bytes, b"a\xC3\xA9 ");
1116 string.push_char('💩');
1117 assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
1118 }
1119
1120 #[test]
1121 fn wtf8buf_push() {
1122 let mut string = Wtf8Buf::from_str("aé ");
1123 assert_eq!(string.bytes, b"a\xC3\xA9 ");
1124 string.push(CodePoint::from_char('💩'));
1125 assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
1126
1127 fn c(value: u32) -> CodePoint {
1128 CodePoint::from_u32(value).unwrap()
1129 }
1130
1131 let mut string = Wtf8Buf::new();
1132 string.push(c(0xd83d)); string.push(c(0xdca9)); assert_eq!(string.bytes, b"\xF0\x9F\x92\xA9"); let mut string = Wtf8Buf::new();
1137 string.push(c(0xd83d)); string.push(c(0x20)); string.push(c(0xdca9)); assert_eq!(string.bytes, b"\xED\xA0\xBD \xED\xB2\xA9");
1141
1142 let mut string = Wtf8Buf::new();
1143 string.push(c(0xd800)); string.push(c(0xdbff)); assert_eq!(string.bytes, b"\xED\xA0\x80\xED\xAF\xBF");
1146
1147 let mut string = Wtf8Buf::new();
1148 string.push(c(0xd800)); string.push(c(0xe000)); assert_eq!(string.bytes, b"\xED\xA0\x80\xEE\x80\x80");
1151
1152 let mut string = Wtf8Buf::new();
1153 string.push(c(0xd7ff)); string.push(c(0xdc00)); assert_eq!(string.bytes, b"\xED\x9F\xBF\xED\xB0\x80");
1156
1157 let mut string = Wtf8Buf::new();
1158 string.push(c(0x61)); string.push(c(0xdc00)); assert_eq!(string.bytes, b"\x61\xED\xB0\x80");
1161
1162 let mut string = Wtf8Buf::new();
1163 string.push(c(0xdc00)); assert_eq!(string.bytes, b"\xED\xB0\x80");
1165 }
1166
1167 #[test]
1168 fn wtf8buf_push_wtf8() {
1169 let mut string = Wtf8Buf::from_str("aé");
1170 assert_eq!(string.bytes, b"a\xC3\xA9");
1171 string.push_wtf8(Wtf8::from_str(" 💩"));
1172 assert_eq!(string.bytes, b"a\xC3\xA9 \xF0\x9F\x92\xA9");
1173
1174 fn w(value: &[u8]) -> &Wtf8 {
1175 unsafe { transmute(value) }
1176 }
1177
1178 let mut string = Wtf8Buf::new();
1179 string.push_wtf8(w(b"\xED\xA0\xBD")); string.push_wtf8(w(b"\xED\xB2\xA9")); assert_eq!(string.bytes, b"\xF0\x9F\x92\xA9"); let mut string = Wtf8Buf::new();
1184 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");
1188
1189 let mut string = Wtf8Buf::new();
1190 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");
1193
1194 let mut string = Wtf8Buf::new();
1195 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");
1198
1199 let mut string = Wtf8Buf::new();
1200 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");
1203
1204 let mut string = Wtf8Buf::new();
1205 string.push_wtf8(w(b"a")); string.push_wtf8(w(b"\xED\xB0\x80")); assert_eq!(string.bytes, b"\x61\xED\xB0\x80");
1208
1209 let mut string = Wtf8Buf::new();
1210 string.push_wtf8(w(b"\xED\xB0\x80")); assert_eq!(string.bytes, b"\xED\xB0\x80");
1212 }
1213
1214 #[test]
1215 fn wtf8buf_truncate() {
1216 let mut string = Wtf8Buf::from_str("aé");
1217 string.truncate(1);
1218 assert_eq!(string.bytes, b"a");
1219 }
1220
1221 #[test]
1222 #[should_panic]
1223 fn wtf8buf_truncate_fail_code_point_boundary() {
1224 let mut string = Wtf8Buf::from_str("aé");
1225 string.truncate(2);
1226 }
1227
1228 #[test]
1229 #[should_panic]
1230 fn wtf8buf_truncate_fail_longer() {
1231 let mut string = Wtf8Buf::from_str("aé");
1232 string.truncate(4);
1233 }
1234
1235 #[test]
1236 fn wtf8buf_into_string() {
1237 let mut string = Wtf8Buf::from_str("aé 💩");
1238 assert_eq!(string.clone().into_string(), Ok(String::from("aé 💩")));
1239 string.push(CodePoint::from_u32(0xd800).unwrap());
1240 assert_eq!(string.clone().into_string(), Err(string));
1241 }
1242
1243 #[test]
1244 fn wtf8buf_into_string_lossy() {
1245 let mut string = Wtf8Buf::from_str("aé 💩");
1246 assert_eq!(string.clone().into_string_lossy(), String::from("aé 💩"));
1247 string.push(CodePoint::from_u32(0xd800).unwrap());
1248 assert_eq!(string.clone().into_string_lossy(), String::from("aé 💩�"));
1249 }
1250
1251 #[test]
1252 fn wtf8buf_from_iterator() {
1253 fn f(values: &[u32]) -> Wtf8Buf {
1254 values
1255 .iter()
1256 .map(|&c| CodePoint::from_u32(c).unwrap())
1257 .collect::<Wtf8Buf>()
1258 }
1259 assert_eq!(
1260 f(&[0x61, 0xe9, 0x20, 0x1f4a9]).bytes,
1261 b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1262 );
1263
1264 assert_eq!(f(&[0xd83d, 0xdca9]).bytes, b"\xF0\x9F\x92\xA9"); assert_eq!(
1266 f(&[0xd83d, 0x20, 0xdca9]).bytes,
1267 b"\xED\xA0\xBD \xED\xB2\xA9"
1268 );
1269 assert_eq!(f(&[0xd800, 0xdbff]).bytes, b"\xED\xA0\x80\xED\xAF\xBF");
1270 assert_eq!(f(&[0xd800, 0xe000]).bytes, b"\xED\xA0\x80\xEE\x80\x80");
1271 assert_eq!(f(&[0xd7ff, 0xdc00]).bytes, b"\xED\x9F\xBF\xED\xB0\x80");
1272 assert_eq!(f(&[0x61, 0xdc00]).bytes, b"\x61\xED\xB0\x80");
1273 assert_eq!(f(&[0xdc00]).bytes, b"\xED\xB0\x80");
1274 }
1275
1276 #[test]
1277 fn wtf8buf_extend() {
1278 fn e(initial: &[u32], extended: &[u32]) -> Wtf8Buf {
1279 fn c(value: &u32) -> CodePoint {
1280 CodePoint::from_u32(*value).unwrap()
1281 }
1282 let mut string = initial.iter().map(c).collect::<Wtf8Buf>();
1283 string.extend(extended.iter().map(c));
1284 string
1285 }
1286
1287 assert_eq!(
1288 e(&[0x61, 0xe9], &[0x20, 0x1f4a9]).bytes,
1289 b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1290 );
1291
1292 assert_eq!(e(&[0xd83d], &[0xdca9]).bytes, b"\xF0\x9F\x92\xA9"); assert_eq!(
1294 e(&[0xd83d, 0x20], &[0xdca9]).bytes,
1295 b"\xED\xA0\xBD \xED\xB2\xA9"
1296 );
1297 assert_eq!(e(&[0xd800], &[0xdbff]).bytes, b"\xED\xA0\x80\xED\xAF\xBF");
1298 assert_eq!(e(&[0xd800], &[0xe000]).bytes, b"\xED\xA0\x80\xEE\x80\x80");
1299 assert_eq!(e(&[0xd7ff], &[0xdc00]).bytes, b"\xED\x9F\xBF\xED\xB0\x80");
1300 assert_eq!(e(&[0x61], &[0xdc00]).bytes, b"\x61\xED\xB0\x80");
1301 assert_eq!(e(&[], &[0xdc00]).bytes, b"\xED\xB0\x80");
1302 }
1303
1304 #[test]
1305 fn wtf8buf_debug() {
1306 let mut string = Wtf8Buf::from_str("aé 💩");
1307 string.push(CodePoint::from_u32(0xd800).unwrap());
1308 assert_eq!(format!("{string:?}"), r#""aé 💩\u{D800}""#);
1309 }
1310
1311 #[test]
1312 fn wtf8buf_as_slice() {
1313 assert_eq!(Wtf8Buf::from_str("aé"), Wtf8::from_str("aé"));
1314 }
1315
1316 #[test]
1317 fn wtf8_debug() {
1318 let mut string = Wtf8Buf::from_str("aé 💩");
1319 string.push(CodePoint::from_u32(0xd800).unwrap());
1320 let string_ref = &*string;
1321 assert_eq!(format!("{string_ref:?}"), r#""aé 💩\u{D800}""#);
1322 }
1323
1324 #[test]
1325 fn wtf8_from_str() {
1326 assert_eq!(&Wtf8::from_str("").bytes, b"");
1327 assert_eq!(
1328 &Wtf8::from_str("aé 💩").bytes,
1329 b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1330 );
1331 }
1332
1333 #[test]
1334 fn wtf8_as_bytes() {
1335 assert_eq!(Wtf8::from_str("").as_bytes(), b"");
1336 assert_eq!(
1337 Wtf8::from_str("aé 💩").as_bytes(),
1338 b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1339 );
1340 }
1341
1342 #[test]
1343 fn wtf8_from_bytes_unchecked() {
1344 assert_eq!(unsafe { &Wtf8::from_bytes_unchecked(b"").bytes }, b"");
1345 assert_eq!(
1346 unsafe { &Wtf8::from_bytes_unchecked(b"a\xC3\xA9 \xF0\x9F\x92\xA9").bytes },
1347 b"a\xC3\xA9 \xF0\x9F\x92\xA9"
1348 );
1349 assert_eq!(
1350 unsafe { Wtf8::from_bytes_unchecked(b"a\xC3\xA9 \xF0\x9F\x92\xA9") },
1351 Wtf8::from_str("aé 💩")
1352 )
1353 }
1354
1355 #[test]
1356 fn wtf8_cow() {
1357 let s: Cow<Wtf8> = Cow::from(Wtf8::from_str("aé 💩"));
1358 assert!(matches!(s, Cow::Borrowed(_)));
1359 let owned: Wtf8Buf = s.into_owned();
1360 assert_eq!(owned, Wtf8Buf::from_str("aé 💩"));
1361 }
1362
1363 #[test]
1364 fn wtf8_len() {
1365 assert_eq!(Wtf8::from_str("").len(), 0);
1366 assert_eq!(Wtf8::from_str("aé 💩").len(), 8);
1367 }
1368
1369 #[test]
1370 fn wtf8_slice() {
1371 assert_eq!(&Wtf8::from_str("aé 💩").slice(1, 4).bytes, b"\xC3\xA9 ");
1372 }
1373
1374 #[test]
1375 #[should_panic]
1376 fn wtf8_slice_not_code_point_boundary() {
1377 Wtf8::from_str("aé 💩").slice(2, 4);
1378 }
1379
1380 #[test]
1381 fn wtf8_slice_from() {
1382 assert_eq!(
1383 &Wtf8::from_str("aé 💩").slice_from(1).bytes,
1384 b"\xC3\xA9 \xF0\x9F\x92\xA9"
1385 );
1386 }
1387
1388 #[test]
1389 #[should_panic]
1390 fn wtf8_slice_from_not_code_point_boundary() {
1391 Wtf8::from_str("aé 💩").slice_from(2);
1392 }
1393
1394 #[test]
1395 fn wtf8_slice_to() {
1396 assert_eq!(&Wtf8::from_str("aé 💩").slice_to(4).bytes, b"a\xC3\xA9 ");
1397 }
1398
1399 #[test]
1400 #[should_panic]
1401 fn wtf8_slice_to_not_code_point_boundary() {
1402 Wtf8::from_str("aé 💩").slice_from(5);
1403 }
1404
1405 #[test]
1406 fn wtf8_ascii_byte_at() {
1407 let slice = Wtf8::from_str("aé 💩");
1408 assert_eq!(slice.ascii_byte_at(0), b'a');
1409 assert_eq!(slice.ascii_byte_at(1), b'\xFF');
1410 assert_eq!(slice.ascii_byte_at(2), b'\xFF');
1411 assert_eq!(slice.ascii_byte_at(3), b' ');
1412 assert_eq!(slice.ascii_byte_at(4), b'\xFF');
1413 }
1414
1415 #[test]
1416 fn wtf8_code_points() {
1417 fn c(value: u32) -> CodePoint {
1418 CodePoint::from_u32(value).unwrap()
1419 }
1420 fn cp(string: &Wtf8Buf) -> Vec<Option<char>> {
1421 string
1422 .code_points()
1423 .map(|c| c.to_char())
1424 .collect::<Vec<_>>()
1425 }
1426 let mut string = Wtf8Buf::from_str("é ");
1427 assert_eq!(cp(&string), vec![Some('é'), Some(' ')]);
1428 string.push(c(0xd83d));
1429 assert_eq!(cp(&string), vec![Some('é'), Some(' '), None]);
1430 string.push(c(0xdca9));
1431 assert_eq!(cp(&string), vec![Some('é'), Some(' '), Some('💩')]);
1432 }
1433
1434 #[test]
1435 fn wtf8_as_str() {
1436 assert_eq!(Wtf8::from_str("").as_str(), Some(""));
1437 assert_eq!(Wtf8::from_str("aé 💩").as_str(), Some("aé 💩"));
1438 let mut string = Wtf8Buf::new();
1439 string.push(CodePoint::from_u32(0xd800).unwrap());
1440 assert_eq!(string.as_str(), None);
1441 }
1442
1443 #[test]
1444 fn wtf8_to_string_lossy() {
1445 assert_eq!(Wtf8::from_str("").to_string_lossy(), Cow::Borrowed(""));
1446 assert_eq!(
1447 Wtf8::from_str("aé 💩").to_string_lossy(),
1448 Cow::Borrowed("aé 💩")
1449 );
1450 let mut string = Wtf8Buf::from_str("aé 💩");
1451 string.push(CodePoint::from_u32(0xd800).unwrap());
1452 assert_eq!(string.to_string_lossy(), {
1453 let o: Cow<str> = Cow::Owned(String::from("aé 💩�"));
1454 o
1455 });
1456 }
1457
1458 #[test]
1459 fn wtf8_to_ill_formed_utf16() {
1460 let mut string = Wtf8Buf::from_str("aé ");
1461 string.push(CodePoint::from_u32(0xd83d).unwrap());
1462 string.push_char('💩');
1463 assert_eq!(
1464 string.to_ill_formed_utf16().collect::<Vec<_>>(),
1465 vec![0x61, 0xe9, 0x20, 0xd83d, 0xd83d, 0xdca9]
1466 );
1467 }
1468
1469 #[test]
1470 fn wtf8buf_wtf8_from_bytes_valid() {
1471 assert!(Wtf8Buf::from_bytes(b"hello".to_vec()).is_ok());
1473 assert!(Wtf8Buf::from_bytes(b"a\xC3\xA9 \xF0\x9F\x92\xA9".to_vec()).is_ok());
1474 assert!(Wtf8::from_bytes(b"hello").is_ok());
1475 assert!(Wtf8::from_bytes(b"a\xC3\xA9 \xF0\x9F\x92\xA9").is_ok());
1476
1477 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());
1483 }
1485
1486 #[test]
1487 fn wtf8buf_wtf8_from_bytes_invalid() {
1488 assert!(Wtf8Buf::from_bytes(b"\xED\xA0\x80\xED\xB0\x80".to_vec()).is_err());
1490 assert!(Wtf8Buf::from_bytes(b"\xED\xA0\xBD\xED\xB2\xA9".to_vec()).is_err());
1491 assert!(Wtf8::from_bytes(b"\xED\xA0\x80\xED\xB0\x80").is_err());
1492 assert!(Wtf8::from_bytes(b"\xED\xA0\xBD\xED\xB2\xA9").is_err());
1493
1494 assert!(Wtf8Buf::from_bytes(vec![0xff]).is_err());
1496 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];
1502 let result = Wtf8Buf::from_bytes(original.clone());
1503 assert_eq!(result.unwrap_err(), original);
1504
1505 let result = Wtf8::from_bytes(&original);
1506 assert_eq!(result.unwrap_err(), original);
1507 }
1508}