On Mon, Jun 16, 2008 at 08:45:18AM +0200, Tito wrote:
>On Monday 16 June 2008 06:33:35 you wrote:
>There was one more reason to avoid strlen, but it was just my personal stupid
>paranoia:
>
>as we don't know the lenght of the strings it was difficult to decide
>the correct type for the result to avoid possible overflows
>OTOH using the biggest looked like overkill,
>so I decided to walk to the end of the string instead.
>
>> Mine version is:
>>
>> char* strrstr(const char *haystack, const char *needle)
>> {
>> char *r = NULL;
>>
>> if (!needle[0])
>> return r;
>> while (1) {
>> char *p = strstr(haystack, needle);
>> if (!p)
>> return r;
>> r = p;
>> haystack = p + 1;
>> }
>> }
>>
>> # ./a.out
>> 'baaabaaab' vs. 'aaa' : PASSED
>> 'baaabaaaab' vs. 'aaa' : PASSED
>> 'baaabaab' vs. 'aaa' : PASSED
>> 'aaa' vs. 'aaa' : PASSED
>> 'aaa' vs. 'a' : PASSED
>> 'aaa' vs. 'bbb' : PASSED
>> 'a' vs. 'aaa' : PASSED
>> 'aaa' vs. '' : PASSED
>> '' vs. 'aaa' : PASSED
>> '' vs. '' : PASSED
>> (cases with NULLs are not tested)
>>
>> Comparing with current busybox:
>>
>> function old new delta
>> strrstr 53 42 -11
Sounds good. Please apply.
>./test2
>strstr(aaa,"") returns not-NULL
>aaa
>strstr("","") returns not-NULL
For practical reasons i would return NULL if one of them is NULL or ""
to avoid checks in the callers.
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox