alt_std/
string.rs

1use crate::vec::*;
2use ::core::*;
3use ::core::cmp::*;
4use crate::hash::*;
5
6#[repr(C)]
7pub struct String {
8    data    : Vec<u8>
9}
10
11impl String {
12    pub fn withCapacity(c: usize) -> Self {
13        Self { data: Vec::withCapacity(c) }
14    }
15
16    pub fn new() -> Self { Self { data: Vec::new() } }
17    pub fn from(s: &str) -> Self {
18        let mut st = Self::new();
19        for c in s.bytes() {
20            st.data.pushBack(c);
21        }
22        st
23    }
24
25    pub fn toStr(&self) -> &str {
26        ::core::str::from_utf8(self.data.asArray()).expect("Error getting string out")
27    }
28
29    pub fn add(&mut self, u: u8) {
30        self.data.pushBack(u);
31    }
32
33    pub fn asArray(&self) -> &[u8] { self.data.asArray() }
34    pub fn asMutArray(&mut self) -> &mut [u8] { self.data.asMutArray() }
35}
36
37pub trait Append<T> {
38    fn append(&mut self, other: T);
39}
40
41impl Append<&str> for String {
42    fn append(&mut self, s: &str) {
43        for c in s.bytes() {
44            self.data.pushBack(c);
45        }
46    }
47}
48
49impl Append<&String> for String {
50    fn append(&mut self, s: &String) {
51        for c in s.toStr().bytes() {
52            self.data.pushBack(c);
53        }
54    }
55}
56
57impl PartialEq<String> for String {
58    fn eq(&self, other: &Self) -> bool {
59        let ls = self.data.len();
60        let lo = other.data.len();
61        if ls != lo { return false }
62        for i in 0..self.data.len() {
63            if self.data[i] != other.data[i] { return false }
64        }
65        true
66    }
67}
68
69impl Eq for String {}
70
71impl PartialEq<&str> for String {
72    fn eq(&self, other: &&str) -> bool {
73        let ob = (*other).as_bytes();
74        let ls = self.data.len();
75        let lo = ob.len();
76        if ls != lo { return false }
77        for i in 0..self.data.len() {
78            if self.data[i] != ob[i] { return false }
79        }
80        true
81    }
82}
83
84impl Clone for String {
85    fn clone(&self) -> Self {
86        String::from(self.toStr())
87    }
88}
89
90impl fmt::Write for String {
91    #[inline]
92    fn write_str(&mut self, s: &str) -> fmt::Result {
93        self.append(s);
94        Ok(())
95    }
96
97    #[inline]
98    fn write_char(&mut self, c: char) -> fmt::Result {
99        self.add(c as u8);
100        Ok(())
101    }
102}
103
104impl fmt::Display for String {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        write!(f, "{}", self.toStr())
107    }
108}
109
110impl Hash for String {
111    fn hash(&self) -> usize {
112        self.asArray().hash()
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119    use crate::io::*;
120    #[test]
121    fn testConversion() {
122        {
123            let u : u32 = 12345;
124            if String::from("12345") != format(format_args!("{}", u)) {
125                panic!("fail {}", u);
126            }
127        }
128
129        {
130            let i : i32 = -12345;
131            if String::from("-12345") != format(format_args!("{}", i)) {
132                panic!("fail {}", i);
133            }
134        }
135    }
136}