|
| 1 | +use crate::array::Array; |
| 2 | +use std::ops::{Deref, DerefMut}; |
| 3 | +use std::str; |
| 4 | +use std::borrow::{Borrow, BorrowMut}; |
| 5 | +use std::fmt; |
| 6 | +use std::hash::Hash; |
| 7 | +use lox_gc::{Trace, Tracer}; |
| 8 | + |
| 9 | +pub struct LoxString { |
| 10 | + vec: Array<u8>, |
| 11 | +} |
| 12 | + |
| 13 | +impl LoxString { |
| 14 | + pub fn new() -> Self { |
| 15 | + Self { |
| 16 | + vec: Array::new(), |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + pub fn as_bytes(&self) -> &[u8] { |
| 21 | + &self.vec |
| 22 | + } |
| 23 | + |
| 24 | + pub fn with_capacity(capacity: usize) -> Self { |
| 25 | + Self { |
| 26 | + vec: Array::with_capacity(capacity), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + pub fn as_str(&self) -> &str { |
| 31 | + self |
| 32 | + } |
| 33 | + |
| 34 | + pub fn push_str(&mut self, string: &str) { |
| 35 | + self.vec.extend_from_slice(string.as_bytes()); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +unsafe impl Trace for LoxString { |
| 40 | + fn trace(&self, _tracer: &mut Tracer) { |
| 41 | + self.vec.mark(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl PartialEq<LoxString> for LoxString { |
| 46 | + fn eq(&self, other: &LoxString) -> bool { |
| 47 | + PartialEq::eq(&self[..], &other[..]) |
| 48 | + } |
| 49 | + |
| 50 | + fn ne(&self, other: &LoxString) -> bool { |
| 51 | + PartialEq::ne(&self[..], &other[..]) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl From<String> for LoxString { |
| 56 | + fn from(value: String) -> Self { |
| 57 | + let mut str = LoxString::with_capacity(value.len()); |
| 58 | + str.push_str(&value); |
| 59 | + str |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl From<&String> for LoxString { |
| 64 | + fn from(value: &String) -> Self { |
| 65 | + let mut str = LoxString::with_capacity(value.len()); |
| 66 | + str.push_str(value); |
| 67 | + str |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl fmt::Debug for LoxString { |
| 72 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 73 | + fmt::Debug::fmt(&**self, f) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl fmt::Display for LoxString { |
| 78 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 79 | + fmt::Display::fmt(&**self, f) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl Hash for LoxString { |
| 84 | + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 85 | + (&**self).hash(state) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl Default for LoxString { |
| 90 | + fn default() -> Self { |
| 91 | + Self::new() |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl Deref for LoxString { |
| 96 | + type Target = str; |
| 97 | + |
| 98 | + fn deref(&self) -> &Self::Target { |
| 99 | + unsafe { str::from_utf8_unchecked(&self.vec) } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl DerefMut for LoxString { |
| 104 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 105 | + unsafe { str::from_utf8_unchecked_mut(&mut self.vec) } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl AsRef<str> for LoxString { |
| 110 | + fn as_ref(&self) -> &str { |
| 111 | + self |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl Borrow<str> for LoxString { |
| 116 | + fn borrow(&self) -> &str { |
| 117 | + &self[..] |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl BorrowMut<str> for LoxString { |
| 122 | + fn borrow_mut(&mut self) -> &mut str { |
| 123 | + &mut self[..] |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl AsRef<[u8]> for LoxString { |
| 128 | + fn as_ref(&self) -> &[u8] { |
| 129 | + self.as_bytes() |
| 130 | + } |
| 131 | +} |
0 commit comments