Hi,
current implementation fails to be coherent with real strstr()
on the last test case:
./test
'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. '' : FAILED
this patch fixes this with no size increase:
./test
'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
./scripts/bloat-o-meter busybox_old busybox_unstripped
function old new delta
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/0 up/down: 0/0) Total: 0 bytes
Function is changed to:
char *strrstr(const char *haystack, const char *needle)
{
char *r = NULL;
do {
char *p = strstr(haystack, needle);
if (p)
r = p;
} while (*haystack++);
return r;
}
The contra is that it is probably a little more cpu intensive :)
Please apply if you like it.
Ciao,
Tito
--- libbb/strrstr_orig.c 2008-06-17 16:11:06.000000000 +0200
+++ libbb/strrstr.c 2008-06-17 21:33:51.000000000 +0200
@@ -13,19 +13,16 @@
* The strrstr() function finds the last occurrence of the substring needle
* in the string haystack. The terminating nul characters are not compared.
*/
-char* strrstr(const char *haystack, const char *needle)
+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;
+
+ do {
+ char *p = strstr(haystack, needle);
+ if (p)
r = p;
- haystack = p + 1;
- }
+ } while (*haystack++);
+ return r;
}
#ifdef __DO_STRRSTR_TEST
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox