diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-03 00:52:16 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-03 00:52:16 +0200 |
commit | 8a659f6ff9a364fb48fbfa95d70d09134b579627 (patch) | |
tree | 91bf7ada57a359c75a9319534e9b23de0c55079b /libbb | |
parent | 05d1a32192011ef43248920db367653ccc4d980e (diff) | |
download | busybox-w32-8a659f6ff9a364fb48fbfa95d70d09134b579627.tar.gz busybox-w32-8a659f6ff9a364fb48fbfa95d70d09134b579627.tar.bz2 busybox-w32-8a659f6ff9a364fb48fbfa95d70d09134b579627.zip |
libbb: make index_in_substrings return -1 on ambiguous matches
function old new delta
index_in_substrings 67 93 +26
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-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 43c59e8e0..599449fe0 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c | |||
@@ -53,19 +53,24 @@ int FAST_FUNC index_in_substr_array(const char *const string_array[], const char | |||
53 | 53 | ||
54 | int FAST_FUNC index_in_substrings(const char *strings, const char *key) | 54 | int FAST_FUNC index_in_substrings(const char *strings, const char *key) |
55 | { | 55 | { |
56 | int len = strlen(key); | 56 | int matched_idx = -1; |
57 | const int len = strlen(key); | ||
57 | 58 | ||
58 | if (len) { | 59 | if (len) { |
59 | int idx = 0; | 60 | int idx = 0; |
60 | while (*strings) { | 61 | while (*strings) { |
61 | if (strncmp(strings, key, len) == 0) { | 62 | if (strncmp(strings, key, len) == 0) { |
62 | return idx; | 63 | if (strings[len] == '\0') |
64 | return idx; /* exact match */ | ||
65 | if (matched_idx >= 0) | ||
66 | return -1; /* ambiguous match */ | ||
67 | matched_idx = idx; | ||
63 | } | 68 | } |
64 | strings += strlen(strings) + 1; /* skip NUL */ | 69 | strings += strlen(strings) + 1; /* skip NUL */ |
65 | idx++; | 70 | idx++; |
66 | } | 71 | } |
67 | } | 72 | } |
68 | return -1; | 73 | return matched_idx; |
69 | } | 74 | } |
70 | 75 | ||
71 | const char* FAST_FUNC nth_string(const char *strings, int n) | 76 | const char* FAST_FUNC nth_string(const char *strings, int n) |
@@ -76,3 +81,15 @@ const char* FAST_FUNC nth_string(const char *strings, int n) | |||
76 | } | 81 | } |
77 | return strings; | 82 | return strings; |
78 | } | 83 | } |
84 | |||
85 | #ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */ | ||
86 | /* Returns 0 for no, 1 for yes or a negative value on error. */ | ||
87 | smallint FAST_FUNC yesno(const char *str) | ||
88 | { | ||
89 | static const char no_yes[] ALIGN1 = | ||
90 | "0\0" "off\0" "no\0" | ||
91 | "1\0" "on\0" "yes\0"; | ||
92 | int ret = index_in_substrings(no_yes, str); | ||
93 | return ret / 3; | ||
94 | } | ||
95 | #endif | ||