lua-users home
lua-l archive

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


On Sun, Mar 2, 2014 at 12:41 PM, Coroutines <coroutines@gmail.com> wrote:
> On Sun, Mar 2, 2014 at 12:20 PM, Coroutines <coroutines@gmail.com> wrote:
>> On Sun, Mar 2, 2014 at 12:15 PM, Coroutines <coroutines@gmail.com> wrote:
>>> On Sun, Mar 2, 2014 at 12:15 PM, Coroutines <coroutines@gmail.com> wrote:
>>>> I think this is what I want: string.at = function (s1, i, s2) return i
>>>> == string.find(s1, '^' .. s2, i, true) end
>>>>
>>>> Again, sorry for all the confusion guys -- I really forgot that
>>>> string.find() is expected to search forward from the starting index --
>>>> not just *at* the starting index.
>>>>
>>>> On Sun, Mar 2, 2014 at 12:07 PM, Coroutines <coroutines@gmail.com> wrote:
>>>>> On Sun, Mar 2, 2014 at 11:45 AM, Thiago L. <fakedme@gmail.com> wrote:
>>>>>>
>>>>>> On 02/03/2014 16:40, Dirk Laurie wrote:
>>>>>>>
>>>>>>> 2014-03-02 21:36 GMT+02:00 Thiago L. <fakedme@gmail.com>:
>>>>>>>
>>>>>>>> Ok, you want a plain match, same rules apply: it doesn't care about the
>>>>>>>> index, it'll just try to find the data, for index -30 and string "cat"
>>>>>>>> -30
>>>>>>>> would be translated into -27 (because string length - 30) and then it
>>>>>>>> would
>>>>>>>> still match as "cat" is still on the string (which's 30 chars long with a
>>>>>>>> padding of 27 empty spaces on the left).
>>>>>>>
>>>>>>> This is not what happens. There is no padding to the left. The -27 is
>>>>>>> adjusted to be 1.
>>>>>>>
>>>>>> Yes, I know, but I think it should be changed/fixed...
>>>>>>
>>>>>
>>>>> Hmm.  I did do something screwy here.  I often write a function like
>>>>> this: string.at = function (s1, s2, i) return i == string.find(s1, s2,
>>>>> i) end
>>>>>
>>>>> I was misunderstanding the purpose of string.find(), I expected to
>>>>> match the needle at the index I give it.  I really don't know how I
>>>>> got 3-4 replies in without noticing my err. :\  I should have been
>>>>> using my at() function than find(), this entire thread is hooplah.  It
>>>>> makes sense that it would reposition the starting index at the
>>>>> beginning of the string if it expects to search forward through the
>>>>> string/haystack.  Sorry guys, I missed my coffee. :(
>>>
>>> Forgive me father for I have sinned... it has been 37 seconds since I
>>> last top-posted...
>>
>> I think maybe I'll just call it a day and go read a book or something
>> -- final form:
>>
>> string.at = function (s1, i, s2) return i == string.find(s1, '^' .. s2, i) end
>
> final final final form:  string.at = function (s1, i, s2) if #s1 - i >
> 0 then return nil end return i == string.find(s1, '^' .. s2, i) end

If I really wanted to be safe and not risk overflowing a double it
should be: string.at = function (s1, i, s2) if i < 0 - #s1 then return
nil end return i == string.find(s1, '^' .. s2, i) end