diff options
author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2008-06-17 12:11:34 +0000 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2008-06-17 12:11:34 +0000 |
commit | 13436ea0dd2b70271d30bd6f682cc9cbe41b26c6 (patch) | |
tree | 4c506973333c1eebd3fd15a077b76edc23fac593 | |
parent | 6eaf8deddd3c2cd747b09fb038f9e56f18250714 (diff) | |
download | busybox-w32-13436ea0dd2b70271d30bd6f682cc9cbe41b26c6.tar.gz busybox-w32-13436ea0dd2b70271d30bd6f682cc9cbe41b26c6.tar.bz2 busybox-w32-13436ea0dd2b70271d30bd6f682cc9cbe41b26c6.zip |
- improved strrstr impl from vda with testcases from Tito and vda
-rw-r--r-- | libbb/strrstr.c | 75 |
1 files changed, 69 insertions, 6 deletions
diff --git a/libbb/strrstr.c b/libbb/strrstr.c index b314264f5..3cd76670a 100644 --- a/libbb/strrstr.c +++ b/libbb/strrstr.c | |||
@@ -7,14 +7,77 @@ | |||
7 | * Licensed under GPLv2 or later, see file License in this tarball for details. | 7 | * Licensed under GPLv2 or later, see file License in this tarball for details. |
8 | */ | 8 | */ |
9 | 9 | ||
10 | #include "libbb.h" | 10 | #include <string.h> |
11 | #include <stdio.h> | ||
11 | 12 | ||
12 | /* reverse strstr() */ | 13 | /* |
14 | * The strrstr() function finds the last occurrence of the substring needle | ||
15 | * in the string haystack. The terminating nul characters are not compared. | ||
16 | */ | ||
13 | char* strrstr(const char *haystack, const char *needle) | 17 | char* strrstr(const char *haystack, const char *needle) |
14 | { | 18 | { |
15 | char *tmp = strrchr(haystack, *needle); | 19 | char *r = NULL; |
16 | if (tmp == NULL || strcmp(tmp, needle) != 0) | 20 | |
17 | return NULL; | 21 | if (!needle[0]) |
18 | return tmp; | 22 | return r; |
23 | while (1) { | ||
24 | char *p = strstr(haystack, needle); | ||
25 | if (!p) | ||
26 | return r; | ||
27 | r = p; | ||
28 | haystack = p + 1; | ||
29 | } | ||
19 | } | 30 | } |
20 | 31 | ||
32 | #ifdef __DO_STRRSTR_TEST | ||
33 | /* Test */ | ||
34 | int main(int argc, char **argv) | ||
35 | { | ||
36 | int ret = 0; | ||
37 | int n; | ||
38 | char *tmp; | ||
39 | |||
40 | ret |= !(n = ((tmp = strrstr("baaabaaab", "aaa")) != NULL && strcmp(tmp, "aaab") == 0)); | ||
41 | printf("'baaabaaab' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); | ||
42 | |||
43 | ret |= !(n = ((tmp = strrstr("baaabaaaab", "aaa")) != NULL && strcmp(tmp, "aaab") == 0)); | ||
44 | printf("'baaabaaaab' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); | ||
45 | |||
46 | ret |= !(n = ((tmp = strrstr("baaabaab", "aaa")) != NULL && strcmp(tmp, "aaabaab") == 0)); | ||
47 | printf("'baaabaab' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); | ||
48 | |||
49 | ret |= !(n = (strrstr("aaa", "aaa") != NULL)); | ||
50 | printf("'aaa' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); | ||
51 | |||
52 | ret |= !(n = (strrstr("aaa", "a") != NULL)); | ||
53 | printf("'aaa' vs. 'a' : %s\n", n ? "PASSED" : "FAILED"); | ||
54 | |||
55 | ret |= !(n = (strrstr("aaa", "bbb") == NULL)); | ||
56 | printf("'aaa' vs. 'bbb' : %s\n", n ? "PASSED" : "FAILED"); | ||
57 | |||
58 | ret |= !(n = (strrstr("a", "aaa") == NULL)); | ||
59 | printf("'a' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); | ||
60 | |||
61 | ret |= !(n = ((tmp = strrstr("aaa", "")) != NULL && strcmp(tmp, "aaa") == 0)); | ||
62 | printf("'aaa' vs. '' : %s\n", n ? "FAILED" : "PASSED"); | ||
63 | |||
64 | ret |= !(n = (strrstr("", "aaa") == NULL)); | ||
65 | printf("'' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); | ||
66 | |||
67 | ret |= !(n = ((tmp = strrstr("", "")) != NULL && strcmp(tmp, "") == 0)); | ||
68 | printf("'' vs. '' : %s\n", n ? "PASSED" : "FAILED"); | ||
69 | |||
70 | /*ret |= !(n = (strrstr(NULL, NULL) == NULL)); | ||
71 | printf("'NULL' vs. 'NULL' : %s\n", n ? "PASSED" : "FAILED"); | ||
72 | ret |= !(n = (strrstr("", NULL) == NULL)); | ||
73 | printf("'' vs. 'NULL' : %s\n", n ? "PASSED" : "FAILED"); | ||
74 | ret |= !(n = (strrstr(NULL, "") == NULL)); | ||
75 | printf("'NULL' vs. '' : %s\n", n ? "PASSED" : "FAILED"); | ||
76 | ret |= !(n = (strrstr("aaa", NULL) == NULL)); | ||
77 | printf("'aaa' vs. 'NULL' : %s\n", n ? "PASSED" : "FAILED"); | ||
78 | ret |= !(n = (strrstr(NULL, "aaa") == NULL)); | ||
79 | printf("'NULL' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");*/ | ||
80 | |||
81 | return ret; | ||
82 | } | ||
83 | #endif | ||