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 | */ |
| 9 | |
Bernhard Reutner-Fischer | 619b87d | 2008-06-17 12:45:39 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 11 | |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 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 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 16 | char* FAST_FUNC strrstr(const char *haystack, const char *needle) |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 17 | { |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 18 | char *r = NULL; |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 19 | |
| 20 | if (!needle[0]) |
Denis Vlasenko | 1363f0d | 2008-06-18 20:01:12 +0000 | [diff] [blame] | 21 | return (char*)haystack + strlen(haystack); |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 22 | while (1) { |
Bernhard Reutner-Fischer | 4954d47 | 2008-06-18 08:32:25 +0000 | [diff] [blame] | 23 | char *p = strstr(haystack, needle); |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 24 | if (!p) |
| 25 | return r; |
| 26 | r = p; |
| 27 | haystack = p + 1; |
| 28 | } |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Bartosz Golaszewski | 3ed81cf | 2014-06-22 16:30:41 +0200 | [diff] [blame] | 31 | #if ENABLE_UNIT_TEST |
| 32 | |
| 33 | BBUNIT_DEFINE_TEST(strrstr) |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 34 | { |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 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 | }; |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 52 | int i; |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 53 | |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 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); |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 57 | if (r == NULL) |
| 58 | r = test_array[i].h - 1; |
Bartosz Golaszewski | 3ed81cf | 2014-06-22 16:30:41 +0200 | [diff] [blame] | 59 | BBUNIT_ASSERT_EQ(r, test_array[i].h + test_array[i].pos); |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 60 | i++; |
| 61 | } |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 62 | |
Bartosz Golaszewski | 3ed81cf | 2014-06-22 16:30:41 +0200 | [diff] [blame] | 63 | BBUNIT_ENDTEST; |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 64 | } |
Bartosz Golaszewski | 3ed81cf | 2014-06-22 16:30:41 +0200 | [diff] [blame] | 65 | |
| 66 | #endif /* ENABLE_UNIT_TEST */ |