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 | * |
| 7 | * Licensed under GPLv2 or later, see file License in this tarball for details. |
| 8 | */ |
| 9 | |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 10 | #ifdef __DO_STRRSTR_TEST |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <stdio.h> |
| 14 | #else |
Bernhard Reutner-Fischer | 619b87d | 2008-06-17 12:45:39 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 16 | #endif |
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 | /* |
| 19 | * The strrstr() function finds the last occurrence of the substring needle |
| 20 | * in the string haystack. The terminating nul characters are not compared. |
| 21 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 22 | char* FAST_FUNC strrstr(const char *haystack, const char *needle) |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 23 | { |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 24 | char *r = NULL; |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 25 | |
| 26 | if (!needle[0]) |
Denis Vlasenko | 1363f0d | 2008-06-18 20:01:12 +0000 | [diff] [blame] | 27 | return (char*)haystack + strlen(haystack); |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 28 | while (1) { |
Bernhard Reutner-Fischer | 4954d47 | 2008-06-18 08:32:25 +0000 | [diff] [blame] | 29 | char *p = strstr(haystack, needle); |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 30 | if (!p) |
| 31 | return r; |
| 32 | r = p; |
| 33 | haystack = p + 1; |
| 34 | } |
Bernhard Reutner-Fischer | 1728229 | 2008-05-28 14:19:27 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 37 | #ifdef __DO_STRRSTR_TEST |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 38 | int main(int argc, char **argv) |
| 39 | { |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 40 | static const struct { |
| 41 | const char *h, *n; |
| 42 | int pos; |
| 43 | } test_array[] = { |
| 44 | /* 0123456789 */ |
| 45 | { "baaabaaab", "aaa", 5 }, |
| 46 | { "baaabaaaab", "aaa", 6 }, |
| 47 | { "baaabaab", "aaa", 1 }, |
| 48 | { "aaa", "aaa", 0 }, |
| 49 | { "aaa", "a", 2 }, |
| 50 | { "aaa", "bbb", -1 }, |
| 51 | { "a", "aaa", -1 }, |
| 52 | { "aaa", "", 3 }, |
| 53 | { "", "aaa", -1 }, |
| 54 | { "", "", 0 }, |
| 55 | }; |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 57 | int i; |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 59 | i = 0; |
| 60 | while (i < sizeof(test_array) / sizeof(test_array[0])) { |
| 61 | const char *r = strrstr(test_array[i].h, test_array[i].n); |
| 62 | printf("'%s' vs. '%s': '%s' - ", test_array[i].h, test_array[i].n, r); |
| 63 | if (r == NULL) |
| 64 | r = test_array[i].h - 1; |
| 65 | printf("%s\n", r == test_array[i].h + test_array[i].pos ? "PASSED" : "FAILED"); |
| 66 | i++; |
| 67 | } |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | d5736c5 | 2008-06-18 19:49:46 +0000 | [diff] [blame] | 69 | return 0; |
Bernhard Reutner-Fischer | 13436ea | 2008-06-17 12:11:34 +0000 | [diff] [blame] | 70 | } |
| 71 | #endif |