1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 * Return NULL if string is not prefixed with key. Return pointer to the
9 * first character in string after the prefix key. If key is an empty string,
10 * return pointer to the beginning of string.
12 char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
14 #if 0 /* Two passes over key - probably slower */
15 int len = strlen(key);
16 if (strncmp(string, key, len) == 0)
19 #else /* Open-coded */
20 while (*key != '\0') {
31 * Return NULL if string is not suffixed with key. Return pointer to the
32 * beginning of prefix key in string. If key is an empty string return pointer
33 * to the end of string.
35 char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
37 size_t key_len = strlen(key);
38 ssize_t len_diff = strlen(string) - key_len;
42 if (strcmp(string, key) == 0) {
50 /* returns the array index of the string */
51 /* (index of first match is returned, or -1) */
52 int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
56 for (i = 0; string_array[i] != 0; i++) {
57 if (strcmp(string_array[i], key) == 0) {
64 int FAST_FUNC index_in_strings(const char *strings, const char *key)
69 if (strcmp(strings, key) == 0) {
72 strings += strlen(strings) + 1; /* skip NUL */
78 /* returns the array index of the string, even if it matches only a beginning */
79 /* (index of first match is returned, or -1) */
81 int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
85 for (i = 0; string_array[i] != 0; i++) {
86 if (is_prefixed_with(string_array[i], key)) {
95 int FAST_FUNC index_in_substrings(const char *strings, const char *key)
98 const int len = strlen(key);
103 if (strncmp(strings, key, len) == 0) {
104 if (strings[len] == '\0')
105 return idx; /* exact match */
106 if (matched_idx >= 0)
107 return -1; /* ambiguous match */
110 strings += strlen(strings) + 1; /* skip NUL */
117 const char* FAST_FUNC nth_string(const char *strings, int n)
121 strings += strlen(strings) + 1;
126 #ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */
127 /* Returns 0 for no, 1 for yes or a negative value on error. */
128 smallint FAST_FUNC yesno(const char *str)
130 static const char no_yes[] ALIGN1 =
132 "1\0" "on\0" "yes\0";
133 int ret = index_in_substrings(no_yes, str);
140 BBUNIT_DEFINE_TEST(is_prefixed_with)
142 BBUNIT_ASSERT_STREQ(" bar", is_prefixed_with("foo bar", "foo"));
143 BBUNIT_ASSERT_STREQ("bar", is_prefixed_with("foo bar", "foo "));
144 BBUNIT_ASSERT_STREQ("", is_prefixed_with("foo", "foo"));
145 BBUNIT_ASSERT_STREQ("foo", is_prefixed_with("foo", ""));
146 BBUNIT_ASSERT_STREQ("", is_prefixed_with("", ""));
148 BBUNIT_ASSERT_NULL(is_prefixed_with("foo", "bar foo"));
149 BBUNIT_ASSERT_NULL(is_prefixed_with("foo foo", "bar"));
150 BBUNIT_ASSERT_NULL(is_prefixed_with("", "foo"));
155 BBUNIT_DEFINE_TEST(is_suffixed_with)
157 BBUNIT_ASSERT_STREQ("bar", is_suffixed_with("foo bar", "bar"));
158 BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo"));
159 BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", ""));
160 BBUNIT_ASSERT_STREQ("", is_suffixed_with("", ""));
161 BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo"));
163 BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo"));
164 BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar"));
165 BBUNIT_ASSERT_NULL(is_suffixed_with("", "foo"));
170 #endif /* ENABLE_UNIT_TEST */