blob: 93d970a1b10bfd1bee59ae79ade3c1b46fdf77c5 [file] [log] [blame]
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00005 * Copyright (C) 2008 Bernhard Reutner-Fischer
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +00008 */
9
Bernhard Reutner-Fischer619b87d2008-06-17 12:45:39 +000010#include "libbb.h"
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +000011
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000012/*
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 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000016char* FAST_FUNC strrstr(const char *haystack, const char *needle)
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +000017{
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000018 char *r = NULL;
Denis Vlasenkod5736c52008-06-18 19:49:46 +000019
20 if (!needle[0])
Denis Vlasenko1363f0d2008-06-18 20:01:12 +000021 return (char*)haystack + strlen(haystack);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000022 while (1) {
Bernhard Reutner-Fischer4954d472008-06-18 08:32:25 +000023 char *p = strstr(haystack, needle);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000024 if (!p)
25 return r;
26 r = p;
27 haystack = p + 1;
28 }
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +000029}
30
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020031#if ENABLE_UNIT_TEST
32
33BBUNIT_DEFINE_TEST(strrstr)
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000034{
Denis Vlasenkod5736c52008-06-18 19:49:46 +000035 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 };
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000051
Denis Vlasenkod5736c52008-06-18 19:49:46 +000052 int i;
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000053
Denis Vlasenkod5736c52008-06-18 19:49:46 +000054 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);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000057 if (r == NULL)
58 r = test_array[i].h - 1;
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020059 BBUNIT_ASSERT_EQ(r, test_array[i].h + test_array[i].pos);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000060 i++;
61 }
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000062
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020063 BBUNIT_ENDTEST;
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000064}
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020065
66#endif /* ENABLE_UNIT_TEST */