diff options
author | Bartosz Golaszewski <bartekgola@gmail.com> | 2015-08-25 16:36:43 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-08-25 16:36:43 +0200 |
commit | 7448b513c84feb3fd06fc57b39f5ab450970c01e (patch) | |
tree | de8a5238b249f7c452d424b437357721971ece15 /libbb | |
parent | 5b865deb3f2eafa1dcefabedf4af17a4c72f6cb8 (diff) | |
download | busybox-w32-7448b513c84feb3fd06fc57b39f5ab450970c01e.tar.gz busybox-w32-7448b513c84feb3fd06fc57b39f5ab450970c01e.tar.bz2 busybox-w32-7448b513c84feb3fd06fc57b39f5ab450970c01e.zip |
libbb: add is_suffixed_with() function
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/compare_string_array.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index cdcb2718d..e0d8e421b 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c | |||
@@ -28,6 +28,25 @@ char* FAST_FUNC is_prefixed_with(const char *string, const char *key) | |||
28 | #endif | 28 | #endif |
29 | } | 29 | } |
30 | 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 | if (strcmp(string + len_diff, key) == 0) { | ||
43 | return (char*)key; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | return NULL; | ||
48 | } | ||
49 | |||
31 | /* returns the array index of the string */ | 50 | /* returns the array index of the string */ |
32 | /* (index of first match is returned, or -1) */ | 51 | /* (index of first match is returned, or -1) */ |
33 | int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) | 52 | int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) |
@@ -133,4 +152,18 @@ BBUNIT_DEFINE_TEST(is_prefixed_with) | |||
133 | BBUNIT_ENDTEST; | 152 | BBUNIT_ENDTEST; |
134 | } | 153 | } |
135 | 154 | ||
155 | BBUNIT_DEFINE_TEST(is_suffixed_with) | ||
156 | { | ||
157 | BBUNIT_ASSERT_STREQ("bar", is_suffixed_with("foo bar", "bar")); | ||
158 | BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo")); | ||
159 | BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", "")); | ||
160 | BBUNIT_ASSERT_STREQ("", is_suffixed_with("", "")); | ||
161 | |||
162 | BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo")); | ||
163 | BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar")); | ||
164 | BBUNIT_ASSERT_NULL(is_suffixed_with("", "foo")); | ||
165 | |||
166 | BBUNIT_ENDTEST; | ||
167 | } | ||
168 | |||
136 | #endif /* ENABLE_UNIT_TEST */ | 169 | #endif /* ENABLE_UNIT_TEST */ |