libbb: add unit tests for is_prefixed_with()
[oweals/busybox.git] / libbb / compare_string_array.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4  */
5
6 #include "libbb.h"
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
26 /* returns the array index of the string */
27 /* (index of first match is returned, or -1) */
28 int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
29 {
30         int i;
31
32         for (i = 0; string_array[i] != 0; i++) {
33                 if (strcmp(string_array[i], key) == 0) {
34                         return i;
35                 }
36         }
37         return -1;
38 }
39
40 int FAST_FUNC index_in_strings(const char *strings, const char *key)
41 {
42         int idx = 0;
43
44         while (*strings) {
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
54 /* returns the array index of the string, even if it matches only a beginning */
55 /* (index of first match is returned, or -1) */
56 #ifdef UNUSED
57 int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
58 {
59         int i;
60         if (key[0]) {
61                 for (i = 0; string_array[i] != 0; i++) {
62                         if (is_prefixed_with(string_array[i], key)) {
63                                 return i;
64                         }
65                 }
66         }
67         return -1;
68 }
69 #endif
70
71 int FAST_FUNC index_in_substrings(const char *strings, const char *key)
72 {
73         int matched_idx = -1;
74         const int len = strlen(key);
75
76         if (len) {
77                 int idx = 0;
78                 while (*strings) {
79                         if (strncmp(strings, key, len) == 0) {
80                                 if (strings[len] == '\0')
81                                         return idx; /* exact match */
82                                 if (matched_idx >= 0)
83                                         return -1; /* ambiguous match */
84                                 matched_idx = idx;
85                         }
86                         strings += strlen(strings) + 1; /* skip NUL */
87                         idx++;
88                 }
89         }
90         return matched_idx;
91 }
92
93 const char* FAST_FUNC nth_string(const char *strings, int n)
94 {
95         while (n) {
96                 n--;
97                 strings += strlen(strings) + 1;
98         }
99         return strings;
100 }
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
113
114 #if ENABLE_UNIT_TEST
115
116 BBUNIT_DEFINE_TEST(is_prefixed_with)
117 {
118         BBUNIT_ASSERT_STREQ(" bar", is_prefixed_with("foo bar", "foo"));
119         BBUNIT_ASSERT_STREQ("bar", is_prefixed_with("foo bar", "foo "));
120         BBUNIT_ASSERT_STREQ("", is_prefixed_with("foo", "foo"));
121         BBUNIT_ASSERT_STREQ("foo", is_prefixed_with("foo", ""));
122         BBUNIT_ASSERT_STREQ("", is_prefixed_with("", ""));
123
124         BBUNIT_ASSERT_NULL(is_prefixed_with("foo", "bar foo"));
125         BBUNIT_ASSERT_NULL(is_prefixed_with("foo foo", "bar"));
126         BBUNIT_ASSERT_NULL(is_prefixed_with("", "foo"));
127
128         BBUNIT_ENDTEST;
129 }
130
131 #endif /* ENABLE_UNIT_TEST */