Last active
June 18, 2021 08:41
-
-
Save osa1/96d9b501d57ec9612c01f1276673feb3 to your computer and use it in GitHub Desktop.
Replace '\u{...}' with '<char>'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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