Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions autocorrect/src/code/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub fn get_file_extension(filename: &str) -> String {
return String::from(filename);
}

let filename = filename.split('/').next_back().unwrap().to_string();
let filename = filename.split('/').next_back().unwrap_or(filename).to_string();
let path_parts: Vec<&str> = filename.split('.').collect();
let mut ext: String = path_parts.last().unwrap().to_string();
let mut ext: String = path_parts.last().unwrap_or(&"").to_string();

let part_len = path_parts.len();
if part_len > 2 {
Expand Down
15 changes: 11 additions & 4 deletions autocorrect/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ where
pub fn load(config_str: &str) -> Result<Config, Error> {
let config: Config = Config::from_str(config_str)?;

let new_config: Config = CURRENT_CONFIG.write().unwrap().merge(&config)?;
let new_config: Config = CURRENT_CONFIG
.write()
.expect("Failed to acquire write lock on CURRENT_CONFIG")
.merge(&config)?;

Ok(new_config)
}
Expand Down Expand Up @@ -129,7 +132,11 @@ impl From<std::string::String> for Error {

impl Config {
pub fn current() -> Rc<RwLockReadGuard<'static, Config>> {
Rc::new(CURRENT_CONFIG.read().unwrap())
Rc::new(
CURRENT_CONFIG
.read()
.expect("Failed to acquire read lock on CURRENT_CONFIG"),
)
}

pub fn get_file_type(&self, ext: &str) -> Option<&str> {
Expand All @@ -153,8 +160,8 @@ impl Config {
}

pub fn merge(&mut self, config: &Config) -> Result<Config, Error> {
for (k, v) in config.rules.clone() {
self.rules.insert(k, v);
for (k, v) in &config.rules {
self.rules.insert(k.clone(), *v);
}

// DEPRECATED: since 2.0.0, remove this in 3.0.0
Expand Down
24 changes: 14 additions & 10 deletions autocorrect/src/rule/fullwidth.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
// autocorrect: false
use regex::Regex;
use std::{borrow::Cow, collections::HashMap};
use std::borrow::Cow;

const SPECIAL_PUNCTUATIONS: &str = "[.:!]([ ]*)";
const NORMAL_PUNCTUATIONS: &str = "[,?]([ ]*)";

lazy_static! {
static ref FULLWIDTH_MAPS: HashMap<&'static str, &'static str> = map!(
"," => ",",
"." => "。",
";" => ";",
":" => ":",
"!" => "!",
"?" => "?",
);
static ref PUNCTUATION_WITH_LEFT_CJK_RE: Regex = regexp!(
"{}{}{}",
r"[\p{CJ}\w\d]+",
Expand Down Expand Up @@ -57,13 +49,25 @@ pub fn format(text: &str) -> Cow<'_, str> {

fn fullwidth_replace_part(part: &str) -> String {
PUNCTUATIONS_RE
.replace_all(part, |cap: &regex::Captures| FULLWIDTH_MAPS[&cap[0].trim()])
.replace_all(part, |cap: &regex::Captures| {
let key = cap[0].trim();
match key {
"," => Cow::Borrowed(","),
"." => Cow::Borrowed("。"),
";" => Cow::Borrowed(";"),
":" => Cow::Borrowed(":"),
"!" => Cow::Borrowed("!"),
"?" => Cow::Borrowed("?"),
_ => Cow::Owned(key.to_string()),
}
})
.to_string()
}

#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;

#[track_caller]
fn assert_cases(cases: HashMap<&str, &str>) {
Expand Down
Loading