chiark / gitweb /
TOML::Tiny: Try to be more faithful for small decimals
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 4 May 2020 00:19:49 +0000 (01:19 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 5 May 2020 20:10:56 +0000 (21:10 +0100)
Ideally, if we read a TOML file and write it back out again, we get a
semantically equivalent TOML file.  Here we improve the situation
for smallish deciaml integers.

We check for values that round trip from string through a perl integer
and back to the same string; if they do, return that integer rather
than the string.

The main point of this check is to avoid losing information if the
value is too large to fit into a Perl integer, which might be only
32-bit.

This technique is not perfect: it can't cope with hex, octal or
binary, or with larger values that would need bignums.  But it is an
improvement and probably the best that can be done without
unreasonable effort.

README.pod
lib/TOML/Tiny.pm
lib/TOML/Tiny/Tokenizer.pm

index 4b0d40cfa684dba1fe81ee864ab972fd10bd6df5..78a96b9cff74acdf80bde26623d8e77026b01086 100644 (file)
@@ -134,8 +134,9 @@ If you wish to override this, you can provide your own routine to generate value
 =item inflate_integer
 
 TOML integers are 64 bit and may not match the size of the compiled perl's
-internal integer type. By default, integers are left as-is as perl strings
-which may be upgraded as needed by the caller.
+internal integer type. By default, integers other than smallish
+decimal integers are left as-is as perl strings which may be upgraded
+as needed by the caller.
 
   my $parser = TOML::Tiny->new(
     inflate_integer => sub{
index 4597fe8f93fb4e60ba0cfa8362f1ed751fee5935..e68f548a776bdbb63c40f9b64e6bb4935e505ef3 100644 (file)
@@ -191,8 +191,9 @@ If you wish to override this, you can provide your own routine to generate value
 =item inflate_integer
 
 TOML integers are 64 bit and may not match the size of the compiled perl's
-internal integer type. By default, integers are left as-is as perl strings
-which may be upgraded as needed by the caller.
+internal integer type. By default, integers other than smallish
+decimal integers are left as-is as perl strings which may be upgraded
+as needed by the caller.
 
   my $parser = TOML::Tiny->new(
     inflate_integer => sub{
index e2deb91d6ae213328149d0200a071746735178e1..b9b3ee414a13f7f1575a1bcb7650721803b693e2 100644 (file)
@@ -178,7 +178,7 @@ sub tokenize_float {
 
 sub tokenize_integer {
   $_[1] =~ tr/_+//d;
-  $_[1];
+  $_[1] !~ m/^0[xob]/ && $_[1] + 0 eq $_[0] ? $_[1] + 0 : "$_[1]"
 }
 
 sub tokenize_string {