Skip to content

Commit

Permalink
fix: utf8 replacements (#26)
Browse files Browse the repository at this point in the history
* fix: utf8 replacements

* fix
  • Loading branch information
Terkwood authored Jun 11, 2022
1 parent 1fd0a8e commit 053be3e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 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 = "zeditor"
version = "0.1.0-very-well-done"
version = "0.1.0-walemoji"
edition = "2021"

[dependencies]
Expand Down
22 changes: 20 additions & 2 deletions src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,19 @@ fn replace_text(text: &str, replacements: &[Replacement]) -> String {

let mut out = String::new();
let mut last: usize = 0;

// we need to take care not to split unicode characters in the
// middle of their sequences
// see https://stackoverflow.com/a/51983601/9935916
let utf8 = text.chars().collect::<Vec<_>>();
for r in rs {
out.push_str(&text[last..r.start]);
out.push_str(&utf8[last..r.start].iter().cloned().collect::<String>());
out.push_str(&r.term);

last = r.end;
}

out.push_str(&text[last..]);
out.push_str(&utf8[last..].iter().cloned().collect::<String>());

out
}
Expand Down Expand Up @@ -163,4 +168,17 @@ mod tests {
let expected = "a quick brown dog jumped over a tiny turtle";
assert_eq!(replace_text(input, &replacements), expected.to_string());
}

#[test]
fn utf8_test() {
let input = "๐ŸŒŽ๐ŸŒ๐Ÿถ๐Ÿฎ๐Ÿ’˜";
let replacements = vec![Replacement {
term: "foo".to_string(),
start: 1,
end: 2,
}];

let expected = "๐ŸŒŽfoo๐Ÿถ๐Ÿฎ๐Ÿ’˜";
assert_eq!(replace_text(input, &replacements), expected.to_string());
}
}

0 comments on commit 053be3e

Please sign in to comment.