"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 6 | #include "libbb.h" |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 7 | |
Bartosz Golaszewski | 0a4d0e8 | 2015-08-25 13:10:00 +0200 | [diff] [blame] | 8 | /* |
| 9 | * Return NULL if string is not prefixed with key. Return pointer to the |
| 10 | * first character in string after the prefix key. If key is an empty string, |
| 11 | * return pointer to the beginning of string. |
| 12 | */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 13 | char* FAST_FUNC is_prefixed_with(const char *string, const char *key) |
| 14 | { |
| 15 | #if 0 /* Two passes over key - probably slower */ |
| 16 | int len = strlen(key); |
| 17 | if (strncmp(string, key, len) == 0) |
| 18 | return string + len; |
| 19 | return NULL; |
| 20 | #else /* Open-coded */ |
| 21 | while (*key != '\0') { |
| 22 | if (*key != *string) |
| 23 | return NULL; |
| 24 | key++; |
| 25 | string++; |
| 26 | } |
| 27 | return (char*)string; |
| 28 | #endif |
| 29 | } |
| 30 | |
Bartosz Golaszewski | 7448b51 | 2015-08-25 16:36:43 +0200 | [diff] [blame] | 31 | /* |
| 32 | * Return NULL if string is not suffixed with key. Return pointer to the |
| 33 | * beginning of prefix key in string. If key is an empty string return pointer |
| 34 | * to the end of string. |
| 35 | */ |
| 36 | char* FAST_FUNC is_suffixed_with(const char *string, const char *key) |
| 37 | { |
| 38 | size_t key_len = strlen(key); |
| 39 | ssize_t len_diff = strlen(string) - key_len; |
| 40 | |
| 41 | if (len_diff >= 0) { |
Denys Vlasenko | 68acc0f | 2015-08-25 21:47:33 +0200 | [diff] [blame] | 42 | string += len_diff; |
| 43 | if (strcmp(string, key) == 0) { |
| 44 | return (char*)string; |
Bartosz Golaszewski | 7448b51 | 2015-08-25 16:36:43 +0200 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | return NULL; |
| 49 | } |
| 50 | |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 51 | /* returns the array index of the string */ |
| 52 | /* (index of first match is returned, or -1) */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 53 | int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 54 | { |
"Vladimir N. Oleynik" | cc34344 | 2005-11-26 10:45:26 +0000 | [diff] [blame] | 55 | int i; |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 56 | |
| 57 | for (i = 0; string_array[i] != 0; i++) { |
| 58 | if (strcmp(string_array[i], key) == 0) { |
"Vladimir N. Oleynik" | cc34344 | 2005-11-26 10:45:26 +0000 | [diff] [blame] | 59 | return i; |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 62 | return -1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 65 | int FAST_FUNC index_in_strings(const char *strings, const char *key) |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 66 | { |
| 67 | int idx = 0; |
| 68 | |
Denis Vlasenko | d12fcc2 | 2008-05-31 07:34:14 +0000 | [diff] [blame] | 69 | while (*strings) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 70 | if (strcmp(strings, key) == 0) { |
| 71 | return idx; |
| 72 | } |
| 73 | strings += strlen(strings) + 1; /* skip NUL */ |
| 74 | idx++; |
| 75 | } |
| 76 | return -1; |
| 77 | } |
| 78 | |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 79 | /* returns the array index of the string, even if it matches only a beginning */ |
| 80 | /* (index of first match is returned, or -1) */ |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 81 | #ifdef UNUSED |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 82 | int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key) |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 83 | { |
| 84 | int i; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 85 | if (key[0]) { |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 86 | for (i = 0; string_array[i] != 0; i++) { |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 87 | if (is_prefixed_with(string_array[i], key)) { |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 88 | return i; |
| 89 | } |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | return -1; |
| 93 | } |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 94 | #endif |
| 95 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 96 | int FAST_FUNC index_in_substrings(const char *strings, const char *key) |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 97 | { |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 98 | int matched_idx = -1; |
| 99 | const int len = strlen(key); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 100 | |
| 101 | if (len) { |
| 102 | int idx = 0; |
Denis Vlasenko | d12fcc2 | 2008-05-31 07:34:14 +0000 | [diff] [blame] | 103 | while (*strings) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 104 | if (strncmp(strings, key, len) == 0) { |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 105 | if (strings[len] == '\0') |
| 106 | return idx; /* exact match */ |
| 107 | if (matched_idx >= 0) |
| 108 | return -1; /* ambiguous match */ |
| 109 | matched_idx = idx; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 110 | } |
| 111 | strings += strlen(strings) + 1; /* skip NUL */ |
| 112 | idx++; |
| 113 | } |
| 114 | } |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 115 | return matched_idx; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 116 | } |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 117 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 118 | const char* FAST_FUNC nth_string(const char *strings, int n) |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 119 | { |
Denis Vlasenko | 6b40443 | 2008-01-07 16:13:14 +0000 | [diff] [blame] | 120 | while (n) { |
| 121 | n--; |
| 122 | strings += strlen(strings) + 1; |
| 123 | } |
| 124 | return strings; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 125 | } |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 126 | |
| 127 | #ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */ |
| 128 | /* Returns 0 for no, 1 for yes or a negative value on error. */ |
| 129 | smallint FAST_FUNC yesno(const char *str) |
| 130 | { |
| 131 | static const char no_yes[] ALIGN1 = |
| 132 | "0\0" "off\0" "no\0" |
| 133 | "1\0" "on\0" "yes\0"; |
| 134 | int ret = index_in_substrings(no_yes, str); |
| 135 | return ret / 3; |
| 136 | } |
| 137 | #endif |
Bartosz Golaszewski | b432923 | 2015-08-25 13:09:59 +0200 | [diff] [blame] | 138 | |
| 139 | #if ENABLE_UNIT_TEST |
| 140 | |
| 141 | BBUNIT_DEFINE_TEST(is_prefixed_with) |
| 142 | { |
| 143 | BBUNIT_ASSERT_STREQ(" bar", is_prefixed_with("foo bar", "foo")); |
| 144 | BBUNIT_ASSERT_STREQ("bar", is_prefixed_with("foo bar", "foo ")); |
| 145 | BBUNIT_ASSERT_STREQ("", is_prefixed_with("foo", "foo")); |
| 146 | BBUNIT_ASSERT_STREQ("foo", is_prefixed_with("foo", "")); |
| 147 | BBUNIT_ASSERT_STREQ("", is_prefixed_with("", "")); |
| 148 | |
| 149 | BBUNIT_ASSERT_NULL(is_prefixed_with("foo", "bar foo")); |
| 150 | BBUNIT_ASSERT_NULL(is_prefixed_with("foo foo", "bar")); |
| 151 | BBUNIT_ASSERT_NULL(is_prefixed_with("", "foo")); |
| 152 | |
| 153 | BBUNIT_ENDTEST; |
| 154 | } |
| 155 | |
Bartosz Golaszewski | 7448b51 | 2015-08-25 16:36:43 +0200 | [diff] [blame] | 156 | BBUNIT_DEFINE_TEST(is_suffixed_with) |
| 157 | { |
| 158 | BBUNIT_ASSERT_STREQ("bar", is_suffixed_with("foo bar", "bar")); |
| 159 | BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo")); |
| 160 | BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", "")); |
| 161 | BBUNIT_ASSERT_STREQ("", is_suffixed_with("", "")); |
Tito Ragusa | f085344 | 2015-09-15 23:38:01 +0200 | [diff] [blame^] | 162 | BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo")); |
Bartosz Golaszewski | 7448b51 | 2015-08-25 16:36:43 +0200 | [diff] [blame] | 163 | |
| 164 | BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo")); |
| 165 | BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar")); |
| 166 | BBUNIT_ASSERT_NULL(is_suffixed_with("", "foo")); |
| 167 | |
| 168 | BBUNIT_ENDTEST; |
| 169 | } |
| 170 | |
Bartosz Golaszewski | b432923 | 2015-08-25 13:09:59 +0200 | [diff] [blame] | 171 | #endif /* ENABLE_UNIT_TEST */ |