Skip to content

Instantly share code, notes, and snippets.

@kmod-midori
Created April 25, 2021 12:46
Show Gist options
  • Save kmod-midori/b714450c0f3c4c3064917cba3a89bb01 to your computer and use it in GitHub Desktop.
Save kmod-midori/b714450c0f3c4c3064917cba3a89bb01 to your computer and use it in GitHub Desktop.
use std::io::prelude::*;
use std::{fs::File, path::PathBuf};
use encoding_rs::GB18030;
fn main() -> std::io::Result<()> {
let mut input_path = std::env::args().skip(1).next().map(PathBuf::from).unwrap();
let mut in_file = File::open(&input_path)?;
let mut src = String::new();
in_file.read_to_string(&mut src)?;
let (dst, _, _) = GB18030.encode(&src);
input_path.set_file_name(format!(
"gb_{}",
input_path
.file_name()
.and_then(|s| s.to_str())
.unwrap_or("")
));
let mut out_file = File::create(&input_path)?;
out_file.write_all(&dst)?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment