diff --git a/Cargo.lock b/Cargo.lock index e8b658f..8587b68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -701,7 +701,7 @@ checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" [[package]] name = "urbit-q" -version = "0.2.4" +version = "0.2.5" dependencies = [ "criterion", "phf", diff --git a/Cargo.toml b/Cargo.toml index bc5a2b9..5197887 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "urbit-q" -version = "0.2.4" +version = "0.2.5" authors = ["k2l8m11n2"] edition = "2018" description = "Encode and decode data in Urbit's @q format" diff --git a/src/lib.rs b/src/lib.rs index 6d50f95..d494c07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,21 +77,23 @@ pub fn encode(input: &[u8]) -> String { /// decode("hello world"); // None /// ``` pub fn decode(input: &str) -> Option> { - let mut input_bytes = Vec::from(input); - input_bytes.retain(|x| *x != ('-' as u8) && *x != (' ' as u8)); - if input_bytes.len() == 3 { - return Some(vec![*consts::SUFFIXES_MAP.get(&input_bytes[..])?]); + let mut bytes = Vec::from(input); + bytes.retain(|x| *x != ('-' as u8) && *x != (' ' as u8)); + if bytes.len() == 3 { + bytes[0] = *consts::SUFFIXES_MAP.get(&bytes[..])?; + bytes.truncate(1); + return Some(bytes); } - if input_bytes.len() % 3 != 0 { + if bytes.len() % 3 != 0 { return None; } - let capacity = input_bytes.len() / 3; - let mut output: Vec = Vec::with_capacity(capacity); - for i in (0..input_bytes.len()).step_by(6) { - output.push(*consts::PREFIXES_MAP.get(&input_bytes[i..i + 3])?); - if input_bytes.len() >= i + 6 { - output.push(*consts::SUFFIXES_MAP.get(&input_bytes[i + 3..i + 6])?); + for i in (0..bytes.len()).step_by(6) { + let j = i / 3; + bytes[j] = *consts::PREFIXES_MAP.get(&bytes[i..i + 3])?; + if bytes.len() >= i + 6 { + bytes[j + 1] = *consts::SUFFIXES_MAP.get(&bytes[i + 3..i + 6])?; } } - Some(output) + bytes.truncate(bytes.len() / 3); + Some(bytes) }