On 6/22/13 12:28 PM, Adam D. Ruppe wrote:
On Saturday, 22 June 2013 at 16:38:31 UTC, Andrei Alexandrescu wrote:
Huh, even the shortest impl I can think of is about the same length:
inout(char)* mystrstr(inout(char)* haystack, const(char*) needle) {
assert(haystack !is null);
if(needle is null)
return haystack;
while(*haystack) {
auto h = haystack;
const(char)* n = needle;
while(*n == *h && *n && *h) {
h++;
n++;
}
if(*n == 0)
return haystack;
haystack++;
}
return null;
}
I like this a lot better because it is more straightforward.
Still buggy. The empty string must be a prefix of any string including
the empty string.
I describe the canonical implementation of substring brute for string
search as far as I see it
http://www.serversidemagazine.com/news/10-questions-with-facebook-research-engineer-andrei-alexandrescu/.
Andrei