feat: support scientific notation and decimal separator (#1688)

This commit is contained in:
Kelvin 2023-01-10 10:28:35 +08:00 committed by GitHub
parent 91efcafd77
commit c46b09f182
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -100,10 +100,27 @@ impl NumberTypeOptionPB {
pub(crate) fn format_cell_data(&self, s: &str) -> FlowyResult<NumberCellData> {
match self.format {
NumberFormat::Num => {
let strnum = NUM_REGEX.replace_all(s, "");
match Decimal::from_str(&strnum) {
Ok(value, ..) => Ok(NumberCellData::from_decimal(value)),
Err(_) => Ok(NumberCellData::new()),
if SCIENTIFIC_NOTATION_REGEX.is_match(s).unwrap() {
match Decimal::from_scientific(&s.to_lowercase()) {
Ok(value, ..) => Ok(NumberCellData::from_decimal(value)),
Err(_) => Ok(NumberCellData::new()),
}
} else {
let draw_numer_string = NUM_REGEX.replace_all(s, "");
let strnum = match draw_numer_string.matches(".").count() {
0 | 1 => draw_numer_string.to_string(),
_ => match EXTRACT_NUM_REGEX.captures(&draw_numer_string) {
Ok(captures) => match captures {
Some(capture) => capture[1].to_string(),
None => "".to_string(),
},
Err(_) => "".to_string(),
},
};
match Decimal::from_str(&strnum) {
Ok(value, ..) => Ok(NumberCellData::from_decimal(value)),
Err(_) => Ok(NumberCellData::new()),
}
}
}
_ => NumberCellData::from_format_str(s, self.sign_positive, &self.format),
@ -214,3 +231,11 @@ impl std::default::Default for NumberTypeOptionPB {
lazy_static! {
static ref NUM_REGEX: Regex = Regex::new(r"[^\d\.]").unwrap();
}
lazy_static! {
static ref SCIENTIFIC_NOTATION_REGEX: Regex = Regex::new(r"([+-]?\d*\.?\d+)e([+-]?\d+)").unwrap();
}
lazy_static! {
static ref EXTRACT_NUM_REGEX: Regex = Regex::new(r"^(\d+\.\d+)(?:\.\d+)*$").unwrap();
}