use crate::{UChar, UStr, WideChar};
use core::borrow::Borrow;
use core::ops::{Deref, Index, RangeFull};
use core::{char, cmp, slice};
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::{
borrow::{Cow, ToOwned},
boxed::Box,
string::String,
vec::Vec,
};
#[cfg(feature = "std")]
use std::{
borrow::{Cow, ToOwned},
boxed::Box,
convert::Infallible,
str::FromStr,
string::String,
vec::Vec,
};
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct UString<C: UChar> {
pub(crate) inner: Vec<C>,
}
impl<C: UChar> UString<C> {
pub fn new() -> Self {
Self { inner: Vec::new() }
}
pub fn from_vec(raw: impl Into<Vec<C>>) -> Self {
Self { inner: raw.into() }
}
pub unsafe fn from_ptr(p: *const C, len: usize) -> Self {
if len == 0 {
return Self::new();
}
assert!(!p.is_null());
let slice = slice::from_raw_parts(p, len);
Self::from_vec(slice)
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
inner: Vec::with_capacity(capacity),
}
}
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
pub fn clear(&mut self) {
self.inner.clear()
}
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}
pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional)
}
pub fn into_vec(self) -> Vec<C> {
self.inner
}
pub fn as_ustr(&self) -> &UStr<C> {
self
}
pub fn push(&mut self, s: impl AsRef<UStr<C>>) {
self.inner.extend_from_slice(&s.as_ref().inner)
}
pub fn push_slice(&mut self, s: impl AsRef<[C]>) {
self.inner.extend_from_slice(&s.as_ref())
}
pub fn shrink_to_fit(&mut self) {
self.inner.shrink_to_fit();
}
pub fn into_boxed_ustr(self) -> Box<UStr<C>> {
let rw = Box::into_raw(self.inner.into_boxed_slice()) as *mut UStr<C>;
unsafe { Box::from_raw(rw) }
}
}
impl FromStr for UString<u16> {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from_str(s))
}
}
impl UString<u16> {
#[allow(clippy::should_implement_trait)]
pub fn from_str<S: AsRef<str> + ?Sized>(s: &S) -> Self {
Self {
inner: s.as_ref().encode_utf16().collect(),
}
}
#[cfg(feature = "std")]
pub fn from_os_str<S: AsRef<std::ffi::OsStr> + ?Sized>(s: &S) -> Self {
Self {
inner: crate::platform::os_to_wide(s.as_ref()),
}
}
pub fn push_str(&mut self, s: impl AsRef<str>) {
self.inner.extend(s.as_ref().encode_utf16())
}
#[cfg(feature = "std")]
pub fn push_os_str(&mut self, s: impl AsRef<std::ffi::OsStr>) {
self.inner.extend(crate::platform::os_to_wide(s.as_ref()))
}
}
impl FromStr for UString<u32> {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from_str(s))
}
}
impl UString<u32> {
pub fn from_chars(raw: Vec<char>) -> Self {
UString {
inner: raw.into_iter().map(u32::from).collect::<Vec<_>>(),
}
}
#[allow(clippy::should_implement_trait)]
pub fn from_str<S: AsRef<str> + ?Sized>(s: &S) -> Self {
let v: Vec<char> = s.as_ref().chars().collect();
UString::from_chars(v)
}
#[cfg(feature = "std")]
pub fn from_os_str<S: AsRef<std::ffi::OsStr> + ?Sized>(s: &S) -> Self {
let v: Vec<char> = s.as_ref().to_string_lossy().chars().collect();
UString::from_chars(v)
}
pub unsafe fn from_char_ptr(p: *const char, len: usize) -> Self {
UString::from_ptr(p as *const u32, len)
}
pub fn push_str(&mut self, s: impl AsRef<str>) {
self.inner.extend(s.as_ref().chars().map(|c| c as u32))
}
#[cfg(feature = "std")]
pub fn push_os_str(&mut self, s: impl AsRef<std::ffi::OsStr>) {
self.inner
.extend(s.as_ref().to_string_lossy().chars().map(|c| c as u32))
}
}
impl<C: UChar> Into<Vec<C>> for UString<C> {
fn into(self) -> Vec<C> {
self.into_vec()
}
}
impl<'a> From<UString<u16>> for Cow<'a, UStr<u16>> {
fn from(s: UString<u16>) -> Self {
Cow::Owned(s)
}
}
impl<'a> From<UString<u32>> for Cow<'a, UStr<u32>> {
fn from(s: UString<u32>) -> Self {
Cow::Owned(s)
}
}
impl Into<UString<u16>> for Vec<u16> {
fn into(self) -> UString<u16> {
UString::from_vec(self)
}
}
impl Into<UString<u32>> for Vec<u32> {
fn into(self) -> UString<u32> {
UString::from_vec(self)
}
}
impl Into<UString<u32>> for Vec<char> {
fn into(self) -> UString<u32> {
UString::from_chars(self)
}
}
impl From<String> for UString<u16> {
fn from(s: String) -> Self {
Self::from_str(&s)
}
}
impl From<String> for UString<u32> {
fn from(s: String) -> Self {
Self::from_str(&s)
}
}
impl From<&'_ str> for UString<u16> {
fn from(s: &str) -> Self {
Self::from_str(s)
}
}
impl From<&'_ str> for UString<u32> {
fn from(s: &str) -> Self {
Self::from_str(s)
}
}
impl From<&'_ String> for UString<u16> {
fn from(s: &String) -> Self {
Self::from_str(s)
}
}
impl From<&'_ String> for UString<u32> {
fn from(s: &String) -> Self {
Self::from_str(s)
}
}
#[cfg(feature = "std")]
impl From<std::ffi::OsString> for UString<u16> {
fn from(s: std::ffi::OsString) -> Self {
Self::from_os_str(&s)
}
}
#[cfg(feature = "std")]
impl From<std::ffi::OsString> for UString<u32> {
fn from(s: std::ffi::OsString) -> Self {
Self::from_os_str(&s)
}
}
#[cfg(feature = "std")]
impl From<UString<u16>> for std::ffi::OsString {
fn from(s: UString<u16>) -> Self {
s.to_os_string()
}
}
#[cfg(feature = "std")]
impl From<UString<u32>> for std::ffi::OsString {
fn from(s: UString<u32>) -> Self {
s.to_os_string()
}
}
impl<'a, C: UChar, T: ?Sized + AsRef<UStr<C>>> From<&'a T> for UString<C> {
fn from(s: &'a T) -> Self {
s.as_ref().to_ustring()
}
}
impl<C: UChar> Index<RangeFull> for UString<C> {
type Output = UStr<C>;
#[inline]
fn index(&self, _index: RangeFull) -> &UStr<C> {
UStr::from_slice(&self.inner)
}
}
impl<C: UChar> Deref for UString<C> {
type Target = UStr<C>;
#[inline]
fn deref(&self) -> &UStr<C> {
&self[..]
}
}
impl<C: UChar> PartialEq<UStr<C>> for UString<C> {
#[inline]
fn eq(&self, other: &UStr<C>) -> bool {
self.as_ustr() == other
}
}
impl<C: UChar> PartialOrd<UStr<C>> for UString<C> {
#[inline]
fn partial_cmp(&self, other: &UStr<C>) -> Option<cmp::Ordering> {
self.as_ustr().partial_cmp(other)
}
}
impl<'a, C: UChar> PartialEq<&'a UStr<C>> for UString<C> {
#[inline]
fn eq(&self, other: &&'a UStr<C>) -> bool {
self.as_ustr() == *other
}
}
impl<'a, C: UChar> PartialOrd<&'a UStr<C>> for UString<C> {
#[inline]
fn partial_cmp(&self, other: &&'a UStr<C>) -> Option<cmp::Ordering> {
self.as_ustr().partial_cmp(*other)
}
}
impl<'a, C: UChar> PartialEq<Cow<'a, UStr<C>>> for UString<C> {
#[inline]
fn eq(&self, other: &Cow<'a, UStr<C>>) -> bool {
self.as_ustr() == other.as_ref()
}
}
impl<'a, C: UChar> PartialOrd<Cow<'a, UStr<C>>> for UString<C> {
#[inline]
fn partial_cmp(&self, other: &Cow<'a, UStr<C>>) -> Option<cmp::Ordering> {
self.as_ustr().partial_cmp(other.as_ref())
}
}
impl<C: UChar> Borrow<UStr<C>> for UString<C> {
fn borrow(&self) -> &UStr<C> {
&self[..]
}
}
impl<C: UChar> ToOwned for UStr<C> {
type Owned = UString<C>;
fn to_owned(&self) -> UString<C> {
self.to_ustring()
}
}
impl<'a> From<&'a UStr<u16>> for Cow<'a, UStr<u16>> {
fn from(s: &'a UStr<u16>) -> Self {
Cow::Borrowed(s)
}
}
impl<'a> From<&'a UStr<u32>> for Cow<'a, UStr<u32>> {
fn from(s: &'a UStr<u32>) -> Self {
Cow::Borrowed(s)
}
}
impl<C: UChar> AsRef<UStr<C>> for UStr<C> {
fn as_ref(&self) -> &Self {
self
}
}
impl<C: UChar> AsRef<UStr<C>> for UString<C> {
fn as_ref(&self) -> &UStr<C> {
self
}
}
impl<C: UChar> AsRef<[C]> for UStr<C> {
fn as_ref(&self) -> &[C] {
self.as_slice()
}
}
impl<C: UChar> AsRef<[C]> for UString<C> {
fn as_ref(&self) -> &[C] {
self.as_slice()
}
}
impl<'a, C: UChar> From<&'a UStr<C>> for Box<UStr<C>> {
fn from(s: &'a UStr<C>) -> Self {
let boxed: Box<[C]> = Box::from(&s.inner);
let rw = Box::into_raw(boxed) as *mut UStr<C>;
unsafe { Box::from_raw(rw) }
}
}
impl<C: UChar> From<Box<UStr<C>>> for UString<C> {
fn from(boxed: Box<UStr<C>>) -> Self {
boxed.into_ustring()
}
}
impl<C: UChar> From<UString<C>> for Box<UStr<C>> {
fn from(s: UString<C>) -> Self {
s.into_boxed_ustr()
}
}
impl<C: UChar> Default for Box<UStr<C>> {
fn default() -> Self {
let boxed: Box<[C]> = Box::from([]);
let rw = Box::into_raw(boxed) as *mut UStr<C>;
unsafe { Box::from_raw(rw) }
}
}
pub type U16String = UString<u16>;
pub type U32String = UString<u32>;
pub type WideString = UString<WideChar>;