blob: 450916c3a985e74ce205affeab798fc50031e9da [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
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01008char* 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
Denis Vlasenko5af906e2006-11-05 18:05:09 +000026/* returns the array index of the string */
27/* (index of first match is returned, or -1) */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000028int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
Glenn L McGrathb9638752002-12-02 00:01:36 +000029{
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000030 int i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000031
32 for (i = 0; string_array[i] != 0; i++) {
33 if (strcmp(string_array[i], key) == 0) {
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000034 return i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000035 }
36 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +000037 return -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000038}
39
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000040int FAST_FUNC index_in_strings(const char *strings, const char *key)
Denis Vlasenko990d0f62007-07-24 15:54:42 +000041{
42 int idx = 0;
43
Denis Vlasenkod12fcc22008-05-31 07:34:14 +000044 while (*strings) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +000045 if (strcmp(strings, key) == 0) {
46 return idx;
47 }
48 strings += strlen(strings) + 1; /* skip NUL */
49 idx++;
50 }
51 return -1;
52}
53
Denis Vlasenko5af906e2006-11-05 18:05:09 +000054/* returns the array index of the string, even if it matches only a beginning */
55/* (index of first match is returned, or -1) */
Denis Vlasenko990d0f62007-07-24 15:54:42 +000056#ifdef UNUSED
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000057int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
Denis Vlasenko5af906e2006-11-05 18:05:09 +000058{
59 int i;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010060 if (key[0]) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000061 for (i = 0; string_array[i] != 0; i++) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010062 if (is_prefixed_with(string_array[i], key)) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000063 return i;
64 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +000065 }
66 }
67 return -1;
68}
Denis Vlasenko990d0f62007-07-24 15:54:42 +000069#endif
70
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000071int FAST_FUNC index_in_substrings(const char *strings, const char *key)
Denis Vlasenko990d0f62007-07-24 15:54:42 +000072{
Denys Vlasenko8a659f62010-04-03 00:52:16 +020073 int matched_idx = -1;
74 const int len = strlen(key);
Denis Vlasenko990d0f62007-07-24 15:54:42 +000075
76 if (len) {
77 int idx = 0;
Denis Vlasenkod12fcc22008-05-31 07:34:14 +000078 while (*strings) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +000079 if (strncmp(strings, key, len) == 0) {
Denys Vlasenko8a659f62010-04-03 00:52:16 +020080 if (strings[len] == '\0')
81 return idx; /* exact match */
82 if (matched_idx >= 0)
83 return -1; /* ambiguous match */
84 matched_idx = idx;
Denis Vlasenko990d0f62007-07-24 15:54:42 +000085 }
86 strings += strlen(strings) + 1; /* skip NUL */
87 idx++;
88 }
89 }
Denys Vlasenko8a659f62010-04-03 00:52:16 +020090 return matched_idx;
Denis Vlasenko990d0f62007-07-24 15:54:42 +000091}
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000092
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000093const char* FAST_FUNC nth_string(const char *strings, int n)
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000094{
Denis Vlasenko6b404432008-01-07 16:13:14 +000095 while (n) {
96 n--;
97 strings += strlen(strings) + 1;
98 }
99 return strings;
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000100}
Denys Vlasenko8a659f62010-04-03 00:52:16 +0200101
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. */
104smallint 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
Bartosz Golaszewskib4329232015-08-25 13:09:59 +0200113
114#if ENABLE_UNIT_TEST
115
116BBUNIT_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 */