blob: a173b034f6dcb71dc89f09a913195edfd879bd0b [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 */
Bernhard Reutner-Fischer619b87d2008-06-17 12:45:39 +00009#include "libbb.h"
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +000010
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000011/*
12 * The strrstr() function finds the last occurrence of the substring needle
13 * in the string haystack. The terminating nul characters are not compared.
14 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000015char* FAST_FUNC strrstr(const char *haystack, const char *needle)
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +000016{
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000017 char *r = NULL;
Denis Vlasenkod5736c52008-06-18 19:49:46 +000018
19 if (!needle[0])
Denis Vlasenko1363f0d2008-06-18 20:01:12 +000020 return (char*)haystack + strlen(haystack);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000021 while (1) {
Bernhard Reutner-Fischer4954d472008-06-18 08:32:25 +000022 char *p = strstr(haystack, needle);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000023 if (!p)
24 return r;
25 r = p;
26 haystack = p + 1;
27 }
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +000028}
29
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020030#if ENABLE_UNIT_TEST
31
32BBUNIT_DEFINE_TEST(strrstr)
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000033{
Denis Vlasenkod5736c52008-06-18 19:49:46 +000034 static const struct {
35 const char *h, *n;
36 int pos;
37 } test_array[] = {
38 /* 0123456789 */
39 { "baaabaaab", "aaa", 5 },
40 { "baaabaaaab", "aaa", 6 },
41 { "baaabaab", "aaa", 1 },
42 { "aaa", "aaa", 0 },
43 { "aaa", "a", 2 },
44 { "aaa", "bbb", -1 },
45 { "a", "aaa", -1 },
46 { "aaa", "", 3 },
47 { "", "aaa", -1 },
48 { "", "", 0 },
49 };
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000050
Denis Vlasenkod5736c52008-06-18 19:49:46 +000051 int i;
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000052
Denis Vlasenkod5736c52008-06-18 19:49:46 +000053 i = 0;
54 while (i < sizeof(test_array) / sizeof(test_array[0])) {
55 const char *r = strrstr(test_array[i].h, test_array[i].n);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000056 if (r == NULL)
57 r = test_array[i].h - 1;
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020058 BBUNIT_ASSERT_EQ(r, test_array[i].h + test_array[i].pos);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000059 i++;
60 }
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000061
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020062 BBUNIT_ENDTEST;
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000063}
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020064
65#endif /* ENABLE_UNIT_TEST */