diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2007-09-24 16:43:16 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 15:07:12 -0500 |
commit | 6b1c7d36f88b0c7cfdf16628d33512d4bacced88 (patch) | |
tree | a15f21185e7654045312978d9001fece25d1b9ed /source4 | |
parent | f2d64e1c45994e4b519454c071e90e0cd8240c8a (diff) | |
download | samba-6b1c7d36f88b0c7cfdf16628d33512d4bacced88.tar.gz samba-6b1c7d36f88b0c7cfdf16628d33512d4bacced88.tar.bz2 samba-6b1c7d36f88b0c7cfdf16628d33512d4bacced88.zip |
r25307: add string_sub_talloc.
(This used to be commit 96c1a24874289fdeddcac43d23c2d1214b9b6225)
Diffstat (limited to 'source4')
-rw-r--r-- | source4/lib/util/tests/str.c | 43 | ||||
-rw-r--r-- | source4/lib/util/util.h | 4 | ||||
-rw-r--r-- | source4/lib/util/util_str.c | 36 |
3 files changed, 83 insertions, 0 deletions
diff --git a/source4/lib/util/tests/str.c b/source4/lib/util/tests/str.c index 4a964af0ee..a219ef0891 100644 --- a/source4/lib/util/tests/str.c +++ b/source4/lib/util/tests/str.c @@ -58,6 +58,40 @@ static bool test_string_sub_shorter(struct torture_context *tctx) 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 @@ struct torture_suite *torture_local_util_str(TALLOC_CTX *mem_ctx) 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; } diff --git a/source4/lib/util/util.h b/source4/lib/util/util.h index 68bf326d87..1960aa6196 100644 --- a/source4/lib/util/util.h +++ b/source4/lib/util/util.h @@ -383,6 +383,10 @@ _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_ **/ _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! diff --git a/source4/lib/util/util_str.c b/source4/lib/util/util_str.c index e9f81dbd9b..9ea6403c52 100644 --- a/source4/lib/util/util_str.c +++ b/source4/lib/util/util_str.c @@ -317,6 +317,42 @@ _PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_ } } +/** + * 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. |