Skip to content

Commit

Permalink
fix: panic when skipping char at boundary in convert_to_with (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowee committed Jan 11, 2025
1 parent 0af3a19 commit ed56498
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,12 @@ impl ZhConverter {
// not shadowed: pick a word in original automaton
if shadowed_source_words.contains(a.2) {
// source word is disabled: skip one char and re-search
let first_char_len = text.chars().next().unwrap().len_utf8();
//
// NOTE: In case there are two rules like `{abcd -> foo, abc -> bar}`,
// even if the former is disabled, the latter won't take effect, since `a`
// will be skipped and the next search start at `bc`.
// It is inevitable if we do not re-build a new automaton every time.
let first_char_len = text[a.0..].chars().next().unwrap().len_utf8();
(
last,
a.0 + first_char_len,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
//! assert_eq!(zhconv_mw("-{zh-tw:鼠麴草;zh-cn:香茅}-是菊科草本植物。", Variant::ZhCN), "香茅是菊科草本植物。");
//! assert_eq!(zhconv_mw("菊科草本植物包括-{zh-tw:鼠麴草;zh-cn:香茅;}-等。", Variant::ZhTW), "菊科草本植物包括鼠麴草等。");
//! assert_eq!(zhconv_mw("-{H|zh:馬;zh-cn:鹿;}-馬克思主義", Variant::ZhCN), "鹿克思主义"); // global rule
//! assert_eq!(zhconv_mw("&二極體\n-{-|zh-hans:二极管; zh-hant:二極體}-\n", Variant::ZhCN), "&二极体\n\n")
//! ```
//!
//! To load or add additional conversion rules such as CGroups or `(FROM, TO)` pairs,
Expand Down
5 changes: 4 additions & 1 deletion src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ impl FromStr for ConvRule {
match flag {
// FIX: 'A'
'+' => action = Some(Action::Add),
'-' => action = Some(Action::Remove),
'-' => action = {
output = None;
Some(Action::Remove)
},
// no conv, just display the inner as-is
'R' => {
return Ok(ConvRule {
Expand Down

0 comments on commit ed56498

Please sign in to comment.