OT: CS education gone wrong (Was: Re: TDD is BS?)
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sun Jun 23 09:02:20 PDT 2013
On 6/21/13 4:02 PM, Walter Bright wrote:
> On 6/21/2013 3:35 PM, Andrei Alexandrescu wrote:
>> On 6/21/13 3:22 PM, Adam D. Ruppe wrote:
>>> Just for laughs I just slapped together a strstr
>>
>> Post it and I'll destroy it.
>
> Can I play, too? Mine from the Digital Mars C library. Haven't looked at
> it since 2001.
> ==================================================
>
> /*_ strstr.c */
> /* Copyright (C) 1985-2001 by Digital Mars */
> /* All Rights Reserved */
> /* www.digitalmars.com */
> /* Written by Walter Bright */
>
> #include <stdio.h>
> #include <ctype.h>
> #include <stddef.h>
> #include <string.h>
> #include <stdlib.h>
>
> #if 0 /* Smaller but slower under many circumstances. */
> char *strstr(const char *s1,const char *s2)
> { size_t len2;
> size_t len1;
> char c2 = *s2;
>
> len1 = strlen(s1);
> len2 = strlen(s2);
> if (!len2)
> return (char *) s1;
> while (len2 <= len1)
> {
> if (c2 == *s1)
> if (memcmp(s2,s1,len2) == 0)
> return (char *) s1;
> s1++;
> len1--;
> }
> return NULL;
> }
> #else
I won't comment on the B-M implementation. This brute force
implementation has the problem of calling strlen on both strings
upfront, which is arguably unnecessary. Although it doesn't affect
complexity, it is an inefficiency hard to recover from. (The tradeoff is
that memcmp will help with that.)
Andrei
More information about the Digitalmars-d
mailing list