Skip to content

Commit

Permalink
Check condition once instead of once per iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
alajpie committed Jun 17, 2020
1 parent 0c11a9a commit 1be04a4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 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.5"
version = "0.2.6"
authors = ["k2l8m11n2"]
edition = "2018"
description = "Encode and decode data in Urbit's @q format"
Expand Down
26 changes: 18 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,25 @@ pub fn decode(input: &str) -> Option<Vec<u8>> {
bytes.truncate(1);
return Some(bytes);
}
if bytes.len() % 3 != 0 {
return None;
}
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])?;
match bytes.len() % 6 {
0 => {
for i in (0..bytes.len()).step_by(6) {
let j = i / 3;
bytes[j] = *consts::PREFIXES_MAP.get(&bytes[i..i + 3])?;
bytes[j + 1] = *consts::SUFFIXES_MAP.get(&bytes[i + 3..i + 6])?;
}
}
3 => {
for i in (0..bytes.len() - 6).step_by(6) {
let j = i / 3;
bytes[j] = *consts::PREFIXES_MAP.get(&bytes[i..i + 3])?;
bytes[j + 1] = *consts::SUFFIXES_MAP.get(&bytes[i + 3..i + 6])?;
}
let i = bytes.len() - 3;
let j = i / 3;
bytes[j] = *consts::SUFFIXES_MAP.get(&bytes[i..i + 3])?;
}
_ => return None,
}
bytes.truncate(bytes.len() / 3);
Some(bytes)
Expand Down

0 comments on commit 1be04a4

Please sign in to comment.