Skip to content

Commit

Permalink
chore: fix new clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowee committed Mar 22, 2024
1 parent cebd3bd commit bd8d3a0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# zhconv-rs 中文简繁及地區詞轉換
zhconv-rs converts Chinese text among traditional/simplified scripts or regional variants (e.g. `zh-TW <-> zh-CN <-> zh-HK <-> zh-Hans <-> zh-Hant`), built on the top of rulesets from MediaWiki/Wikipedia and OpenCC.

The implementation is powered by an [Aho-Corasick](https://github.com/daac-tools/daachorse) automaton, ensuring linear time complexity with respect to the length of input text and conversion rules (`O(n+m)`), processing dozens of MiBs text per second.
The implementation is powered by the [Aho-Corasick](https://github.com/daac-tools/daachorse) algorithm, ensuring linear time complexity with respect to the length of input text and conversion rules (`O(n+m)`), processing dozens of MiBs text per second.

🔗 **Web App: https://zhconv.pages.dev** (powered by WASM)

Expand Down
6 changes: 2 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

use std::collections::HashSet;
use std::convert::TryInto;
use std::env;
use std::fs::{self, File};
use std::io;
Expand Down Expand Up @@ -358,7 +357,6 @@ mod opencc {
use daachorse::{
CharwiseDoubleArrayAhoCorasick, CharwiseDoubleArrayAhoCorasickBuilder, MatchKind,
};
use itertools::Itertools;
// use aho_corasick::{AhoCorasick, AhoCorasickBuilder, MatchKind};
use lazy_static::lazy_static;
use std::collections::HashMap;
Expand Down Expand Up @@ -429,7 +427,7 @@ mod opencc {
s: &str,
) {
for line in s.lines().map(|l| l.trim()).filter(|l| !l.is_empty()) {
if let Some((f, ts)) = line.split_once(char::is_whitespace){
if let Some((f, ts)) = line.split_once(char::is_whitespace) {
if f.is_empty() || ts.is_empty() {
continue;
}
Expand Down Expand Up @@ -528,5 +526,5 @@ fn read_and_validate_file(path: &str, sha256sum: &[u8; 32]) -> String {
fn sha256(text: &str) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(text.as_bytes());
hasher.finalize().try_into().unwrap()
hasher.finalize().into()
}
24 changes: 15 additions & 9 deletions src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
pagerules::PageRules,
rule::{Conv, ConvAction, ConvRule},
tables::expand_table,
utils::{regex, unwrap_or_return},
utils::regex,
variant::Variant,
};

Expand Down Expand Up @@ -98,10 +98,13 @@ impl ZhConverter {

/// Same as `convert`, except that it takes a `&mut String` as dest instead of returning a `String`.
pub fn convert_to(&self, text: &str, output: &mut String) {
let automaton = unwrap_or_return!(self.automaton.as_ref().or_else(|| {
output.push_str(text);
None
}));
let automaton = match self.automaton.as_ref() {
Some(automaton) => automaton,
None => {
output.push_str(text);
return;
}
};

// Ref: https://github.dev/rust-lang/regex/blob/5197f21287344d2994f9cf06758a3ea30f5a26c3/src/re_trait.rs#L192
let mut last = 0;
Expand Down Expand Up @@ -186,10 +189,13 @@ impl ZhConverter {
shadowing_target_words: &[String],
shadowed_source_words: &HashSet<String>,
) {
let automaton = unwrap_or_return!(self.automaton.as_ref().or_else(|| {
output.push_str(text);
None
}));
let automaton = match self.automaton.as_ref() {
Some(automaton) => automaton,
None => {
output.push_str(text);
return;
}
};

// let mut cnt = HashMap::<usize, usize>::new();
let mut last = 0;
Expand Down
32 changes: 16 additions & 16 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ macro_rules! regex {
}
pub(crate) use regex;

// https://stackoverflow.com/a/51345372/5488616
macro_rules! unwrap_or_return {
( $e:expr ) => {
match $e {
Some(x) => x,
None => return,
}
};
( $e:expr, $r:expr ) => {
match $e {
Some(x) => x,
None => return $r,
}
};
}
pub(crate) use unwrap_or_return;
// // https://stackoverflow.com/a/51345372/5488616
// macro_rules! unwrap_or_return {
// ( $e:expr ) => {
// match $e {
// Some(x) => x,
// None => return,
// }
// };
// ( $e:expr, $r:expr ) => {
// match $e {
// Some(x) => x,
// None => return $r,
// }
// };
// }
// pub(crate) use unwrap_or_return;

0 comments on commit bd8d3a0

Please sign in to comment.