blob: d8cd033a3dccaf5886d5531d04d525ea590a47a5 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrathb9638752002-12-02 00:01:36 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrathb9638752002-12-02 00:01:36 +00004 */
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00005#include "libbb.h"
Glenn L McGrathb9638752002-12-02 00:01:36 +00006
Bartosz Golaszewski0a4d0e82015-08-25 13:10:00 +02007/*
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.
11 */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010012char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
13{
14#if 0 /* Two passes over key - probably slower */
15 int len = strlen(key);
16 if (strncmp(string, key, len) == 0)
17 return string + len;
18 return NULL;
19#else /* Open-coded */
20 while (*key != '\0') {
21 if (*key != *string)
22 return NULL;
23 key++;
24 string++;
25 }
26 return (char*)string;
27#endif
28}
29
Bartosz Golaszewski7448b512015-08-25 16:36:43 +020030/*
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.
34 */
35char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
36{
37 size_t key_len = strlen(key);
38 ssize_t len_diff = strlen(string) - key_len;
39
40 if (len_diff >= 0) {
Denys Vlasenko68acc0f2015-08-25 21:47:33 +020041 string += len_diff;
42 if (strcmp(string, key) == 0) {
43 return (char*)string;
Bartosz Golaszewski7448b512015-08-25 16:36:43 +020044 }
45 }
46
47 return NULL;
48}
49
Denis Vlasenko5af906e2006-11-05 18:05:09 +000050/* returns the array index of the string */
51/* (index of first match is returned, or -1) */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000052int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
Glenn L McGrathb9638752002-12-02 00:01:36 +000053{
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000054 int i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000055
56 for (i = 0; string_array[i] != 0; i++) {
57 if (strcmp(string_array[i], key) == 0) {
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000058 return i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000059 }
60 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +000061 return -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000062}
63
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000064int FAST_FUNC index_in_strings(const char *strings, const char *key)
Denis Vlasenko990d0f62007-07-24 15:54:42 +000065{
Ron Yorstonbcf91d22021-01-29 13:23:27 +000066 int j, idx = 0;
Denis Vlasenko990d0f62007-07-24 15:54:42 +000067
Denis Vlasenkod12fcc22008-05-31 07:34:14 +000068 while (*strings) {
Ron Yorstonbcf91d22021-01-29 13:23:27 +000069 /* Do we see "key\0" at current position in strings? */
70 for (j = 0; *strings == key[j]; ++j) {
71 if (*strings++ == '\0') {
72 //bb_error_msg("found:'%s' i:%u", key, idx);
73 return idx; /* yes */
74 }
Denis Vlasenko990d0f62007-07-24 15:54:42 +000075 }
Ron Yorstonbcf91d22021-01-29 13:23:27 +000076 /* No. Move to the start of the next string. */
77 while (*strings++ != '\0')
78 continue;
Denis Vlasenko990d0f62007-07-24 15:54:42 +000079 idx++;
80 }
81 return -1;
82}
83
Denis Vlasenko5af906e2006-11-05 18:05:09 +000084/* returns the array index of the string, even if it matches only a beginning */
85/* (index of first match is returned, or -1) */
Denis Vlasenko990d0f62007-07-24 15:54:42 +000086#ifdef UNUSED
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000087int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
Denis Vlasenko5af906e2006-11-05 18:05:09 +000088{
89 int i;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010090 if (key[0]) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000091 for (i = 0; string_array[i] != 0; i++) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010092 if (is_prefixed_with(string_array[i], key)) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000093 return i;
94 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +000095 }
96 }
97 return -1;
98}
Denis Vlasenko990d0f62007-07-24 15:54:42 +000099#endif
100
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000101int FAST_FUNC index_in_substrings(const char *strings, const char *key)
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000102{
Denys Vlasenko8a659f62010-04-03 00:52:16 +0200103 int matched_idx = -1;
104 const int len = strlen(key);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000105
106 if (len) {
107 int idx = 0;
Denis Vlasenkod12fcc22008-05-31 07:34:14 +0000108 while (*strings) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000109 if (strncmp(strings, key, len) == 0) {
Denys Vlasenko8a659f62010-04-03 00:52:16 +0200110 if (strings[len] == '\0')
111 return idx; /* exact match */
112 if (matched_idx >= 0)
113 return -1; /* ambiguous match */
114 matched_idx = idx;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000115 }
116 strings += strlen(strings) + 1; /* skip NUL */
117 idx++;
118 }
119 }
Denys Vlasenko8a659f62010-04-03 00:52:16 +0200120 return matched_idx;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000121}
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000122
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000123const char* FAST_FUNC nth_string(const char *strings, int n)
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000124{
Denis Vlasenko6b404432008-01-07 16:13:14 +0000125 while (n) {
Martin Lewisc9fc1532020-06-11 15:45:58 -0500126 if (*strings++ == '\0') {
127 if (*strings == '\0') /* reached end of strings */
128 break;
129 n--;
130 }
Denis Vlasenko6b404432008-01-07 16:13:14 +0000131 }
132 return strings;
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000133}
Denys Vlasenko8a659f62010-04-03 00:52:16 +0200134
135#ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */
136/* Returns 0 for no, 1 for yes or a negative value on error. */
137smallint FAST_FUNC yesno(const char *str)
138{
139 static const char no_yes[] ALIGN1 =
140 "0\0" "off\0" "no\0"
141 "1\0" "on\0" "yes\0";
142 int ret = index_in_substrings(no_yes, str);
143 return ret / 3;
144}
145#endif
Bartosz Golaszewskib4329232015-08-25 13:09:59 +0200146
147#if ENABLE_UNIT_TEST
148
149BBUNIT_DEFINE_TEST(is_prefixed_with)
150{
151 BBUNIT_ASSERT_STREQ(" bar", is_prefixed_with("foo bar", "foo"));
152 BBUNIT_ASSERT_STREQ("bar", is_prefixed_with("foo bar", "foo "));
153 BBUNIT_ASSERT_STREQ("", is_prefixed_with("foo", "foo"));
154 BBUNIT_ASSERT_STREQ("foo", is_prefixed_with("foo", ""));
155 BBUNIT_ASSERT_STREQ("", is_prefixed_with("", ""));
156
157 BBUNIT_ASSERT_NULL(is_prefixed_with("foo", "bar foo"));
158 BBUNIT_ASSERT_NULL(is_prefixed_with("foo foo", "bar"));
159 BBUNIT_ASSERT_NULL(is_prefixed_with("", "foo"));
160
161 BBUNIT_ENDTEST;
162}
163
Bartosz Golaszewski7448b512015-08-25 16:36:43 +0200164BBUNIT_DEFINE_TEST(is_suffixed_with)
165{
166 BBUNIT_ASSERT_STREQ("bar", is_suffixed_with("foo bar", "bar"));
167 BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo"));
168 BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", ""));
169 BBUNIT_ASSERT_STREQ("", is_suffixed_with("", ""));
Tito Ragusaf0853442015-09-15 23:38:01 +0200170 BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo"));
Bartosz Golaszewski7448b512015-08-25 16:36:43 +0200171
172 BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo"));
173 BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar"));
174 BBUNIT_ASSERT_NULL(is_suffixed_with("", "foo"));
175
176 BBUNIT_ENDTEST;
177}
178
Bartosz Golaszewskib4329232015-08-25 13:09:59 +0200179#endif /* ENABLE_UNIT_TEST */