indexOf's return val upon not found
Nick Sabalausky
a at a.a
Wed Jul 28 21:57:57 PDT 2010
Wouldn't it be better if std.string's indexOf/lastIndexOf returned
haystack.length instead of -1 to indicate "not found"? The -1 is never
really useful, but needs like this are fairly common:
auto slice = str[str.indexOf(needle)..$];
Which gives you an empty string if needle doesn't exist, which I find is
usually exactly what I want anyway (and consistent with find's semantics,
IIRC). But with the current semantics of indexOf/lastIndexOf, the risk of
the occasional -1 forces the clean code above to be turned into something
like this:
auto needleIndex = str.indexOf(needle);
auto slice = needleIndex==-1? "" : str[needleIndex..$];
Yuck.
More information about the Digitalmars-d
mailing list