-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathreplace.rs
More file actions
73 lines (68 loc) · 2.04 KB
/
replace.rs
File metadata and controls
73 lines (68 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Replaces occurrences of `:name:` with the actual emoji for `name` in text.
//!
//! # Usage
//!
//! ```sh
//! $ echo "launch :rocket:" | cargo run --example replace
//! launch 🚀
//! ```
use std::io;
use std::io::prelude::*;
use std::io::BufWriter;
fn main() -> io::Result<()> {
let stdin = {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf)?;
buf
};
replace(&stdin, BufWriter::new(io::stdout()))
}
fn replace(mut s: &str, mut o: impl Write) -> io::Result<()> {
// The meaning of the index values is as follows.
//
// : r o c k e t :
// ^ ^ ^ ^
// i m n j
//
// i..j gives ":rocket:"
// m..n gives "rocket"
while let Some((i, m, n, j)) = s
.find(':')
.map(|i| (i, i + 1))
.and_then(|(i, m)| s[m..].find(':').map(|x| (i, m, m + x, m + x + 1)))
{
match emojis::get_by_shortcode(&s[m..n]) {
Some(emoji) => {
// Output everything preceding, except the first colon.
o.write_all(&s.as_bytes()[..i])?;
// Output the emoji.
o.write_all(emoji.as_bytes())?;
// Update the string to past the last colon.
s = &s[j..];
}
None => {
// Output everything preceding but not including the colon.
o.write_all(&s.as_bytes()[..n])?;
// Update the string to start with the last colon.
s = &s[n..];
}
}
}
o.write_all(s.as_bytes())
}
#[test]
fn smoke() {
let tests = [
("launch nothing", "launch nothing"),
("launch :rocket: something", "launch 🚀 something"),
("? :unknown: emoji", "? :unknown: emoji"),
("::very:naughty::", "::very:naughty::"),
(":maybe:rocket:", ":maybe🚀"),
(":rocket::rocket:", "🚀🚀"),
];
for (i, o) in tests {
let mut v = Vec::new();
replace(i, &mut v).unwrap();
assert_eq!(std::str::from_utf8(&v).unwrap(), o);
}
}