feat: flat tweaks and string interning

Co-authored-by: rvcas <x@rvcas.dev>
This commit is contained in:
Kasey White
2022-05-31 22:57:52 -04:00
parent 31e7d63d9a
commit 581b8fc85d
7 changed files with 105 additions and 41 deletions

View File

@@ -44,7 +44,7 @@ impl Decode<'_> for char {
impl Decode<'_> for String {
fn decode(d: &mut Decoder) -> Result<Self, String> {
d.string()
d.utf8()
}
}

View File

@@ -51,6 +51,11 @@ impl<'b> Decoder<'b> {
Ok(s)
}
pub fn utf8(&mut self) -> Result<String, String> {
// TODO: Better Error Handling
Ok(String::from_utf8(Vec::<u8>::decode(self)?).unwrap())
}
pub fn filler(&mut self) -> Result<(), String> {
while self.zero()? {}
Ok(())

View File

@@ -46,7 +46,7 @@ impl Encode for char {
impl Encode for &str {
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
e.string(*self)?;
e.utf8(self)?;
Ok(())
}
@@ -54,7 +54,7 @@ impl Encode for &str {
impl Encode for String {
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
e.string(self)?;
e.utf8(self)?;
Ok(())
}

View File

@@ -75,7 +75,7 @@ impl Encoder {
self.word(c as usize);
Ok(self)
}
// TODO: Do we need this?
pub fn string(&mut self, s: &str) -> Result<&mut Self, String> {
for i in s.chars() {
self.one();
@@ -87,6 +87,10 @@ impl Encoder {
Ok(self)
}
pub fn utf8(&mut self, s: &str) -> Result<&mut Self, String> {
self.bytes(s.as_bytes())
}
fn zero(&mut self) {
if self.used_bits == 7 {
self.next_word();