lua-users home
lua-l archive

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


Hi all!

I found two corner cases for string.sub and string.byte and I'd like to know if their behaviour is something we can rely on or it is unspecified (the manual says nothing about it).


(1) -- string.sub(s,i1,i2) --

When both indexes are greater than string length the result is the empty string. When i2 is greater than the length, it is treated as the length.

e = ""
s = "abc"
assert( s:sub(5,5) == e )
assert( s:sub(5,10) == e )
assert( s:sub(1,10) == s )


(2) -- string.byte(s,i1,i2) --

When the string is empty, the returned value is `nil`. When the indexes are "out of range" (as the case (1)), the returned value is `nil`.

assert( e:byte() == nil )
assert( s:byte(5,5) == nil )
assert( s:byte(5,10) == nil )
assert( table.concat( {s:byte(1,10)}, " " ) == "65 66 67" )



The aforementioned behaviour is very intuitive and convenient, but the manual (5.1.4 and 5.2 beta alike) doesn't specify it. Is it unspecified behaviour? If not, I think the manual should report it.


Cheers.
-- Lorenzo