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

KennyTM~ kennytm at gmail.com
Sun Apr 3 12:06:11 PDT 2011


On Apr 4, 11 02:24, spir 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);
>
> I would be far more interested in generalised negative inices in D --à
> la Python. A great step for D's friendliness, and an final end to
> current '$' issues (1).
>
> Denis
>
> (1) Which works only for builtin type via compiler magic, because it
> neither maps to .length, nore is overloadable.

There should be opDollar http://d.puremagic.com/issues/show_bug.cgi?id=3474.


More information about the Digitalmars-d mailing list