diff options
Diffstat (limited to 'libbb/compare_string_array.c')
-rw-r--r-- | libbb/compare_string_array.c | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index 4b10cc138..e24815a03 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c | |||
@@ -5,6 +5,24 @@ | |||
5 | 5 | ||
6 | #include "libbb.h" | 6 | #include "libbb.h" |
7 | 7 | ||
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 | |||
8 | /* returns the array index of the string */ | 26 | /* returns the array index of the string */ |
9 | /* (index of first match is returned, or -1) */ | 27 | /* (index of first match is returned, or -1) */ |
10 | int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) | 28 | int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) |
@@ -39,10 +57,9 @@ int FAST_FUNC index_in_strings(const char *strings, const char *key) | |||
39 | int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key) | 57 | int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key) |
40 | { | 58 | { |
41 | int i; | 59 | int i; |
42 | int len = strlen(key); | 60 | if (key[0]) { |
43 | if (len) { | ||
44 | for (i = 0; string_array[i] != 0; i++) { | 61 | for (i = 0; string_array[i] != 0; i++) { |
45 | if (strncmp(string_array[i], key, len) == 0) { | 62 | if (is_prefixed_with(string_array[i], key)) { |
46 | return i; | 63 | return i; |
47 | } | 64 | } |
48 | } | 65 | } |