Skip to content

Instantly share code, notes, and snippets.

@osa1
Last active June 18, 2021 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osa1/96d9b501d57ec9612c01f1276673feb3 to your computer and use it in GitHub Desktop.
Save osa1/96d9b501d57ec9612c01f1276673feb3 to your computer and use it in GitHub Desktop.
Replace '\u{...}' with '<char>'
use std::convert::TryFrom;
use std::io::{stdin, BufRead};
use std::process::exit;
use regex::{Captures, Regex};
fn main() {
let re = Regex::new(r"'\\u\{([0-9A-Fa-f]+)\}'").unwrap();
let stdin = stdin();
for line in stdin.lock().lines() {
let line = line.unwrap();
let line = re.replace_all(&line, |captures: &Captures| {
let num = captures.get(1).unwrap().as_str();
let i = match u32::from_str_radix(num, 16) {
Err(err) => {
eprintln!("Unable to parse {:?}: {}", num, err);
exit(1);
}
Ok(i) => i,
};
format!("{:?}", char::try_from(i).unwrap())
});
println!("{}", line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment