Skip to content

Commit

Permalink
Ignore tildes
Browse files Browse the repository at this point in the history
  • Loading branch information
alajpie committed Jun 17, 2020
1 parent b0cf895 commit d8afe49
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ format.

Note that when encoding more than one byte, `encode` pads from the beginning to
an even number of bytes (as per the original implementation) and `decode`
ignores any dashes or spaces within the string.
ignores any dashes, tildes or spaces within the string.
```rust
urbit_q::encode(&[1]); // nec
let string = urbit_q::encode(&[1, 2, 3]); // doznec-binwes
urbit_q::decode(&string).unwrap(); // [0, 1, 2, 3]
urbit_q::decode("doz nec bin wes"); // Some([0, 1, 2, 3])
urbit_q::decode("do-z ne cb inwes"); // Some([0, 1, 2, 3])
urbit_q::decode("do-z ne cb~inwes"); // Some([0, 1, 2, 3])
urbit_q::decode("nec-binwes"); // Some([1, 2, 3])
urbit_q::decode("hello world"); // None
```
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//!
//! Note that when encoding more than one byte, `encode` pads from the beginning
//! to an even number of bytes (as per the original implementation) and `decode`
//! ignores any dashes or spaces within the string.
//! ignores any dashes, tildes or spaces within the string.
//! ```rust
//! urbit_q::encode(&[1]); // nec
//! let string = urbit_q::encode(&[1, 2, 3]); // doznec-binwes
//! urbit_q::decode(&string).unwrap(); // [0, 1, 2, 3]
//! urbit_q::decode("doz nec bin wes"); // Some([0, 1, 2, 3])
//! urbit_q::decode("do-z ne cb inwes"); // Some([0, 1, 2, 3])
//! urbit_q::decode("do-z ne cb~inwes"); // Some([0, 1, 2, 3])
//! urbit_q::decode("nec-binwes"); // Some([1, 2, 3])
//! urbit_q::decode("hello world"); // None
//! ```
Expand Down Expand Up @@ -69,18 +69,18 @@ pub fn encode(input: &[u8]) -> String {

/// Decodes data in Urbit's `@q` format
///
/// Note that it ignores any dashes or spaces within the string, e.g.
/// Note that it ignores any dashes, tildes or spaces within the string, e.g.
/// ```
/// # use urbit_q::*;
/// decode("doznec-binwes"); // Some([0, 1, 2, 3])
/// decode("doz nec bin wes"); // Some([0, 1, 2, 3])
/// decode("do-z ne cb inwes"); // Some([0, 1, 2, 3])
/// decode("do-z ne cb~inwes"); // Some([0, 1, 2, 3])
/// decode("nec-binwes"); // Some([1, 2, 3])
/// decode("hello world"); // None
/// ```
pub fn decode(input: &str) -> Option<Vec<u8>> {
let mut bytes = Vec::from(input);
bytes.retain(|x| *x != ('-' as u8) && *x != (' ' as u8));
bytes.retain(|x| *x != ('-' as u8) && *x != ('~' as u8) && *x != (' ' as u8));
if bytes.len() == 3 {
bytes[0] = *consts::SUFFIXES_MAP.get(&bytes[..])?;
bytes.truncate(1);
Expand Down
2 changes: 1 addition & 1 deletion tests/manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn readme() {
assert_eq!(string, "doznec-binwes");
assert_eq!(urbit_q::decode(&string).unwrap(), vec![0, 1, 2, 3]);
assert_eq!(urbit_q::decode("doz nec bin wes"), Some(vec![0, 1, 2, 3]));
assert_eq!(urbit_q::decode("do-z ne cb inwes"), Some(vec![0, 1, 2, 3]));
assert_eq!(urbit_q::decode("do-z ne cb~inwes"), Some(vec![0, 1, 2, 3]));
assert_eq!(urbit_q::decode("nec-binwes"), Some(vec![1, 2, 3]));
assert_eq!(urbit_q::decode("hello world"), None);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/proptest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ proptest! {
decode(&s);
}
#[test]
fn doesnt_crash_ascii(s in "[a-z- ]*") {
fn doesnt_crash_ascii(s in "[a-z- ~]*") {
decode(&s);
}

Expand Down

0 comments on commit d8afe49

Please sign in to comment.