Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Losing .0 (dot zero) in unquoted strings #65

Open
ghost opened this issue Aug 2, 2022 · 1 comment
Open

Losing .0 (dot zero) in unquoted strings #65

ghost opened this issue Aug 2, 2022 · 1 comment

Comments

@ghost
Copy link

ghost commented Aug 2, 2022

With Typesafe Config (authors of HOCON):

println(
  ConfigFactory
    .parseString("a = 1.0.1-zxc")
    .getString("a")
)

Prints 1.0.1-zxc.

With hocon.rs 0.9.0:

fn main() {
    let mut loader = hocon::HoconLoader::new().strict();
    loader = loader.load_str("a = 1.0.1-zxc").unwrap();
    let parsed = loader.hocon().unwrap();
    println!("{}", parsed["a"].as_string().unwrap());
}

Prints 1.1-zxc, losing the .0 that was in the original string.

I understand there is a chance that the reference HOCON implementation by Typesafe could be wrong. Or is it perhaps a bug in hocon.rs?

@ghost ghost changed the title Losing zero in unquoted strings Losing .0 (dot zero) in unquoted strings Aug 2, 2022
@ghost
Copy link

ghost commented Dec 26, 2022

GitHub doesn't allow passwords so a pull request is not possible, but here's a fix:

diff --git a/src/parser.rs b/src/parser.rs
index 9cce91e..17eb870 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -54,10 +54,21 @@ named!(

 named!(integer<i64>, flat_map!(recognize_float, parse_to!(i64)));

+named!(
+    value_terminator<char>,
+    one_of!("$\"{}[]:=,+#`^?!@*&'\\\t\n")
+);
+
 named!(
     float<f64>,
     map!(
-        flat_map!(recognize_float, parse_to!(F64WithoutLeadingDot)),
+        flat_map!(
+            terminated!(
+                recognize_float,
+                peek!(value_terminator)
+            ),
+            parse_to!(F64WithoutLeadingDot)
+        ),
         |v| v.0
     )
 );

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants