blob: 2f51237a33301fadb9523707c4a23ea4dbf44710 [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 */
5
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00006#include "libbb.h"
Glenn L McGrathb9638752002-12-02 00:01:36 +00007
Bartosz Golaszewski0a4d0e82015-08-25 13:10:00 +02008/*
9 * Return NULL if string is not prefixed with key. Return pointer to the
10 * first character in string after the prefix key. If key is an empty string,
11 * return pointer to the beginning of string.
12 */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010013char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
14{
15#if 0 /* Two passes over key - probably slower */
16 int len = strlen(key);
17 if (strncmp(string, key, len) == 0)
18 return string + len;
19 return NULL;
20#else /* Open-coded */
21 while (*key != '\0') {
22 if (*key != *string)
23 return NULL;
24 key++;
25 string++;
26 }
27 return (char*)string;
28#endif
29}
30
Bartosz Golaszewski7448b512015-08-25 16:36:43 +020031/*
32 * Return NULL if string is not suffixed with key. Return pointer to the
33 * beginning of prefix key in string. If key is an empty string return pointer
34 * to the end of string.
35 */
36char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
37{
38 size_t key_len = strlen(key);
39 ssize_t len_diff = strlen(string) - key_len;
40
41 if (len_diff >= 0) {
Denys Vlasenko68acc0f2015-08-25 21:47:33 +020042 string += len_diff;
43 if (strcmp(string, key) == 0) {
44 return (char*)string;
Bartosz Golaszewski7448b512015-08-25 16:36:43 +020045 }
46 }
47
48 return NULL;
49}
50
Denis Vlasenko5af906e2006-11-05 18:05:09 +000051/* returns the array index of the string */
52/* (index of first match is returned, or -1) */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000053int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
Glenn L McGrathb9638752002-12-02 00:01:36 +000054{
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000055 int i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000056
57 for (i = 0; string_array[i] != 0; i++) {
58 if (strcmp(string_array[i], key) == 0) {
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000059 return i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000060 }
61 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +000062 return -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000063}
64
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000065int FAST_FUNC index_in_strings(const char *strings, const char *key)
Denis Vlasenko990d0f62007-07-24 15:54:42 +000066{
67 int idx = 0;
68
Denis Vlasenkod12fcc22008-05-31 07:34:14 +000069 while (*strings) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +000070 if (strcmp(strings, key) == 0) {
71 return idx;
72 }
73 strings += strlen(strings) + 1; /* skip NUL */
74 idx++;
75 }
76 return -1;
77}
78
Denis Vlasenko5af906e2006-11-05 18:05:09 +000079/* returns the array index of the string, even if it matches only a beginning */
80/* (index of first match is returned, or -1) */
Denis Vlasenko990d0f62007-07-24 15:54:42 +000081#ifdef UNUSED
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000082int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
Denis Vlasenko5af906e2006-11-05 18:05:09 +000083{
84 int i;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010085 if (key[0]) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000086 for (i = 0; string_array[i] != 0; i++) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010087 if (is_prefixed_with(string_array[i], key)) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000088 return i;
89 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +000090 }
91 }
92 return -1;
93}
Denis Vlasenko990d0f62007-07-24 15:54:42 +000094#endif
95
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000096int FAST_FUNC index_in_substrings(const char *strings, const char *key)
Denis Vlasenko990d0f62007-07-24 15:54:42 +000097{
Denys Vlasenko8a659f62010-04-03 00:52:16 +020098 int matched_idx = -1;
99 const int len = strlen(key);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000100
101 if (len) {
102 int idx = 0;
Denis Vlasenkod12fcc22008-05-31 07:34:14 +0000103 while (*strings) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000104 if (strncmp(strings, key, len) == 0) {
Denys Vlasenko8a659f62010-04-03 00:52:16 +0200105 if (strings[len] == '\0')
106 return idx; /* exact match */
107 if (matched_idx >= 0)
108 return -1; /* ambiguous match */
109 matched_idx = idx;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000110 }
111 strings += strlen(strings) + 1; /* skip NUL */
112 idx++;
113 }
114 }
Denys Vlasenko8a659f62010-04-03 00:52:16 +0200115 return matched_idx;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000116}
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000117
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000118const char* FAST_FUNC nth_string(const char *strings, int n)
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000119{
Denis Vlasenko6b404432008-01-07 16:13:14 +0000120 while (n) {
121 n--;
122 strings += strlen(strings) + 1;
123 }
124 return strings;
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000125}
Denys Vlasenko8a659f62010-04-03 00:52:16 +0200126
127#ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */
128/* Returns 0 for no, 1 for yes or a negative value on error. */
129smallint FAST_FUNC yesno(const char *str)
130{
131 static const char no_yes[] ALIGN1 =
132 "0\0" "off\0" "no\0"
133 "1\0" "on\0" "yes\0";
134 int ret = index_in_substrings(no_yes, str);
135 return ret / 3;
136}
137#endif
Bartosz Golaszewskib4329232015-08-25 13:09:59 +0200138
139#if ENABLE_UNIT_TEST
140
141BBUNIT_DEFINE_TEST(is_prefixed_with)
142{
143 BBUNIT_ASSERT_STREQ(" bar", is_prefixed_with("foo bar", "foo"));
144 BBUNIT_ASSERT_STREQ("bar", is_prefixed_with("foo bar", "foo "));
145 BBUNIT_ASSERT_STREQ("", is_prefixed_with("foo", "foo"));
146 BBUNIT_ASSERT_STREQ("foo", is_prefixed_with("foo", ""));
147 BBUNIT_ASSERT_STREQ("", is_prefixed_with("", ""));
148
149 BBUNIT_ASSERT_NULL(is_prefixed_with("foo", "bar foo"));
150 BBUNIT_ASSERT_NULL(is_prefixed_with("foo foo", "bar"));
151 BBUNIT_ASSERT_NULL(is_prefixed_with("", "foo"));
152
153 BBUNIT_ENDTEST;
154}
155
Bartosz Golaszewski7448b512015-08-25 16:36:43 +0200156BBUNIT_DEFINE_TEST(is_suffixed_with)
157{
158 BBUNIT_ASSERT_STREQ("bar", is_suffixed_with("foo bar", "bar"));
159 BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo"));
160 BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", ""));
161 BBUNIT_ASSERT_STREQ("", is_suffixed_with("", ""));
Tito Ragusaf0853442015-09-15 23:38:01 +0200162 BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo"));
Bartosz Golaszewski7448b512015-08-25 16:36:43 +0200163
164 BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo"));
165 BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar"));
166 BBUNIT_ASSERT_NULL(is_suffixed_with("", "foo"));
167
168 BBUNIT_ENDTEST;
169}
170
Bartosz Golaszewskib4329232015-08-25 13:09:59 +0200171#endif /* ENABLE_UNIT_TEST */