[asterisk-commits] dlee: branch dlee/stasis-http r379071 - in /team/dlee/stasis-http: include/as...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jan 14 15:53:50 CST 2013


Author: dlee
Date: Mon Jan 14 15:53:46 2013
New Revision: 379071

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=379071
Log:
ast_begins_with, with unit tests

Modified:
    team/dlee/stasis-http/include/asterisk/strings.h
    team/dlee/stasis-http/tests/test_strings.c

Modified: team/dlee/stasis-http/include/asterisk/strings.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/strings.h?view=diff&rev=379071&r1=379070&r2=379071
==============================================================================
--- team/dlee/stasis-http/include/asterisk/strings.h (original)
+++ team/dlee/stasis-http/include/asterisk/strings.h Mon Jan 14 15:53:46 2013
@@ -83,15 +83,22 @@
 #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
-  \param str String to check
-  \param prefix Prefix to look for
-  \param 1 if str begins with prefix, 0 otherwise
+  \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.
  */
 AST_INLINE_API(
 int attribute_pure ast_begins_with(const char *str, const char *prefix),
 {
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	ast_assert(str != NULL);
+	ast_assert(prefix != NULL);
+	while (*str == *prefix && *prefix != '\0') {
+		++str;
+		++prefix;
+	}
+	return *prefix == '\0';
 }
 )
 

Modified: team/dlee/stasis-http/tests/test_strings.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/tests/test_strings.c?view=diff&rev=379071&r1=379070&r2=379071
==============================================================================
--- team/dlee/stasis-http/tests/test_strings.c (original)
+++ team/dlee/stasis-http/tests/test_strings.c Mon Jan 14 15:53:46 2013
@@ -251,15 +251,47 @@
 	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;
+}
+
+
 static int unload_module(void)
 {
 	AST_TEST_UNREGISTER(str_test);
+	AST_TEST_UNREGISTER(begins_with_test);
 	return 0;
 }
 
 static int load_module(void)
 {
 	AST_TEST_REGISTER(str_test);
+	AST_TEST_REGISTER(begins_with_test);
 	return AST_MODULE_LOAD_SUCCESS;
 }
 




More information about the asterisk-commits mailing list