Author: jelmer Date: 2007-09-24 16:43:16 +0000 (Mon, 24 Sep 2007) New Revision: 25307
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=25307 Log: add string_sub_talloc. Modified: branches/SAMBA_4_0/ branches/SAMBA_4_0/source/lib/util/tests/str.c branches/SAMBA_4_0/source/lib/util/util.h branches/SAMBA_4_0/source/lib/util/util_str.c Changeset: Property changes on: branches/SAMBA_4_0 ___________________________________________________________________ Name: bzr:revision-info ...skipped... Name: bzr:file-ids ...skipped... Name: bzr:revision-id:v3-trunk0 ...skipped... Modified: branches/SAMBA_4_0/source/lib/util/tests/str.c =================================================================== --- branches/SAMBA_4_0/source/lib/util/tests/str.c 2007-09-24 15:55:26 UTC (rev 25306) +++ branches/SAMBA_4_0/source/lib/util/tests/str.c 2007-09-24 16:43:16 UTC (rev 25307) @@ -58,6 +58,40 @@ return true; } +static bool test_string_sub_special_char(struct torture_context *tctx) +{ + char tmp[100]; + safe_strcpy(tmp, "foobla", sizeof(tmp)); + string_sub(tmp, "foo", "%b;l", sizeof(tmp)); + torture_assert_str_equal(tctx, tmp, "_b_lbla", "invalid sub"); + return true; +} + +static bool test_string_sub_talloc_simple(struct torture_context *tctx) +{ + char *t; + + t = string_sub_talloc(tctx, "foobla", "foo", "bl"); + + torture_assert_str_equal(tctx, t, "blbla", "invalid sub"); + + return true; +} + +static bool test_string_sub_talloc_multiple(struct torture_context *tctx) +{ + char *t; + + t = string_sub_talloc(tctx, "fooblafoo", "foo", "aapnootmies"); + + torture_assert_str_equal(tctx, t, "aapnootmiesblaaapnootmies", + "invalid sub"); + + return true; +} + + + struct torture_suite *torture_local_util_str(TALLOC_CTX *mem_ctx) { struct torture_suite *suite = torture_suite_create(mem_ctx, "STR"); @@ -74,5 +108,14 @@ torture_suite_add_simple_test(suite, "string_sub_longer", test_string_sub_longer); + torture_suite_add_simple_test(suite, "string_sub_special_chars", + test_string_sub_special_char); + + torture_suite_add_simple_test(suite, "string_sub_talloc_simple", + test_string_sub_talloc_simple); + + torture_suite_add_simple_test(suite, "string_sub_talloc_multiple", + test_string_sub_talloc_multiple); + return suite; } Modified: branches/SAMBA_4_0/source/lib/util/util.h =================================================================== --- branches/SAMBA_4_0/source/lib/util/util.h 2007-09-24 15:55:26 UTC (rev 25306) +++ branches/SAMBA_4_0/source/lib/util/util.h 2007-09-24 16:43:16 UTC (rev 25307) @@ -383,6 +383,10 @@ **/ _PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len); + +_PUBLIC_ char *string_sub_talloc(TALLOC_CTX *mem_ctx, const char *s, + const char *pattern, const char *insert); + /** Similar to string_sub() but allows for any character to be substituted. Use with caution! Modified: branches/SAMBA_4_0/source/lib/util/util_str.c =================================================================== --- branches/SAMBA_4_0/source/lib/util/util_str.c 2007-09-24 15:55:26 UTC (rev 25306) +++ branches/SAMBA_4_0/source/lib/util/util_str.c 2007-09-24 16:43:16 UTC (rev 25307) @@ -317,7 +317,43 @@ } } +/** + * Talloc'ed version of string_sub + */ +_PUBLIC_ char *string_sub_talloc(TALLOC_CTX *mem_ctx, const char *s, + const char *pattern, const char *insert) +{ + const char *p; + char *ret; + size_t len, alloc_len; + if (insert == NULL || pattern == NULL || !*pattern || s == NULL) + return NULL; + + /* determine length needed */ + len = strlen(s); + + for (p = strstr(s, pattern); p != NULL; + p = strstr(p+strlen(pattern), pattern)) { + len += strlen(insert) - strlen(pattern); + } + + alloc_len = MAX(len, strlen(s))+1; + ret = talloc_array(mem_ctx, char, alloc_len); + if (ret == NULL) + return NULL; + strncpy(ret, s, alloc_len); + string_sub(ret, pattern, insert, alloc_len); + + ret = talloc_realloc(mem_ctx, ret, char, len+1); + if (ret == NULL) + return NULL; + + SMB_ASSERT(ret[len] == '\0'); + + return ret; +} + /** Similar to string_sub() but allows for any character to be substituted. Use with caution!