Array-only version of startsWith and skipOver
Per Nordlöw
per.nordlow at gmail.com
Fri Oct 25 06:59:07 UTC 2019
Why does
/** Array-overload for `startsWith` with no explicit predicate
predicate. */
bool startsWith(T)(scope const(T)[] haystack,
scope const(T)[] needle)
{
if (haystack.length >= needle.length)
{
return haystack[0 .. needle.length] == needle; // range
check is elided by LDC in release builds
}
return false;
}
/** Array-overload for `skipOver` with no explicit predicate
predicate. */
bool skipOver(T)(scope ref const(T)[] haystack,
scope const(T)[] needle)
{
if (startsWith(haystack, needle))
{
haystack = haystack[needle.length .. $];
return true;
}
return false;
}
///
@safe pure nothrow @nogc unittest
{
string x = "beta version";
assert(x.skipOver("beta"));
}
fail to compile as
array_algorithm.d(59,22): Error: template
`array_algorithm.skipOver` cannot deduce function from argument
types `!()(string, string)`, candidates are:
assert(x.skipOver("beta"));
^
array_algorithm.d(44,6): `skipOver(T)(ref scope const(T)[]
haystack, scope const(T)[] needle)`
bool skipOver(T)(scope ref const(T)[] haystack,
More information about the Digitalmars-d-learn
mailing list