BCM WL 6.30.102.9 (r366174)
[tomato.git] / release / src / router / busybox / libbb / compare_string_array.c
blob4b10cc138ab216556e12b93eecaa09a013e46f35
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 */
6 #include "libbb.h"
8 /* returns the array index of the string */
9 /* (index of first match is returned, or -1) */
10 int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
12 int i;
14 for (i = 0; string_array[i] != 0; i++) {
15 if (strcmp(string_array[i], key) == 0) {
16 return i;
19 return -1;
22 int FAST_FUNC index_in_strings(const char *strings, const char *key)
24 int idx = 0;
26 while (*strings) {
27 if (strcmp(strings, key) == 0) {
28 return idx;
30 strings += strlen(strings) + 1; /* skip NUL */
31 idx++;
33 return -1;
36 /* returns the array index of the string, even if it matches only a beginning */
37 /* (index of first match is returned, or -1) */
38 #ifdef UNUSED
39 int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
41 int i;
42 int len = strlen(key);
43 if (len) {
44 for (i = 0; string_array[i] != 0; i++) {
45 if (strncmp(string_array[i], key, len) == 0) {
46 return i;
50 return -1;
52 #endif
54 int FAST_FUNC index_in_substrings(const char *strings, const char *key)
56 int matched_idx = -1;
57 const int len = strlen(key);
59 if (len) {
60 int idx = 0;
61 while (*strings) {
62 if (strncmp(strings, key, len) == 0) {
63 if (strings[len] == '\0')
64 return idx; /* exact match */
65 if (matched_idx >= 0)
66 return -1; /* ambiguous match */
67 matched_idx = idx;
69 strings += strlen(strings) + 1; /* skip NUL */
70 idx++;
73 return matched_idx;
76 const char* FAST_FUNC nth_string(const char *strings, int n)
78 while (n) {
79 n--;
80 strings += strlen(strings) + 1;
82 return strings;
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)
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;
95 #endif