Skip to content

Commit

Permalink
Use rchunks_exact for a performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
alajpie committed Jun 16, 2020
1 parent 103fdb7 commit 8ce9d3d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 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.1.1"
version = "0.1.2"
authors = ["k2l8m11n2"]
edition = "2018"
description = "Encode and decode data in Urbit's @q format"
Expand Down
21 changes: 11 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ pub fn encode(input: &[u8]) -> String {
output.push_str(consts::PREFIXES[0]);
}
let mut dashes_placed = 0;
for pair in input.rchunks(2).rev() {
match pair.len() {
1 => {
output.push_str(consts::SUFFIXES[pair[0] as usize]);
}
2 => {
output.push_str(consts::PREFIXES[pair[0] as usize]);
output.push_str(consts::SUFFIXES[pair[1] as usize]);
}
_ => unreachable!(),
let iter = input.rchunks_exact(2);
let remainder = iter.remainder();
if remainder.len() != 0 {
output.push_str(consts::SUFFIXES[remainder[0] as usize]);
if dashes_placed != dashes {
output.push('-');
dashes_placed += 1;
}
}
for pair in iter.rev() {
output.push_str(consts::PREFIXES[pair[0] as usize]);
output.push_str(consts::SUFFIXES[pair[1] as usize]);
if dashes_placed != dashes {
output.push('-');
dashes_placed += 1;
Expand Down

0 comments on commit 8ce9d3d

Please sign in to comment.