aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-06-18 19:49:46 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-06-18 19:49:46 +0000
commitd5736c560703725d13c88234f03e82693212c265 (patch)
tree422a693a8f584c9512641032d6499082c8703606
parent9cd4c76b50b8fde9e21943bcd520168fa9d57290 (diff)
downloadbusybox-w32-d5736c560703725d13c88234f03e82693212c265.tar.gz
busybox-w32-d5736c560703725d13c88234f03e82693212c265.tar.bz2
busybox-w32-d5736c560703725d13c88234f03e82693212c265.zip
strrchr: bikeshed painting time!
replace cubic running time implementation with quadratic make embedded test actually readable function old new delta strrstr 42 44 +2
-rw-r--r--libbb/strrstr.c94
1 files changed, 43 insertions, 51 deletions
diff --git a/libbb/strrstr.c b/libbb/strrstr.c
index 126fed75c..088d9f457 100644
--- a/libbb/strrstr.c
+++ b/libbb/strrstr.c
@@ -7,73 +7,65 @@
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#ifdef __DO_STRRSTR_TEST
11#include <stdlib.h>
12#include <string.h>
13#include <stdio.h>
14#else
10#include "libbb.h" 15#include "libbb.h"
16#endif
11 17
12/* 18/*
13 * The strrstr() function finds the last occurrence of the substring needle 19 * The strrstr() function finds the last occurrence of the substring needle
14 * in the string haystack. The terminating nul characters are not compared. 20 * in the string haystack. The terminating nul characters are not compared.
15 */ 21 */
16char *strrstr(const char *haystack, const char *needle) 22char* strrstr(const char *haystack, const char *needle)
17{ 23{
18 char *r = NULL; 24 char *r = NULL;
19 25
20 do { 26 if (!needle[0])
27 return (char*)haystack;
28 while (1) {
21 char *p = strstr(haystack, needle); 29 char *p = strstr(haystack, needle);
22 if (p) 30 if (!p)
23 r = p; 31 return r;
24 } while (*haystack++); 32 r = p;
25 return r; 33 haystack = p + 1;
34 }
26} 35}
27 36
28#ifdef __DO_STRRSTR_TEST 37#ifdef __DO_STRRSTR_TEST
29/* Test */
30int main(int argc, char **argv) 38int main(int argc, char **argv)
31{ 39{
32 int ret = 0; 40 static const struct {
33 int n; 41 const char *h, *n;
34 char *tmp; 42 int pos;
35 43 } test_array[] = {
36 ret |= !(n = ((tmp = strrstr("baaabaaab", "aaa")) != NULL && strcmp(tmp, "aaab") == 0)); 44 /* 0123456789 */
37 printf("'baaabaaab' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); 45 { "baaabaaab", "aaa", 5 },
38 46 { "baaabaaaab", "aaa", 6 },
39 ret |= !(n = ((tmp = strrstr("baaabaaaab", "aaa")) != NULL && strcmp(tmp, "aaab") == 0)); 47 { "baaabaab", "aaa", 1 },
40 printf("'baaabaaaab' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); 48 { "aaa", "aaa", 0 },
41 49 { "aaa", "a", 2 },
42 ret |= !(n = ((tmp = strrstr("baaabaab", "aaa")) != NULL && strcmp(tmp, "aaabaab") == 0)); 50 { "aaa", "bbb", -1 },
43 printf("'baaabaab' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); 51 { "a", "aaa", -1 },
44 52 { "aaa", "", 3 },
45 ret |= !(n = (strrstr("aaa", "aaa") != NULL)); 53 { "", "aaa", -1 },
46 printf("'aaa' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED"); 54 { "", "", 0 },
47 55 };
48 ret |= !(n = (strrstr("aaa", "a") != NULL));
49 printf("'aaa' vs. 'a' : %s\n", n ? "PASSED" : "FAILED");
50
51 ret |= !(n = (strrstr("aaa", "bbb") == NULL));
52 printf("'aaa' vs. 'bbb' : %s\n", n ? "PASSED" : "FAILED");
53
54 ret |= !(n = (strrstr("a", "aaa") == NULL));
55 printf("'a' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");
56
57 ret |= !(n = ((tmp = strrstr("aaa", "")) != NULL && strcmp(tmp, "aaa") == 0));
58 printf("'aaa' vs. '' : %s\n", n ? "FAILED" : "PASSED");
59
60 ret |= !(n = (strrstr("", "aaa") == NULL));
61 printf("'' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");
62 56
63 ret |= !(n = ((tmp = strrstr("", "")) != NULL && strcmp(tmp, "") == 0)); 57 int i;
64 printf("'' vs. '' : %s\n", n ? "PASSED" : "FAILED");
65 58
66 /*ret |= !(n = (strrstr(NULL, NULL) == NULL)); 59 i = 0;
67 printf("'NULL' vs. 'NULL' : %s\n", n ? "PASSED" : "FAILED"); 60 while (i < sizeof(test_array) / sizeof(test_array[0])) {
68 ret |= !(n = (strrstr("", NULL) == NULL)); 61 const char *r = strrstr(test_array[i].h, test_array[i].n);
69 printf("'' vs. 'NULL' : %s\n", n ? "PASSED" : "FAILED"); 62 printf("'%s' vs. '%s': '%s' - ", test_array[i].h, test_array[i].n, r);
70 ret |= !(n = (strrstr(NULL, "") == NULL)); 63 if (r == NULL)
71 printf("'NULL' vs. '' : %s\n", n ? "PASSED" : "FAILED"); 64 r = test_array[i].h - 1;
72 ret |= !(n = (strrstr("aaa", NULL) == NULL)); 65 printf("%s\n", r == test_array[i].h + test_array[i].pos ? "PASSED" : "FAILED");
73 printf("'aaa' vs. 'NULL' : %s\n", n ? "PASSED" : "FAILED"); 66 i++;
74 ret |= !(n = (strrstr(NULL, "aaa") == NULL)); 67 }
75 printf("'NULL' vs. 'aaa' : %s\n", n ? "PASSED" : "FAILED");*/
76 68
77 return ret; 69 return 0;
78} 70}
79#endif 71#endif