[asterisk-commits] dlee: branch dlee/string-ops r381808 - in /team/dlee/string-ops: include/aste...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Feb 19 16:47:36 CST 2013
Author: dlee
Date: Tue Feb 19 16:47:33 2013
New Revision: 381808
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=381808
Log:
Adding ast_ends_with and ast_begins_with
Modified:
team/dlee/string-ops/include/asterisk/strings.h
team/dlee/string-ops/tests/test_strings.c
Modified: team/dlee/string-ops/include/asterisk/strings.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/string-ops/include/asterisk/strings.h?view=diff&rev=381808&r1=381807&r2=381808
==============================================================================
--- team/dlee/string-ops/include/asterisk/strings.h (original)
+++ team/dlee/string-ops/include/asterisk/strings.h Tue Feb 19 16:47:33 2013
@@ -82,6 +82,48 @@
*/
#define S_COR(a, b, c) ({typeof(&((b)[0])) __x = (b); (a) && !ast_strlen_zero(__x) ? (__x) : (c);})
+/*
+ \brief Checks whether a string begins with another.
+ \since 12.0.0
+ \param str String to check.
+ \param prefix Prefix to look for.
+ \param 1 if \a str begins with \a prefix, 0 otherwise.
+ */
+static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
+{
+ ast_assert(str != NULL);
+ ast_assert(prefix != NULL);
+ while (*str == *prefix && *prefix != '\0') {
+ ++str;
+ ++prefix;
+ }
+ return *prefix == '\0';
+}
+
+/*
+ \brief Checks whether a string ends with another.
+ \since 12.0.0
+ \param str String to check.
+ \param suffix Suffix to look for.
+ \param 1 if \a str ends with \a suffix, 0 otherwise.
+ */
+static int force_inline attribute_pure ast_ends_with(const char *str, const char *suffix)
+{
+ size_t str_len;
+ size_t suffix_len;
+
+ ast_assert(str != NULL);
+ ast_assert(suffix != NULL);
+ str_len = strlen(str);
+ suffix_len = strlen(suffix);
+
+ if (suffix_len > str_len) {
+ return 0;
+ }
+
+ return strcmp(str + str_len - suffix_len, suffix) == 0;
+}
+
/*!
\brief Gets a pointer to the first non-whitespace character in a string.
\param str the input string
Modified: team/dlee/string-ops/tests/test_strings.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/string-ops/tests/test_strings.c?view=diff&rev=381808&r1=381807&r2=381808
==============================================================================
--- team/dlee/string-ops/tests/test_strings.c (original)
+++ team/dlee/string-ops/tests/test_strings.c Tue Feb 19 16:47:33 2013
@@ -251,15 +251,78 @@
return res;
}
+AST_TEST_DEFINE(begins_with_test)
+{
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "begins_with";
+ info->category = "/main/strings/";
+ info->summary = "Test ast_begins_with";
+ info->description = "Test ast_begins_with";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ // prefixes
+ ast_test_validate(test, 1 == ast_begins_with("foobar", "foobar"));
+ ast_test_validate(test, 1 == ast_begins_with("foobar", "foo"));
+ ast_test_validate(test, 1 == ast_begins_with("foobar", ""));
+ ast_test_validate(test, 1 == ast_begins_with("", ""));
+
+ // not prefixes
+ ast_test_validate(test, 0 == ast_begins_with("foobar", "bang"));
+ ast_test_validate(test, 0 == ast_begins_with("foobar", "foobat"));
+ ast_test_validate(test, 0 == ast_begins_with("boo", "boom"));
+ ast_test_validate(test, 0 == ast_begins_with("", "blitz"));
+
+ // nothing failed; we're all good!
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(ends_with_test)
+{
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "ends_with";
+ info->category = "/main/strings/";
+ info->summary = "Test ast_ends_with";
+ info->description = "Test ast_ends_with";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ // prefixes
+ ast_test_validate(test, 1 == ast_ends_with("foobar", "foobar"));
+ ast_test_validate(test, 1 == ast_ends_with("foobar", "bar"));
+ ast_test_validate(test, 1 == ast_ends_with("foobar", ""));
+ ast_test_validate(test, 1 == ast_ends_with("", ""));
+
+ // not suffixes
+ ast_test_validate(test, 0 == ast_ends_with("bar", "bbar"));
+ ast_test_validate(test, 0 == ast_ends_with("foobar", "bang"));
+ ast_test_validate(test, 0 == ast_ends_with("foobar", "foobat"));
+ ast_test_validate(test, 0 == ast_ends_with("boo", "boom"));
+ ast_test_validate(test, 0 == ast_ends_with("", "blitz"));
+
+ // nothing failed; we're all good!
+ return AST_TEST_PASS;
+}
+
static int unload_module(void)
{
AST_TEST_UNREGISTER(str_test);
+ AST_TEST_UNREGISTER(begins_with_test);
+ AST_TEST_UNREGISTER(ends_with_test);
return 0;
}
static int load_module(void)
{
AST_TEST_REGISTER(str_test);
+ AST_TEST_REGISTER(begins_with_test);
+ AST_TEST_REGISTER(ends_with_test);
return AST_MODULE_LOAD_SUCCESS;
}
More information about the asterisk-commits
mailing list