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