aboutsummaryrefslogtreecommitdiff
path: root/libbb/compare_string_array.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/compare_string_array.c')
-rw-r--r--libbb/compare_string_array.c33
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 */
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 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) */
33int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) 52int 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
155BBUNIT_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 */