"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 | |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 8 | char* FAST_FUNC is_prefixed_with(const char *string, const char *key) |
| 9 | { |
| 10 | #if 0 /* Two passes over key - probably slower */ |
| 11 | int len = strlen(key); |
| 12 | if (strncmp(string, key, len) == 0) |
| 13 | return string + len; |
| 14 | return NULL; |
| 15 | #else /* Open-coded */ |
| 16 | while (*key != '\0') { |
| 17 | if (*key != *string) |
| 18 | return NULL; |
| 19 | key++; |
| 20 | string++; |
| 21 | } |
| 22 | return (char*)string; |
| 23 | #endif |
| 24 | } |
| 25 | |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 26 | /* returns the array index of the string */ |
| 27 | /* (index of first match is returned, or -1) */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 28 | 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] | 29 | { |
"Vladimir N. Oleynik" | cc34344 | 2005-11-26 10:45:26 +0000 | [diff] [blame] | 30 | int i; |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 31 | |
| 32 | for (i = 0; string_array[i] != 0; i++) { |
| 33 | if (strcmp(string_array[i], key) == 0) { |
"Vladimir N. Oleynik" | cc34344 | 2005-11-26 10:45:26 +0000 | [diff] [blame] | 34 | return i; |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 37 | return -1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 40 | int FAST_FUNC index_in_strings(const char *strings, const char *key) |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 41 | { |
| 42 | int idx = 0; |
| 43 | |
Denis Vlasenko | d12fcc2 | 2008-05-31 07:34:14 +0000 | [diff] [blame] | 44 | while (*strings) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 45 | if (strcmp(strings, key) == 0) { |
| 46 | return idx; |
| 47 | } |
| 48 | strings += strlen(strings) + 1; /* skip NUL */ |
| 49 | idx++; |
| 50 | } |
| 51 | return -1; |
| 52 | } |
| 53 | |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 54 | /* returns the array index of the string, even if it matches only a beginning */ |
| 55 | /* (index of first match is returned, or -1) */ |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 56 | #ifdef UNUSED |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 57 | 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] | 58 | { |
| 59 | int i; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 60 | if (key[0]) { |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 61 | for (i = 0; string_array[i] != 0; i++) { |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 62 | if (is_prefixed_with(string_array[i], key)) { |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 63 | return i; |
| 64 | } |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | return -1; |
| 68 | } |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 71 | int FAST_FUNC index_in_substrings(const char *strings, const char *key) |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 72 | { |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 73 | int matched_idx = -1; |
| 74 | const int len = strlen(key); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 75 | |
| 76 | if (len) { |
| 77 | int idx = 0; |
Denis Vlasenko | d12fcc2 | 2008-05-31 07:34:14 +0000 | [diff] [blame] | 78 | while (*strings) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 79 | if (strncmp(strings, key, len) == 0) { |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 80 | if (strings[len] == '\0') |
| 81 | return idx; /* exact match */ |
| 82 | if (matched_idx >= 0) |
| 83 | return -1; /* ambiguous match */ |
| 84 | matched_idx = idx; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 85 | } |
| 86 | strings += strlen(strings) + 1; /* skip NUL */ |
| 87 | idx++; |
| 88 | } |
| 89 | } |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 90 | return matched_idx; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 91 | } |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 93 | const char* FAST_FUNC nth_string(const char *strings, int n) |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 94 | { |
Denis Vlasenko | 6b40443 | 2008-01-07 16:13:14 +0000 | [diff] [blame] | 95 | while (n) { |
| 96 | n--; |
| 97 | strings += strlen(strings) + 1; |
| 98 | } |
| 99 | return strings; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 100 | } |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 101 | |
| 102 | #ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */ |
| 103 | /* Returns 0 for no, 1 for yes or a negative value on error. */ |
| 104 | smallint FAST_FUNC yesno(const char *str) |
| 105 | { |
| 106 | static const char no_yes[] ALIGN1 = |
| 107 | "0\0" "off\0" "no\0" |
| 108 | "1\0" "on\0" "yes\0"; |
| 109 | int ret = index_in_substrings(no_yes, str); |
| 110 | return ret / 3; |
| 111 | } |
| 112 | #endif |