[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Nitpicking about string.sub argument validity check
- From: Sean Conner <sean@...>
- Date: Fri, 31 Aug 2018 19:06:07 -0400
It was thus said that the Great Egor Skriptunoff once stated:
> On Sat, Sep 1, 2018 at 1:09 AM, Albert Chan wrote:
>
> > > 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.
> >
> > I just felt uneasy using infinity as end-point ...
> > Especially bad if the infinity value is hidden behind a variable.
> >
> > What is gain by not using 1 for first location, -1 for last ?
> >
>
> local function get_suffix_of_length(str, len)
> return str:sub(-len)
> end
>
> get_suffix_of_length(s, math.huge) -- Easily understandable
> get_suffix_of_length(s, -1) -- What does "the suffix of length (-1)" mean?
If the code is expected to run under Lua 5.3, then why not
get_suffix_of_length(s,math.maxinteger) --?
And if you want to run it under previous versions:
get_suffix_of_length(s,math.maxinteger or -1)
which shows intent *and* is backwards compatible (yes, the -1 looks odd).
-spc