Last change
on this file since 3832 was 3320, checked in by Bruno Cornec, 10 years ago |
- Re-add (thanks git BTW) the 2.2.9 branch which had been destroyed in
the move to 3.0
|
-
Property svn:eol-style
set to
native
|
File size:
1.5 KB
|
Line | |
---|
1 | /* vi: set sw=4 ts=4: */
|
---|
2 | /*
|
---|
3 | * Utility routines.
|
---|
4 | *
|
---|
5 | * Copyright (C) 2008 Bernhard Reutner-Fischer
|
---|
6 | *
|
---|
7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
8 | */
|
---|
9 |
|
---|
10 | #ifdef __DO_STRRSTR_TEST
|
---|
11 | #include <stdlib.h>
|
---|
12 | #include <string.h>
|
---|
13 | #include <stdio.h>
|
---|
14 | #else
|
---|
15 | #include "libbb.h"
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * The strrstr() function finds the last occurrence of the substring needle
|
---|
20 | * in the string haystack. The terminating nul characters are not compared.
|
---|
21 | */
|
---|
22 | char* FAST_FUNC strrstr(const char *haystack, const char *needle)
|
---|
23 | {
|
---|
24 | char *r = NULL;
|
---|
25 |
|
---|
26 | if (!needle[0])
|
---|
27 | return (char*)haystack + strlen(haystack);
|
---|
28 | while (1) {
|
---|
29 | char *p = strstr(haystack, needle);
|
---|
30 | if (!p)
|
---|
31 | return r;
|
---|
32 | r = p;
|
---|
33 | haystack = p + 1;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | #ifdef __DO_STRRSTR_TEST
|
---|
38 | int main(int argc, char **argv)
|
---|
39 | {
|
---|
40 | static const struct {
|
---|
41 | const char *h, *n;
|
---|
42 | int pos;
|
---|
43 | } test_array[] = {
|
---|
44 | /* 0123456789 */
|
---|
45 | { "baaabaaab", "aaa", 5 },
|
---|
46 | { "baaabaaaab", "aaa", 6 },
|
---|
47 | { "baaabaab", "aaa", 1 },
|
---|
48 | { "aaa", "aaa", 0 },
|
---|
49 | { "aaa", "a", 2 },
|
---|
50 | { "aaa", "bbb", -1 },
|
---|
51 | { "a", "aaa", -1 },
|
---|
52 | { "aaa", "", 3 },
|
---|
53 | { "", "aaa", -1 },
|
---|
54 | { "", "", 0 },
|
---|
55 | };
|
---|
56 |
|
---|
57 | int i;
|
---|
58 |
|
---|
59 | i = 0;
|
---|
60 | while (i < sizeof(test_array) / sizeof(test_array[0])) {
|
---|
61 | const char *r = strrstr(test_array[i].h, test_array[i].n);
|
---|
62 | printf("'%s' vs. '%s': '%s' - ", test_array[i].h, test_array[i].n, r);
|
---|
63 | if (r == NULL)
|
---|
64 | r = test_array[i].h - 1;
|
---|
65 | printf("%s\n", r == test_array[i].h + test_array[i].pos ? "PASSED" : "FAILED");
|
---|
66 | i++;
|
---|
67 | }
|
---|
68 |
|
---|
69 | return 0;
|
---|
70 | }
|
---|
71 | #endif
|
---|
Note:
See
TracBrowser
for help on using the repository browser.