Skip to content

Commit

Permalink
Reuse a Vec to avoid an allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
alajpie committed Jun 17, 2020
1 parent 19b8999 commit 0c11a9a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
26 changes: 14 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,23 @@ pub fn encode(input: &[u8]) -> String {
/// decode("hello world"); // None
/// ```
pub fn decode(input: &str) -> Option<Vec<u8>> {
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<u8> = 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)
}

0 comments on commit 0c11a9a

Please sign in to comment.