On Tuesday 17 June 2008 09:43:54 Bernhard Fischer wrote:
> On Mon, Jun 16, 2008 at 09:48:07PM +0200, Tito wrote:
> >On Monday 16 June 2008 09:47:31 Bernhard Fischer wrote:
> >> On Mon, Jun 16, 2008 at 08:45:18AM +0200, Tito wrote:
> >> >On Monday 16 June 2008 06:33:35 you wrote:
>
> >> >> 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;
>
> >Hi, just for fun,
> >one more variant that seems to pass the tests as modified (for "")
> >
> >char* strrstr(const char *haystack, const char *needle)
> >{
> > char *s = NULL;
> >
> > while (*haystack) {
> > s = (strstr(haystack++, needle)) ? : s;
> > }
> > return s;
> >}
> >
> >but size on my system is the same as Denys' version.... :(
> >
> >scripts/bloat-o-meter busybox_old busybox_unstripped
> >function old new delta
> >strrstr 53 42 -11
> >------------------------------------------------------------------------------
> >(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-11) Total: -11
> >bytes
>
> Tito, can you pick one (your decision) and send me the full file
> (including your nice testcases)?
> TIA and cheers,
>
HI,
Attached you can find a test.c file with both strrstr, mine and Denys', they
show little
differences on "" vs. "" test case.
The test cases are improved and checked against the real strstr for consistency.
As you and Denys are more experienced it is up yo you to decide what to do.
Ciao,
Tito
/* vi: set sw=4 ts=4: */
/*
* Utility routines.
*
* Copyright (C) 2008 Bernhard Fischer
*
* Licensed under GPLv2 or later, see file License in this tarball for details.
*/
#include <string.h>
#include <stdio.h>
#define STRRSTR_TEST
/*#define DENYS*/
/*
* The strrstr() function finds the last occurrence of th substring needle
* in the string haystack. The terminating nul characters are not compared.
*/
#ifdef DENYS
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;
}
}
#else
char* strrstr(const char *haystack, const char *needle)
{
char *s = NULL;
do {
s = (strstr(haystack, needle)) ? : s;
} while (*haystack++);
//printf("%s\n", (s) ? s : "NULL");
return s;
}
#endif
#ifdef STRRSTR_TEST
/* Test */
int main(int argc, char **argv)
{
int ret = 0;
int n;
char *tmp;
ret |= !(n = ((tmp = strrstr("baaabaaab", "aaa")) != NULL && strcmp(tmp, "aaab") == 0));
printf("'baaabaaab' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = ((tmp = strrstr("baaabaaaab", "aaa")) != NULL && strcmp(tmp, "aaab") == 0));
printf("'baaabaaaab' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = ((tmp = strrstr("baaabaab", "aaa")) != NULL && strcmp(tmp, "aaabaab") == 0));
printf("'baaabaab' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = (strrstr("aaa", "aaa") != NULL));
printf("'aaa' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = (strrstr("aaa", "a") != NULL));
printf("'aaa' vs. 'a' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = (strrstr("aaa", "bbb") == NULL));
printf("'aaa' vs. 'bbb' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = (strrstr("a", "aaa") == NULL));
printf("'a' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = ((tmp = strrstr("aaa", "")) != NULL && strcmp(tmp, "aaa") == 0));
printf("'aaa' vs. '' : %s\n", n ? "FAILED" : "PASSED");
ret |= !(n = (strrstr("", "aaa") == NULL));
printf("'' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = ((tmp = strrstr("", "")) != NULL && strcmp(tmp, "") == 0));
printf("'' vs. '' : %s\n", n ? "PASSED" : "FAILED");
/*ret |= !(n = (strrstr(NULL, NULL) == NULL));
printf("'NULL' vs. 'NULL' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = (strrstr("", NULL) == NULL));
printf("'' vs. 'NULL' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = (strrstr(NULL, "") == NULL));
printf("'NULL' vs. '' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = (strrstr("aaa", NULL) == NULL));
printf("'aaa' vs. 'NULL' : %s\n", n ? "PASSED" : "FAILED");
ret |= !(n = (strrstr(NULL, "aaa") == NULL));
printf("'NULL' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");*/
return ret;
}
#endif
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox