Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 8 | */ |
Bernhard Reutner-Fischer | 619b87d | 2008-06-17 12:45:39 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 10 | |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 11 | /* |
| 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 Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 15 | char* FAST_FUNC strrstr(const char *haystack, const char *needle) |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 16 | { |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 17 | char *r = NULL; |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 18 | |
| 19 | if (!needle[0]) |
Denis Vlasenko | 1363f0d | 2008-06-18 20:01:12 +0000 | [diff] [blame] | 20 | return (char*)haystack + strlen(haystack); |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 21 | while (1) { |
Bernhard Reutner-Fischer | 4954d47 | 2008-06-18 08:32:25 +0000 | [diff] [blame] | 22 | char *p = strstr(haystack, needle); |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 23 | if (!p) |
| 24 | return r; |
| 25 | r = p; |
| 26 | haystack = p + 1; |
| 27 | } |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Bartosz Golaszewski | 3ed81cf | 2014-06-22 16:30:41 +0200 | [diff] [blame] | 30 | #if ENABLE_UNIT_TEST |
| 31 | |
| 32 | BBUNIT_DEFINE_TEST(strrstr) |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 33 | { |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 34 | 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-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 51 | int i; |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 53 | 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 Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 56 | if (r == NULL) |
| 57 | r = test_array[i].h - 1; |
Bartosz Golaszewski | 3ed81cf | 2014-06-22 16:30:41 +0200 | [diff] [blame] | 58 | BBUNIT_ASSERT_EQ(r, test_array[i].h + test_array[i].pos); |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 59 | i++; |
| 60 | } |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 61 | |
Bartosz Golaszewski | 3ed81cf | 2014-06-22 16:30:41 +0200 | [diff] [blame] | 62 | BBUNIT_ENDTEST; |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 63 | } |
Bartosz Golaszewski | 3ed81cf | 2014-06-22 16:30:41 +0200 | [diff] [blame] | 64 | |
| 65 | #endif /* ENABLE_UNIT_TEST */ |