lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

It is reasonable to warn user about "string.sub" usage mistakes such as
   str:sub(1, 3.14)
that's why in Lua 5.3 string.sub might raise "number has no integer representation" error.
But it appears that math.huge as argument also raises this error.
IMO, the following use cases should be considered as being valid:
   string.sub("abc", 2, math.huge)
   string.sub("abc", -math.huge, -2)
Yes, I know we could use -1 and 1 values respectively to get the same results,
but infinite values are suitable for the semantic of the function.
It also looks strange that
   ("abc"):sub(2, 10^15)
   ("abc"):sub(-10^15, -2)
are OK, but
   ("abc"):sub(2, 10^155)
   ("abc"):sub(-10^155, -2)
raise the error.

The suggestion:
1) any float position beyond integer range should be treated as infinite value;
2) infinite value should be considered valid despite of not having integer representation.
A small bonus (a hidden surprise for large RAM users): string.rep is unable to repeat 2^31 times.
(Tested on Lua 5.3 on average PC with 16 GB RAM)
> local a = ("a"):rep(2^30); a = a..a; print(#a)
> local a = ("a"):rep(2^31); print(#a)
stdin:1: resulting string too large
stack traceback:
        [C]: in function 'string.rep'
        stdin:1: in main chunk
        [C]: in ?