diff --git a/Cargo.lock b/Cargo.lock index 8587b68..75a569d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -701,7 +701,7 @@ checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" [[package]] name = "urbit-q" -version = "0.2.5" +version = "0.2.6" dependencies = [ "criterion", "phf", diff --git a/Cargo.toml b/Cargo.toml index 5197887..254ea7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index d494c07..f530c00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,15 +84,25 @@ pub fn decode(input: &str) -> Option> { 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)