use alloc::borrow::{Borrow, Cow};
use alloc::vec::Vec;
use alloc::string::{String, FromUtf16Error, FromUtf8Error};
use core::cmp::PartialEq;
use core::fmt::Display;
pub trait StringExt<'a>:
Borrow<str>
+ Display
+ PartialEq<str>
+ PartialEq<&'a str>
+ PartialEq<String>
+ PartialEq<Cow<'a, str>>
{
fn new() -> Self
where
Self: Sized;
fn with_capacity(capacity: usize) -> Self
where
Self: Sized;
fn from_utf8(vec: Vec<u8>) -> Result<Self, FromUtf8Error>
where
Self: Sized;
fn from_utf8_lossy(v: &'a [u8]) -> Cow<'a, str>
where
Self: Sized,
{
String::from_utf8_lossy(v)
}
fn from_utf16(v: &[u16]) -> Result<Self, FromUtf16Error>
where
Self: Sized;
fn from_utf16_lossy(v: &[u16]) -> Self
where
Self: Sized;
unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> Self
where
Self: Sized;
unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> Self
where
Self: Sized;
fn into_bytes(self) -> Vec<u8>;
fn push_str(&mut self, string: &str);
fn capacity(&self) -> usize;
fn reserve(&mut self, additional: usize);
fn reserve_exact(&mut self, additional: usize);
fn shrink_to_fit(&mut self);
fn push(&mut self, ch: char);
fn as_bytes(&self) -> &[u8];
fn truncate(&mut self, new_len: usize);
fn pop(&mut self) -> Option<char>;
fn remove(&mut self, idx: usize) -> char;
fn insert(&mut self, idx: usize, ch: char);
fn insert_str(&mut self, idx: usize, string: &str);
unsafe fn as_mut_slice(&mut self) -> &mut [u8];
fn len(&self) -> usize;
#[inline]
fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
fn clear(&mut self) {
self.truncate(0);
}
}
impl<'a> StringExt<'a> for String {
#[inline]
fn new() -> Self {
String::new()
}
#[inline]
fn with_capacity(capacity: usize) -> Self {
String::with_capacity(capacity)
}
#[inline]
fn from_utf8(vec: Vec<u8>) -> Result<Self, FromUtf8Error> {
String::from_utf8(vec)
}
#[inline]
fn from_utf16(v: &[u16]) -> Result<Self, FromUtf16Error> {
String::from_utf16(v)
}
#[inline]
fn from_utf16_lossy(v: &[u16]) -> Self {
String::from_utf16_lossy(v)
}
#[inline]
unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> Self {
String::from_raw_parts(buf, length, capacity)
}
#[inline]
unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> Self {
String::from_utf8_unchecked(bytes)
}
#[inline]
fn into_bytes(self) -> Vec<u8> {
String::into_bytes(self)
}
#[inline]
fn push_str(&mut self, string: &str) {
String::push_str(self, string)
}
#[inline]
fn capacity(&self) -> usize {
String::capacity(self)
}
#[inline]
fn reserve(&mut self, additional: usize) {
String::reserve(self, additional)
}
#[inline]
fn reserve_exact(&mut self, additional: usize) {
String::reserve_exact(self, additional)
}
#[inline]
fn shrink_to_fit(&mut self) {
String::shrink_to_fit(self)
}
#[inline]
fn push(&mut self, ch: char) {
String::push(self, ch)
}
#[inline]
fn as_bytes(&self) -> &[u8] {
String::as_bytes(self)
}
#[inline]
fn truncate(&mut self, new_len: usize) {
String::truncate(self, new_len)
}
#[inline]
fn pop(&mut self) -> Option<char> {
String::pop(self)
}
#[inline]
fn remove(&mut self, idx: usize) -> char {
String::remove(self, idx)
}
#[inline]
fn insert(&mut self, idx: usize, ch: char) {
String::insert(self, idx, ch)
}
#[inline]
fn insert_str(&mut self, idx: usize, string: &str) {
String::insert_str(self, idx, string)
}
#[inline]
unsafe fn as_mut_slice(&mut self) -> &mut [u8] {
&mut *(self.as_mut_str() as *mut str as *mut [u8])
}
#[inline]
fn len(&self) -> usize {
String::len(self)
}
}
#[cfg(test)]
mod std_string_stringext_sanity_tests {
use alloc::string::String;
use super::StringExt;
#[test]
fn test_new() {
let s = <String as StringExt>::new();
assert!(StringExt::is_empty(&s));
}
#[test]
fn test_with_capacity() {
let s = <String as StringExt>::with_capacity(10);
assert!(StringExt::capacity(&s) >= 10);
}
#[test]
fn test_from_utf8() {
let s = <String as StringExt>::from_utf8(vec![104, 101, 108, 108, 111]);
assert_eq!(s.unwrap(), "hello");
}
#[test]
fn test_from_utf16() {
let v = &mut [0xD834, 0xDD1E, 0x006d, 0x0075, 0x0073, 0x0069, 0x0063];
let s = <String as StringExt>::from_utf16(v);
assert_eq!(s.unwrap(), "𝄞music");
}
#[test]
fn test_from_utf16_lossy() {
let input = b"Hello \xF0\x90\x80World";
let output = <String as StringExt>::from_utf8_lossy(input);
assert_eq!(output, "Hello \u{FFFD}World");
}
#[test]
fn test_into_bytes() {
let s = String::from("hello");
let bytes = StringExt::into_bytes(s);
assert_eq!(bytes, [104, 101, 108, 108, 111]);
}
#[test]
fn test_push_str() {
let mut s = String::from("hello");
StringExt::push_str(&mut s, " world");
assert_eq!(s, "hello world");
}
#[test]
fn test_capacity() {
let s = <String as StringExt>::with_capacity(100);
assert!(String::capacity(&s) >= 100);
}
#[test]
fn test_reserve() {
let mut s = <String as StringExt>::new();
StringExt::reserve(&mut s, 100);
assert!(String::capacity(&s) >= 100);
}
#[test]
fn test_reserve_exact() {
let mut s = <String as StringExt>::new();
StringExt::reserve_exact(&mut s, 100);
assert!(String::capacity(&s) >= 100);
}
#[test]
fn test_shrink_to_fit() {
let mut s = <String as StringExt>::with_capacity(100);
StringExt::push_str(&mut s, "foo");
StringExt::shrink_to_fit(&mut s);
assert_eq!(String::capacity(&s), 3);
}
#[test]
fn test_push() {
let mut s = String::new();
StringExt::push(&mut s, 'a');
assert_eq!(s, "a");
}
#[test]
fn test_truncate() {
let mut s = String::from("foo");
StringExt::truncate(&mut s, 1);
assert_eq!(s, "f");
}
#[test]
fn test_pop() {
let mut s = String::from("foo");
assert_eq!(StringExt::pop(&mut s), Some('o'));
assert_eq!(StringExt::pop(&mut s), Some('o'));
assert_eq!(StringExt::pop(&mut s), Some('f'));
assert_eq!(StringExt::pop(&mut s), None);
}
}