Skip to content

Commit

Permalink
Improve code (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee authored Jun 1, 2024
1 parent a45944f commit c079712
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 66 deletions.
13 changes: 7 additions & 6 deletions autocorrect/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// autocorrect: false
use crate::code::{self, Results};
use crate::{
code::{self},
rule::format_or_lint,
};

/// Automatically add spaces between Chinese and English words.
///
Expand All @@ -20,13 +23,11 @@ use crate::code::{self, Results};
/// // => "既に、世界中の数百という企業が Rust を採用し、高速で低リソースのクロスプラットフォームソリューションを実現しています。"
/// ```
pub fn format(text: &str) -> String {
let result = crate::rule::format_or_lint(text, false);
result.out
format_or_lint(text, false).out
}

/// Format a html content.
///
///
/// Example:
///
/// ```
Expand All @@ -44,8 +45,8 @@ pub fn format(text: &str) -> String {
/// autocorrect::format_html(html);
/// ```
#[deprecated(since = "2.0.0", note = "Please use `format_for` instead")]
pub fn format_html(html_str: &str) -> String {
code::format_html(html_str).to_string()
pub fn format_html(html: &str) -> String {
code::format_html(html).out
}

#[cfg(test)]
Expand Down
8 changes: 5 additions & 3 deletions autocorrect/src/ignorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ static GITIGNORE: &str = ".gitignore";
impl Ignorer {
pub fn new(work_dir: &str) -> Ignorer {
let mut builder = ignore::gitignore::GitignoreBuilder::new(work_dir);
builder.add(Path::join(Path::new(work_dir), AUTOCORRECTIGNORE));
builder.add(Path::join(Path::new(work_dir), GITIGNORE));
let ignorer = builder.build().unwrap();
let work_dir = Path::new(work_dir);

builder.add(work_dir.join(AUTOCORRECTIGNORE));
builder.add(work_dir.join(GITIGNORE));
let ignorer = builder.build().expect("failed to build ignorer");

// println!("---- {:?}", ignorer.len());

Expand Down
1 change: 1 addition & 0 deletions autocorrect/src/result/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub trait Results {
fn push(&mut self, line_result: LineResult);
fn ignore(&mut self, str: &str);
fn error(&mut self, err: &str);
#[allow(unused)]
fn to_string(&self) -> String;
fn is_lint(&self) -> bool;
fn get_toggle(&self) -> toggle::Toggle;
Expand Down
49 changes: 18 additions & 31 deletions autocorrect/src/rule/fullwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lazy_static! {
";" => ";",
":" => ":",
"!" => "!",
"?" => "?"
"?" => "?",
);
static ref PUNCTUATION_WITH_LEFT_CJK_RE: Regex = regexp!(
"{}{}{}",
Expand All @@ -31,43 +31,30 @@ lazy_static! {
}

// fullwidth correct punctuations near the CJK chars
pub fn format(text: &str) -> String {
let mut out = String::from(text);

out = PUNCTUATION_WITH_LEFT_CJK_RE
.replace_all(&out, |cap: &regex::Captures| {
fullwidth_replace_part(&cap[0])
})
.to_string();
pub fn format<'h>(text: &'h str) -> String {
let out = PUNCTUATION_WITH_LEFT_CJK_RE.replace_all(&text, |cap: &regex::Captures| {
fullwidth_replace_part(&cap[0])
});

out = PUNCTUATION_WITH_RIGHT_CJK_RE
.replace_all(&out, |cap: &regex::Captures| {
fullwidth_replace_part(&cap[0])
})
.to_string();
let out = PUNCTUATION_WITH_RIGHT_CJK_RE.replace_all(&out, |cap: &regex::Captures| {
fullwidth_replace_part(&cap[0])
});

out = PUNCTUATION_WITH_SPEICAL_CJK_RE
.replace_all(&out, |cap: &regex::Captures| {
fullwidth_replace_part(&cap[0])
})
.to_string();
let out = PUNCTUATION_WITH_SPEICAL_CJK_RE.replace_all(&out, |cap: &regex::Captures| {
fullwidth_replace_part(&cap[0])
});

out = PUNCTUATION_WITH_SPEICAL_LAST_CJK_RE
.replace_all(&out, |cap: &regex::Captures| {
fullwidth_replace_part(&cap[0])
})
.to_string();
let out = PUNCTUATION_WITH_SPEICAL_LAST_CJK_RE.replace_all(&out, |cap: &regex::Captures| {
fullwidth_replace_part(&cap[0])
});

out
out.to_string()
}

fn fullwidth_replace_part(part: &str) -> String {
let out = PUNCTUATIONS_RE.replace_all(part, |cap: &regex::Captures| {
let str = &cap[0];
return FULLWIDTH_MAPS[String::from(str).trim()];
});

out.to_string()
PUNCTUATIONS_RE
.replace_all(part, |cap: &regex::Captures| FULLWIDTH_MAPS[&cap[0].trim()])
.to_string()
}

#[cfg(test)]
Expand Down
9 changes: 3 additions & 6 deletions autocorrect/src/rule/halfwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl CharMatching for char {
}

fn is_alphanumeric_or_space(&self) -> bool {
self.is_ascii_alphanumeric() || self.eq(&' ') || self.eq(&'\t')
self.is_ascii_alphanumeric() || matches!(self, ' ' | '\t')
}
}

Expand Down Expand Up @@ -145,11 +145,8 @@ pub fn format_word(text: &str) -> String {
}

// Fix 12:00 -> 12:00
out = HALF_TIME_RE
.replace_all(&out, |cap: &regex::Captures| cap[0].replace(':', ":"))
.to_string();

out
let out = HALF_TIME_RE.replace_all(&out, |cap: &regex::Captures| cap[0].replace(':', ":"));
out.to_string()
}

fn is_may_only_english(text: &str) -> bool {
Expand Down
34 changes: 14 additions & 20 deletions autocorrect/src/rule/strategery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,25 @@ impl Strategery {
}

fn add_space(&self, text: &str) -> String {
let mut out = self.add_space_re.replace_all(text, "$1 $2").to_string();

if self.reverse {
out = self
.add_space_reverse_re
.replace_all(&out, "$1 $2")
.to_string();
let out = self.add_space_re.replace_all(text, "$1 $2");
if !self.reverse {
return out.to_string();
}

out
let out = self
.add_space_reverse_re
.replace_all(&out, "$1 $2")
.to_string();
out.to_string()
}

fn remove_space(&self, text: &str) -> String {
// println!("--- remove space before:`{}`", text);
let mut out = self.remove_space_re.replace_all(text, "$1$2").to_string();
// println!("--- remove space after1:`{}`", out);

if self.reverse {
out = self
.remove_space_reverse_re
.replace_all(&out, "$1$2")
.to_string();
let out = self.remove_space_re.replace_all(text, "$1$2");
if !self.reverse {
return out.to_string();
}
// println!("--- remove space after2:`{}`", self.remove_space_reverse_re);
// println!("--- remove space after2:`{}`", out);
out

let out = self.remove_space_reverse_re.replace_all(&out, "$1$2");
out.to_string()
}
}

0 comments on commit c079712

Please sign in to comment.