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<()> {
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) => {
o.write_all(&s.as_bytes()[..i])?;
o.write_all(emoji.as_bytes())?;
s = &s[j..];
}
None => {
o.write_all(&s.as_bytes()[..n])?;
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);
}
}