source: MondoRescue/branches/3.3/mindi-busybox/libbb/strrstr.c@ 3647

Last change on this file since 3647 was 3621, checked in by Bruno Cornec, 10 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

  • Property svn:eol-style set to native
File size: 1.3 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#include "libbb.h"
11
12/*
13 * The strrstr() function finds the last occurrence of the substring needle
14 * in the string haystack. The terminating nul characters are not compared.
15 */
16char* FAST_FUNC strrstr(const char *haystack, const char *needle)
17{
18 char *r = NULL;
19
20 if (!needle[0])
21 return (char*)haystack + strlen(haystack);
22 while (1) {
23 char *p = strstr(haystack, needle);
24 if (!p)
25 return r;
26 r = p;
27 haystack = p + 1;
28 }
29}
30
31#if ENABLE_UNIT_TEST
32
33BBUNIT_DEFINE_TEST(strrstr)
34{
35 static const struct {
36 const char *h, *n;
37 int pos;
38 } test_array[] = {
39 /* 0123456789 */
40 { "baaabaaab", "aaa", 5 },
41 { "baaabaaaab", "aaa", 6 },
42 { "baaabaab", "aaa", 1 },
43 { "aaa", "aaa", 0 },
44 { "aaa", "a", 2 },
45 { "aaa", "bbb", -1 },
46 { "a", "aaa", -1 },
47 { "aaa", "", 3 },
48 { "", "aaa", -1 },
49 { "", "", 0 },
50 };
51
52 int i;
53
54 i = 0;
55 while (i < sizeof(test_array) / sizeof(test_array[0])) {
56 const char *r = strrstr(test_array[i].h, test_array[i].n);
57 if (r == NULL)
58 r = test_array[i].h - 1;
59 BBUNIT_ASSERT_EQ(r, test_array[i].h + test_array[i].pos);
60 i++;
61 }
62
63 BBUNIT_ENDTEST;
64}
65
66#endif /* ENABLE_UNIT_TEST */
Note: See TracBrowser for help on using the repository browser.