Skip to content

Commit

Permalink
fill dictionary in bytes style
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Oct 2, 2023
1 parent 9dbac3b commit c3f6d51
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 33 deletions.
20 changes: 0 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ macro = { path = "src/macro" }
retour = { version = "0.3.1", features = ["static-detour"] }
log = "0.4.20"
simple-logging = "2.0.2"
encoding_rs = "*"
encoding_rs_io = "*"
regex = "*"
toml = "0.8.1"
backtrace = "0.3.69"
Expand Down
22 changes: 11 additions & 11 deletions src/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::collections::HashMap;
use std::io::prelude::*;

use encoding_rs::WINDOWS_1251;
use encoding_rs_io::DecodeReaderBytesBuilder;
use regex::Regex;

use crate::config::CONFIG;
use crate::utils;

Expand Down Expand Up @@ -41,21 +37,25 @@ impl Dictionary {
self.map.capacity()
}

pub fn _data(&self) -> &HashMap<String, Vec<u8>> {
&self.map
}

pub fn _reload(&mut self) -> Result<(), Box<dyn std::error::Error>> {
self.map = Self::load(self.path)?;
Ok(())
}

#[allow(unused_must_use)]
fn load(path: &str) -> Result<HashMap<String, Vec<u8>>, Box<dyn std::error::Error>> {
let file = std::fs::File::open(path)?;
let mut reader = DecodeReaderBytesBuilder::new().encoding(Some(WINDOWS_1251)).build(file);
let mut contents = String::new();
reader.read_to_string(&mut contents)?;
let mut file = std::fs::File::open(path)?;
let mut contents: Vec<u8> = Vec::new();
file.read_to_end(&mut contents);
let mut map = HashMap::<String, Vec<u8>>::new();
for item in Regex::new(r#""(.+)","(.+)""#)?.captures_iter(&contents) {
let mut v = Vec::from(WINDOWS_1251.encode(item.get(2).unwrap().as_str()).0.as_ref());
for item in regex::bytes::Regex::new(r#"(?-u)"(.+)","(.+)""#)?.captures_iter(&contents) {
let mut v = Vec::<u8>::from(&item[2]);
v.push(0);
map.insert(String::from(item.get(1).unwrap().as_str()), v);
map.insert(String::from_utf8_lossy(&item[1]).to_string(), v);
}
Ok(map)
}
Expand Down

0 comments on commit c3f6d51

Please sign in to comment.