[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 2 | /*
|
---|
[2725] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 4 | */
|
---|
| 5 |
|
---|
| 6 | #include "libbb.h"
|
---|
| 7 |
|
---|
[3621] | 8 | /*
|
---|
| 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 | */
|
---|
| 13 | char* 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 |
|
---|
| 31 | /*
|
---|
| 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 | */
|
---|
| 36 | char* 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) {
|
---|
| 42 | string += len_diff;
|
---|
| 43 | if (strcmp(string, key) == 0) {
|
---|
| 44 | return (char*)string;
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | return NULL;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[1765] | 51 | /* returns the array index of the string */
|
---|
| 52 | /* (index of first match is returned, or -1) */
|
---|
[2725] | 53 | int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
|
---|
[821] | 54 | {
|
---|
| 55 | int i;
|
---|
| 56 |
|
---|
| 57 | for (i = 0; string_array[i] != 0; i++) {
|
---|
| 58 | if (strcmp(string_array[i], key) == 0) {
|
---|
| 59 | return i;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
[1765] | 62 | return -1;
|
---|
[821] | 63 | }
|
---|
| 64 |
|
---|
[2725] | 65 | int FAST_FUNC index_in_strings(const char *strings, const char *key)
|
---|
[1765] | 66 | {
|
---|
| 67 | int idx = 0;
|
---|
| 68 |
|
---|
[2725] | 69 | while (*strings) {
|
---|
[1765] | 70 | if (strcmp(strings, key) == 0) {
|
---|
| 71 | return idx;
|
---|
| 72 | }
|
---|
| 73 | strings += strlen(strings) + 1; /* skip NUL */
|
---|
| 74 | idx++;
|
---|
| 75 | }
|
---|
| 76 | return -1;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /* returns the array index of the string, even if it matches only a beginning */
|
---|
| 80 | /* (index of first match is returned, or -1) */
|
---|
| 81 | #ifdef UNUSED
|
---|
[2725] | 82 | int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
|
---|
[1765] | 83 | {
|
---|
| 84 | int i;
|
---|
[3621] | 85 | if (key[0]) {
|
---|
[1765] | 86 | for (i = 0; string_array[i] != 0; i++) {
|
---|
[3621] | 87 | if (is_prefixed_with(string_array[i], key)) {
|
---|
[1765] | 88 | return i;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | return -1;
|
---|
| 93 | }
|
---|
| 94 | #endif
|
---|
| 95 |
|
---|
[2725] | 96 | int FAST_FUNC index_in_substrings(const char *strings, const char *key)
|
---|
[1765] | 97 | {
|
---|
[2725] | 98 | int matched_idx = -1;
|
---|
| 99 | const int len = strlen(key);
|
---|
[1765] | 100 |
|
---|
| 101 | if (len) {
|
---|
| 102 | int idx = 0;
|
---|
[2725] | 103 | while (*strings) {
|
---|
[1765] | 104 | if (strncmp(strings, key, len) == 0) {
|
---|
[2725] | 105 | if (strings[len] == '\0')
|
---|
| 106 | return idx; /* exact match */
|
---|
| 107 | if (matched_idx >= 0)
|
---|
| 108 | return -1; /* ambiguous match */
|
---|
| 109 | matched_idx = idx;
|
---|
[1765] | 110 | }
|
---|
| 111 | strings += strlen(strings) + 1; /* skip NUL */
|
---|
| 112 | idx++;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
[2725] | 115 | return matched_idx;
|
---|
[1765] | 116 | }
|
---|
[2725] | 117 |
|
---|
| 118 | const char* FAST_FUNC nth_string(const char *strings, int n)
|
---|
| 119 | {
|
---|
| 120 | while (n) {
|
---|
| 121 | n--;
|
---|
| 122 | strings += strlen(strings) + 1;
|
---|
| 123 | }
|
---|
| 124 | return strings;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 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. */
|
---|
| 129 | smallint 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
|
---|
[3621] | 138 |
|
---|
| 139 | #if ENABLE_UNIT_TEST
|
---|
| 140 |
|
---|
| 141 | BBUNIT_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 |
|
---|
| 156 | BBUNIT_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("", ""));
|
---|
| 162 | BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo"));
|
---|
| 163 |
|
---|
| 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 |
|
---|
| 171 | #endif /* ENABLE_UNIT_TEST */
|
---|