Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ parking_lot = { version = "0.11.0", optional = true }
num-traits = "0.2"
num-complex = "0.4.0"
num-bigint = "0.4.2"
lexical-core = "0.7"
hexf-parse = "0.1.0"
lexical-parse-float = { version = "0.8.0", features = ["format"] }
hexf-parse = "0.2.1"
cfg-if = "1.0"
once_cell = "1.4.1"
siphasher = "0.3"
Expand Down
54 changes: 28 additions & 26 deletions common/src/float_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,32 @@ pub fn gt_int(value: f64, other_int: &BigInt) -> bool {
}

pub fn parse_str(literal: &str) -> Option<f64> {
if literal.starts_with('_') || literal.ends_with('_') {
return None;
}

let mut buf = String::with_capacity(literal.len());
let mut last_tok: Option<char> = None;
for c in literal.chars() {
if !(c.is_ascii_alphanumeric() || c == '_' || c == '+' || c == '-' || c == '.') {
return None;
}
parse_inner(literal.trim().as_bytes())
}

if !c.is_ascii_alphanumeric() {
if let Some(l) = last_tok {
if !l.is_ascii_alphanumeric() && !(c == '.' && (l == '-' || l == '+')) {
return None;
}
}
}
pub fn parse_bytes(literal: &[u8]) -> Option<f64> {
parse_inner(trim_slice(literal, |b| b.is_ascii_whitespace()))
}

if c != '_' {
buf.push(c);
}
last_tok = Some(c);
fn trim_slice<T>(v: &[T], mut trim: impl FnMut(&T) -> bool) -> &[T] {
let mut it = v.iter();
while it.clone().next().map_or(false, &mut trim) {
it.next();
}

if let Ok(f) = lexical_core::parse(buf.as_bytes()) {
Some(f)
} else {
None
while it.clone().next_back().map_or(false, &mut trim) {
it.next_back();
}
it.as_slice()
}

fn parse_inner(literal: &[u8]) -> Option<f64> {
use lexical_parse_float::{
format::PYTHON3_LITERAL, FromLexicalWithOptions, NumberFormatBuilder, Options,
};
const PYTHON_STRING: u128 = NumberFormatBuilder::rebuild(PYTHON3_LITERAL)
.no_special(false)
.build();
f64::from_lexical_with_options::<PYTHON_STRING>(literal, &Options::new()).ok()
}

pub fn is_integer(v: f64) -> bool {
Expand Down Expand Up @@ -495,3 +491,9 @@ fn test_remove_trailing_redundant_chars() {
// don't truncate integers
assert!(remove_trailing_redundant_chars(String::from("1000")) == String::from("1000"));
}

#[test]
fn test_parse_str(){
assert!(parse_str("1e1") == Some(10.0));
assert!(parse_str("1_e1") == None);
}