mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
NumberCodec::explicit_plus: the sign's digit in any script the reader reads
Review (CodeRabbit): the guard after the sign asked is_ascii_digit, so "+\u{663}" (Arabic-Indic 3) failed it and fell through
to the reader, which read the plus as relative: 13 for a start of 10. The guard now uses the reader's own digit class,
so any supported script's digit keeps the sign the number's, and a plain field's relative reading is unchanged.
This commit is contained in:
1 parent
03afe3f4a8
commit
a868bc616d
2 files changed
+6
-2
No files matched your search
@@ -331,7 +331,7 @@ impl NumberCodec {
|
||||
// relative `+` nor the share of the start value.
|
||||
if self.plus
|
||||
&& let Some(rest) = trimmed.strip_prefix('+')
|
||||
&& rest.trim_start().starts_with(|c: char| c.is_ascii_digit() || c == self.reader.decimal_separator())
|
||||
&& rest.trim_start().starts_with(|c: char| crate::quantity::digit(c).is_some() || c == self.reader.decimal_separator())
|
||||
{
|
||||
return self.parse_tagged_with(rest.trim_start(), &None, false);
|
||||
}
|
||||
@@ -480,6 +480,10 @@ mod review_tests {
|
||||
assert!(angle.parse("+", &Some(10.0)).is_err(), "a lone plus stays no number");
|
||||
let plain = NumberCodec::new(Reader::new(), 2);
|
||||
assert_eq!(plain.parse("+ 2", &Some(10.0)).ok(), Some(Some(12.0)), "a plain field's plus still adds to the start");
|
||||
// The digit after the sign may be written in any script the reader reads: Arabic-Indic ٣ is 3.
|
||||
assert_eq!(angle.parse("+\u{663}", &Some(10.0)).ok(), Some(Some(3.0)), "a sign before an Arabic-Indic digit is still the number's");
|
||||
assert_eq!(angle.parse("\u{663}", &Some(10.0)).ok(), Some(Some(3.0)), "and the reader reads that digit as before");
|
||||
assert_eq!(plain.parse("+\u{663}", &Some(10.0)).ok(), Some(Some(13.0)), "a plain field still adds it to the start");
|
||||
}
|
||||
|
||||
/// A share field's signed percentage is that number, not a share of the start value again.
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::codec::NotAValue;
|
||||
const DIGIT_BLOCKS: [u32; 5] = [0x30, 0x660, 0x6F0, 0x966, 0xFF10];
|
||||
|
||||
/// The value of a decimal digit of any supported script.
|
||||
fn digit(c: char) -> Option<u32> {
|
||||
pub(crate) fn digit(c: char) -> Option<u32> {
|
||||
DIGIT_BLOCKS.iter().find_map(|&zero| (zero..zero + 10).contains(&(c as u32)).then(|| c as u32 - zero))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user