jsimmons/tnetstrings.lua — pull request #2 by mmirate (2012-01-22 04:31:28+00:00)
Title: Add support for distinct float ('^') and integer ('#') types in both the dumper and the parser.

Exactly what the commit message says on the tin: this commit enables the dumper and the parser to distinguish from floats and integers.

In the parser, floats and integers are distinguished by type character and by the fact that the result of parsing an integer should numerically be an integer.

In the dumper, floats and integers are distinguished by numeric quality only.

Since the tnetstrings standard does not limit the number of digits of precision in a float (or in an integer, for that matter) except by the reference implementation's reliance on Python's floats and ints, a possible future improvement would be to use an arbitrary-precision number library to store the output of any parsed number that exceeds the capability of Lua's number type (I believe it is internally a double, which has 14 decimal digits of precision).

I think it might be possible to test for an imminent number overflow using the following code, though since I'm not sure exactly which Lua bignum library should be used in such a case, said code is not actually included in the commit.

```lua
function trim_insignificant_zeroes(str)
if str:find([[.]], 1, true) then str = str:gsub([[0+$]], '') end
str = str:gsub([[^0+]], '')
if str:sub(1,1) == '.' then str = '0' .. str end
if #str > 0 and str ~= '0.0' and str ~= '0.' and str ~= '.0' then return str else return '0' end
end

function will_overflow(numberstring)
return string.len(trim_insignificant_zeroes(string.gsub(
string.gsub(numberstring, [=[[.]]=], ''),
[=[[eE][+-]?%d+$]=], ''
))) > 14
end

-- tests
for _, y in ipairs{'','0.'} do
local x = y
for n=0,20 do
x = x .. (n%10)
assert((tostring(tonumber(x)) ~= trim_insignificant_zeroes(x)) == will_overflow(x))
end
end
```