1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use generic_vec::{raw::Storage, GenericVec};

#[cfg(feature = "alloc")]
use generic_vec::HeapVec;
#[cfg(feature = "alloc")]
use std::{
    borrow::Cow,
    iter::FromIterator,
    ops::{Add, AddAssign},
};

use crate::{string_base::StringBase, OwnedString};
use core::{
    ops::{Index, IndexMut},
    slice::SliceIndex,
};

impl<I> Index<I> for crate::str32
where
    I: SliceIndex<crate::str32>,
{
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &I::Output {
        index.index(self)
    }
}

impl<I> IndexMut<I> for crate::str32
where
    I: SliceIndex<crate::str32>,
{
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut I::Output {
        index.index_mut(self)
    }
}

#[cfg(feature = "alloc")]
impl<S: ?Sized + Storage<Item = char>> std::fmt::Debug for OwnedString<char, S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s: String = self.as_ref().storage.iter().collect();
        write!(f, "{:?}", s)
    }
}

#[cfg(feature = "alloc")]
impl<S: ?Sized + Storage<Item = char>> std::fmt::Display for OwnedString<char, S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s: String = self.as_ref().storage.iter().collect();
        write!(f, "{}", s)
    }
}

#[cfg(feature = "alloc")]
impl std::fmt::Debug for crate::str32 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s: String = self.storage.iter().collect();
        write!(f, "{:?}", s)
    }
}

#[cfg(feature = "alloc")]
impl std::fmt::Display for crate::str32 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s: String = self.storage.iter().collect();
        write!(f, "{}", s)
    }
}

#[cfg(feature = "alloc")]
impl From<String> for crate::String32 {
    fn from(s: String) -> Self {
        s.chars().collect()
    }
}

#[cfg(feature = "alloc")]
impl FromIterator<char> for crate::String32 {
    fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Self {
        let mut new = Self::new();
        iter.into_iter().for_each(|c| new.push(c));
        new
    }
}

#[cfg(feature = "alloc")]
impl From<&str> for StringBase<HeapVec<char>> {
    fn from(s: &str) -> Self {
        s.to_owned().into()
    }
}

impl From<&str> for &crate::str32 {
    fn from(s: &str) -> Self {
        unsafe { core::mem::transmute(s) }
    }
}

impl From<&mut str> for &mut crate::str32 {
    fn from(s: &mut str) -> Self {
        unsafe { core::mem::transmute(s) }
    }
}

impl<S: ?Sized + Storage<Item = char>, T: ?Sized + AsRef<[char]>> PartialEq<T>
    for StringBase<GenericVec<S::Item, S>>
{
    fn eq(&self, other: &T) -> bool {
        self.storage.as_ref() == other.as_ref()
    }
}

impl<T: ?Sized + AsRef<[char]>> PartialEq<T> for crate::str32 {
    fn eq(&self, other: &T) -> bool {
        self.storage.as_ref() == other.as_ref()
    }
}

#[cfg(feature = "alloc")]
impl<'a> Add<&'a crate::str32> for Cow<'a, crate::str32> {
    type Output = Cow<'a, crate::str32>;

    #[inline]
    fn add(mut self, rhs: &'a crate::str32) -> Self::Output {
        self += rhs;
        self
    }
}

#[cfg(feature = "alloc")]
impl<'a> AddAssign<&'a crate::str32> for Cow<'a, crate::str32> {
    fn add_assign(&mut self, rhs: &'a crate::str32) {
        if self.is_empty() {
            *self = Cow::Borrowed(rhs)
        } else if !rhs.is_empty() {
            if let Cow::Borrowed(lhs) = *self {
                let mut s = crate::String32::with_capacity(lhs.len() + rhs.len());
                s.push_str32(lhs);
                *self = Cow::Owned(s);
            }
            self.to_mut().push_str32(rhs);
        }
    }
}