Skip to content

Commit

Permalink
allow whitespace in spelling codec
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotcubit committed Jul 30, 2021
1 parent 0f50e83 commit 54b0deb
Show file tree
Hide file tree
Showing 3 changed files with 18 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 = "rc"
version = "0.3.0"
version = "0.3.1"
authors = ["Elliot Cubit <[email protected]>"]
description = "Text encoding swiss army knife"
edition = "2018"
Expand Down
28 changes: 16 additions & 12 deletions src/codecs/spelling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ impl Codec for SpellingCodec {

fn encode(&self, data: Vec<u8>) -> Result<String, Error> {
match String::from_utf8(data.clone()) {
Ok(s) if s.chars().all(|c| c.is_ascii_alphabetic()) => Ok(s
.trim()
.to_ascii_lowercase()
.split_ascii_whitespace()
.map(|word| {
word.chars()
.map(Self::char_to_word)
.collect::<Vec<String>>()
.join(" ")
})
.collect::<Vec<String>>()
.join(" ")),
Ok(s)
if s.chars()
.all(|c| c.is_ascii_alphabetic() || c.is_ascii_whitespace()) =>
{
Ok(s.trim()
.to_ascii_lowercase()
.split_ascii_whitespace()
.map(|word| {
word.chars()
.map(Self::char_to_word)
.collect::<Vec<String>>()
.join(" ")
})
.collect::<Vec<String>>()
.join(" "))
}
Ok(_) => Err(Error::new("input data is not ascii".to_string())),
Err(_) => Err(Error::new("input data is not utf8".to_string())),
}
Expand Down

0 comments on commit 54b0deb

Please sign in to comment.