lua-users home
lua-l archive

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


2016-09-29 20:26 GMT+02:00 Egor Skriptunoff <egor.skriptunoff@gmail.com>:

> This solution is based on strange behavior of string.sub.

Not every one will agree that the behaviour is strange. For example,
I disagree. The behaviour is clearly documented.

> IMO, "string.sub(index_from, index_to)" will be more handy if
> "index_from = 0'
> would mean "the index after the last character"
> instead of "the index before the first character".
> ("index_to = 0" should mean "index before the first character", as it is
> currently implemented in Lua)
>
> The unexpected result of "str:sub(0)" makes programming a bit harder,
> as this "feature" is actually a trap, and you are compelled
> to use additional "if" to make things right.
> But surprisingly, this weird logic can be beneficial in codegolf :-)

If 1 is the index at the first character, it is fully consistent
that 0 is the index before the first character. Any other
behaviour would be strange, unexpected, surprising and
weird.

My conclusion is rather that codegolf underlines the unremitting
consistency in the design of the Lua string library :-)