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

Aleksandar Ružičić ruzicic.aleksandar at gmail.com
Mon Apr 4 07:01:05 PDT 2011


On Mon, Apr 4, 2011 at 3:18 PM, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> On Sun, 03 Apr 2011 14:24:33 -0400, spir <denis.spir at gmail.com> wrote:
>> 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.
>

This works:
-----------------------------------------------------------
sizediff_t
indexOf(Char, T = sizediff_t)(in Char[] s, dchar c, T sp = 0)
if (isSomeChar!Char)
{
        if (sp < 0)
		sp += s.length;

	if (sp >= 0 && sp < s.length)
        {
		auto i = indexOf(s[sp..$], c);
		if (i > -1)
			return i + sp;
	}
	return -1;
}
-----------------------------------------------------------

And it doesn't require change of existing indexOf.


More information about the Digitalmars-d mailing list