From c7ae706b2d13aa0194816d977103073fe2501259 Mon Sep 17 00:00:00 2001 From: George Joseph Date: Thu, 18 Sep 2014 19:22:39 +0000 Subject: utils: Create ast_strsep function that ignores separators inside quotes This function acts like strsep with three exceptions... * The separator is a single character instead of a string. * Separators inside quotes are treated literally instead of like separators. * You can elect to have leading and trailing whitespace and quotes stripped from the result and have '\' sequences unescaped. Like strsep, ast_strsep maintains no internal state and you can call it recursively using different separators on the same storage. Also like strsep, for consistent results, consecutive separators are not collapsed so you may get an empty string as a valid result. Tested by: George Joseph Review: https://reviewboard.asterisk.org/r/3989/ ........ Merged revisions 423476 from http://svn.asterisk.org/svn/asterisk/branches/12 git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@423478 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- tests/test_strings.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'tests/test_strings.c') diff --git a/tests/test_strings.c b/tests/test_strings.c index 5e5a17d27..127ee789d 100644 --- a/tests/test_strings.c +++ b/tests/test_strings.c @@ -310,11 +310,90 @@ AST_TEST_DEFINE(ends_with_test) return AST_TEST_PASS; } +AST_TEST_DEFINE(strsep_test) +{ + char *test1, *test2, *test3; + + switch (cmd) { + case TEST_INIT: + info->name = "strsep"; + info->category = "/main/strings/"; + info->summary = "Test ast_strsep"; + info->description = "Test ast_strsep"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + test1 = ast_strdupa("ghi=jkl,mno='pqr,stu',abc=def, vwx = yz1 , vwx = yz1 , '" + " vwx = yz1 ' , ' vwx , yz1 ',v\"w\"x, '\"x,v\",\"x\"' , \" i\\'m a test\"" + ", \" i\\'m a, test\", \" i\\'m a, test\", e\\,nd, end\\"); + + test2 = ast_strsep(&test1, ',', 0); + ast_test_validate(test, 0 == strcmp("ghi=jkl", test2)); + + test3 = ast_strsep(&test2, '=', 0); + ast_test_validate(test, 0 == strcmp("ghi", test3)); + + test3 = ast_strsep(&test2, '=', 0); + ast_test_validate(test, 0 == strcmp("jkl", test3)); + + test2 = ast_strsep(&test1, ',', 0); + ast_test_validate(test, 0 == strcmp("mno='pqr,stu'", test2)); + + test3 = ast_strsep(&test2, '=', 0); + ast_test_validate(test, 0 == strcmp("mno", test3)); + + test3 = ast_strsep(&test2, '=', 0); + ast_test_validate(test, 0 == strcmp("'pqr,stu'", test3)); + + test2 = ast_strsep(&test1, ',', 0); + ast_test_validate(test, 0 == strcmp("abc=def", test2)); + + test2 = ast_strsep(&test1, ',', 0); + ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM); + ast_test_validate(test, 0 == strcmp("vwx = yz1", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP); + ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM); + ast_test_validate(test, 0 == strcmp("vwx , yz1", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM); + ast_test_validate(test, 0 == strcmp("v\"w\"x", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM); + ast_test_validate(test, 0 == strcmp("'\"x,v\",\"x\"'", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM); + ast_test_validate(test, 0 == strcmp("\" i\\'m a test\"", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE); + ast_test_validate(test, 0 == strcmp("\" i'm a, test\"", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_ALL); + ast_test_validate(test, 0 == strcmp("i'm a, test", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE); + ast_test_validate(test, 0 == strcmp("e,nd", test2)); + + test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE); + ast_test_validate(test, 0 == strcmp("end", test2)); + + // 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); + AST_TEST_UNREGISTER(strsep_test); return 0; } @@ -323,6 +402,7 @@ static int load_module(void) AST_TEST_REGISTER(str_test); AST_TEST_REGISTER(begins_with_test); AST_TEST_REGISTER(ends_with_test); + AST_TEST_REGISTER(strsep_test); return AST_MODULE_LOAD_SUCCESS; } -- cgit v1.2.3