Skip to content

Commit

Permalink
support 两, imporve the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Dec 11, 2023
1 parent 0585a2e commit b54c306
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chinese-number"
version = "0.7.4"
version = "0.7.5"
authors = ["Magic Len <len@magiclen.org>"]
edition = "2021"
rust-version = "1.58"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ assert_eq!(1000000u64, "一兆".to_number(ChineseCountMethod::Low).unwrap());
assert_eq!(1000000000000u64, "一兆".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(10000000000000000u64, "一兆".to_number(ChineseCountMethod::Middle).unwrap());
assert_eq!(10000000000000000u64, "一兆".to_number(ChineseCountMethod::High).unwrap());

assert_eq!(120u64, "一百二".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(2300u64, "兩千三".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(34000u64, "三萬四".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(105000u64, "十萬五".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(150000000u64, "一億五".to_number(ChineseCountMethod::TenThousand).unwrap());
```

## No Std
Expand Down
2 changes: 1 addition & 1 deletion src/chinese_characters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl ChineseNumber {
match character {
'零' | '0' | '〇' => Some(Self::),
'一' | '壹' | '1' => Some(Self::),
'二' | '貳' | '贰' | '貮' | '兩' | '2' => Some(Self::),
'二' | '貳' | '贰' | '貮' | '兩' | '两' | '2' => Some(Self::),
'三' | '參' | '叁' | '叄' | '参' | '3' => Some(Self::),
'四' | '肆' | '4' => Some(Self::),
'五' | '伍' | '5' => Some(Self::),
Expand Down
27 changes: 27 additions & 0 deletions src/chinese_to_number/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,33 @@ pub(crate) fn chinese_to_unsigned_integer(
exp
},
_ => {
if length >= 3 {
// look ahead

let left_char = chars[pointer - 1];

if let Some(ChineseNumber::) = ChineseNumber::from_char(left_char) {
// do nothing
} else {
match ChineseExponent::from_char(left_char) {
Some(exp) if exp >= ChineseExponent::百 => {
let high = chinese_to_unsigned_integer(method, &chars[..pointer])?;

let low = chinese_to_unsigned_integer_unit(
method,
&chars[pointer..],
0,
exp,
)?
.0 / 10;

return high.checked_add(low).ok_or(ChineseToNumberError::Overflow);
},
_ => (),
}
}
}

pointer += 1;
ChineseExponent::
},
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ assert_eq!(1000000u64, "一兆".to_number(ChineseCountMethod::Low).unwrap());
assert_eq!(1000000000000u64, "一兆".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(10000000000000000u64, "一兆".to_number(ChineseCountMethod::Middle).unwrap());
assert_eq!(10000000000000000u64, "一兆".to_number(ChineseCountMethod::High).unwrap());
assert_eq!(120u64, "一百二".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(2300u64, "兩千三".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(34000u64, "三萬四".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(105000u64, "十萬五".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(150000000u64, "一億五".to_number(ChineseCountMethod::TenThousand).unwrap());
# }
```
Expand Down

0 comments on commit b54c306

Please sign in to comment.