std.string.indexOf with an optional start-at parameter?

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 4 07:40:28 PDT 2011


On Mon, 04 Apr 2011 10:10:00 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 4/4/11 8:18 AM, Steven Schveighoffer wrote:
>> On Sun, 03 Apr 2011 14:24:33 -0400, spir <denis.spir at gmail.com> wrote:
>>
>>> On 04/03/2011 07:39 PM, Aleksandar Ružičić wrote:
>>>> I needed std.string.indexOf to accept start position in the string to
>>>> start the search at. I was really surprised when I realized that this
>>>> (to me) standard parameter is "missing" (I'm used to indexOf in
>>>> javascript, strpos in php and equivalent methods in other languages,
>>>> which support start offset parameter).
>>>>
>>>> There might be some other function (in some other module) that does
>>>> what I want but I wasn't able to find it (I find D's documentation not
>>>> easy to search and read), so I've copied indexOf to my module and
>>>> added wanted functionality:
>>>>
>>>> https://gist.github.com/900589
>>>>
>>>> now, I'm able to write, for example:
>>>>
>>>> auto pos = indexOf(haystack, '$', 10); // will starts search at 11th
>>>> char in haystack
>>>>
>>>> and
>>>>
>>>> auto pos = indexOf(haystack, '$', -5); // will starts search at 5th
>>>> char from the end
>>>>
>>>> My question is: is there a reason why there is no this functionality
>>>> in phobos (maybe there's some language feature I'm not aware of?) and
>>>> if no such reason exists, would it be possible to add it in future
>>>> version of phobos/dmd?
>>>
>>> Agreed this is a fairly standard param in other languages, but D
>>> easily (and rather cheaply) allows
>>> auto pos = indexOf(s[i..$], char);
>>
>> That doesn't work, because it gets you the position in relation to
>> s[i..$], whereas you want the position in relation to s.
>>
>> I think the requested feature is common enough to warrant inclusion,
>> especially since it could take care of out-of-bounds problems where
>> slicing would throw an error instead. To write the equivalent would be
>> very non-trivial.
>>
>> -Steve
>
> I'm worried that most people will want and mean n in indexOf(haystack,  
> needle, n) as "start from the nth character from the front (or back)".  
> Then we need the slower algorithm. Using a slice clarifies the intent on  
> the caller's side.

I was about to write "what does that mean?", but I see what you mean now.

I look at the most important part of this as, when you search on a slice,  
the result is a number that has to be re-adjusted based on the slice.

That is, if I could get the right value by doing indexOf(s[5..$], "abc"),  
then it would be great to just accept slices, but that returns the result  
offset by the slice.

So you have to re-adjust the return value: indexOf(s[5..$], "abc") + 5.

This is a little annoying, and dangerous, especially if the offset (5 in  
this example) is not a simple literal.  There are very very easy ways to  
write this incorrectly.

What if we had a different function name to re-assert intent?   
indexOfSlice(haystack, needle, slicestart, sliceend) where sliceend was  
optional?

-Steve


More information about the Digitalmars-d mailing list