aboutsummaryrefslogtreecommitdiff
path: root/libbb/compare_string_array.c
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2015-10-13 14:45:51 +0100
committerRon Yorston <rmy@pobox.com>2015-10-13 14:45:51 +0100
commit8e509f11bceeec419abc718300bef7422d1fee4c (patch)
treefdfbc752ad94102e3613a5d7254f14a93eaf7f56 /libbb/compare_string_array.c
parent420f5edfe7676fe6e7cddbbf15c04649d096e422 (diff)
parent4d0c1ea4784c9844f8468d97ca5c26d3c70f9921 (diff)
downloadbusybox-w32-8e509f11bceeec419abc718300bef7422d1fee4c.tar.gz
busybox-w32-8e509f11bceeec419abc718300bef7422d1fee4c.tar.bz2
busybox-w32-8e509f11bceeec419abc718300bef7422d1fee4c.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'libbb/compare_string_array.c')
-rw-r--r--libbb/compare_string_array.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c
index e24815a03..2f51237a3 100644
--- a/libbb/compare_string_array.c
+++ b/libbb/compare_string_array.c
@@ -5,6 +5,11 @@
5 5
6#include "libbb.h" 6#include "libbb.h"
7 7
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 */
8char* FAST_FUNC is_prefixed_with(const char *string, const char *key) 13char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
9{ 14{
10#if 0 /* Two passes over key - probably slower */ 15#if 0 /* Two passes over key - probably slower */
@@ -23,6 +28,26 @@ char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
23#endif 28#endif
24} 29}
25 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 */
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) {
42 string += len_diff;
43 if (strcmp(string, key) == 0) {
44 return (char*)string;
45 }
46 }
47
48 return NULL;
49}
50
26/* returns the array index of the string */ 51/* returns the array index of the string */
27/* (index of first match is returned, or -1) */ 52/* (index of first match is returned, or -1) */
28int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) 53int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
@@ -110,3 +135,37 @@ smallint FAST_FUNC yesno(const char *str)
110 return ret / 3; 135 return ret / 3;
111} 136}
112#endif 137#endif
138
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
156BBUNIT_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 */