Skip to content

Commit a5245a0

Browse files
authored
fix(number): do not quote floating point numbers (#5)
1 parent ffad81e commit a5245a0

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/lexer.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@ fn fix_str(s: Cow<'_, str>) -> Cow<'_, str> {
3737
.collect()
3838
}
3939

40+
fn is_numeric(s: &str) -> bool {
41+
let c = match s.chars().next() {
42+
Some('+') | Some('-') | Some('.') => s.chars().nth(1),
43+
Some(c) => Some(c),
44+
None => None,
45+
};
46+
c.unwrap_or_default().is_ascii_digit()
47+
}
48+
4049
fn fix_else(s: Cow<'_, str>) -> Cow<'_, str> {
4150
match s.to_lowercase().as_str() {
4251
"null" | "nil" | "nul" | "none" => "null".into(),
4352
"true" => "true".into(),
4453
"false" => "false".into(),
4554
&_ => {
46-
if s.chars().all(|c| c.is_numeric()) {
55+
if is_numeric(&s) {
4756
s
4857
} else {
4958
fix_str(s)

src/main.rs

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ mod tests {
5353
assert_eq!(value, process(value));
5454
}
5555

56+
#[test]
57+
fn floats() {
58+
let value = r#"{"float":1.4,"with_sign":-2}"#;
59+
assert_eq!(value, process(value));
60+
}
61+
5662
#[test]
5763
fn valid() {
5864
let value = r#"{"a":3,"b": 4}"#;

0 commit comments

Comments
 (0)