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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
use crate::vec::*; use ::core::cmp::*; use crate::hash::*; #[repr(C)] pub struct String { data : Vec<u8> } impl String { pub fn new() -> Self { Self { data: Vec::new() } } pub fn from(s: &str) -> Self { let mut st = Self::new(); for c in s.bytes() { st.data.pushBack(c); } st } pub fn toStr(&self) -> &str { ::core::str::from_utf8(self.data.asArray()).expect("Error getting string out") } pub fn add(&mut self, u: u8) { self.data.pushBack(u); } pub fn asArray(&self) -> &[u8] { self.data.asArray() } } pub trait Append<T> { fn append(&mut self, other: T); } impl Append<&str> for String { fn append(&mut self, s: &str) { for c in s.bytes() { self.data.pushBack(c); } } } impl Append<&String> for String { fn append(&mut self, s: &String) { for c in s.toStr().bytes() { self.data.pushBack(c); } } } impl PartialEq<String> for String { fn eq(&self, other: &Self) -> bool { let ls = self.data.len(); let lo = other.data.len(); if ls != lo { return false } for i in 0..self.data.len() { if self.data[i] != other.data[i] { return false } } true } } impl Eq for String {} impl PartialEq<&str> for String { fn eq(&self, other: &&str) -> bool { let ob = (*other).as_bytes(); let ls = self.data.len(); let lo = ob.len(); if ls != lo { return false } for i in 0..self.data.len() { if self.data[i] != ob[i] { return false } } true } } impl Clone for String { fn clone(&self) -> Self { String::from(self.toStr()) } } pub trait ToString { fn toString(&self) -> String; } impl ToString for &str { fn toString(&self) -> String { String::from(*self) } } macro_rules! impl_signed { ($t:ident) => { impl ToString for $t { fn toString(&self) -> String { let mut v = Vec::<u8>::new(); let mut s = String::new(); let mut current = *self; let sign = current < 0; if sign { current = -current } loop { let u = current % 10; v.pushBack((u + '0' as Self) as u8); current /= 10; if current == 0 { if sign { v.pushBack('-' as u8); } for i in 0..v.len() { s.add(v[v.len() - i - 1]); } return s; } } } } }; } macro_rules! impl_unsigned { ($t:ident) => { impl ToString for $t { fn toString(&self) -> String { let mut v = Vec::<u8>::new(); let mut s = String::new(); let mut current = *self; loop { let u = current % 10; v.pushBack((u + '0' as Self) as u8); current /= 10; if current == 0 { for i in 0..v.len() { s.add(v[v.len() - i - 1]); } return s; } } } } }; } impl ToString for char { fn toString(&self) -> String { let mut s = String::new(); s.add(*self as u8); s } } impl ToString for bool { fn toString(&self) -> String { let tf = if *self { "true" } else { "fasle" }; String::from(tf) } } impl ToString for String { fn toString(&self) -> String { self.clone() } } impl_signed!(i8); impl_signed!(i16); impl_signed!(i32); impl_signed!(i64); impl_unsigned!(u8); impl_unsigned!(u16); impl_unsigned!(u32); impl_unsigned!(u64); impl_unsigned!(usize); impl Hash for String { fn hash(&self) -> usize { self.asArray().hash() } } #[cfg(test)] mod tests { use super::*; #[test] fn testConversion() { { let u : u32 = 12345; if String::from("12345") != u.toString() { panic!("fail {}", u.toString().toStr()); } } { let i : i32 = -12345; if String::from("-12345") != i.toString() { panic!("fail {}", i.toString().toStr()); } } } }