From 218d306ece7f4c0cf962fdc4178dedc759869f4b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 4 Nov 2005 04:05:48 +0000 Subject: r11498: added an optional extra argument to split to limit the number of pieces a string is split into. This allows for a fix in the variable substitution used in provisioning (This used to be commit be06785d4835abcbc7d75c0176c85a8ecc0cc11d) --- source4/scripting/ejs/smbcalls_string.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/source4/scripting/ejs/smbcalls_string.c b/source4/scripting/ejs/smbcalls_string.c index e9e6e148d1..d742d43173 100644 --- a/source4/scripting/ejs/smbcalls_string.c +++ b/source4/scripting/ejs/smbcalls_string.c @@ -92,22 +92,30 @@ static int ejs_strstr(MprVarHandle eid, int argc, char **argv) /* usage: list = split(".", "a.foo.bar"); + list = split(".", "a.foo.bar", count); + + count is an optional count of how many splits to make NOTE: does not take a regular expression, unlike perl split() */ -static int ejs_split(MprVarHandle eid, int argc, char **argv) +static int ejs_split(MprVarHandle eid, int argc, struct MprVar **argv) { - const char *separator; - char *s, *p; + const char *separator, *s; + char *p; struct MprVar ret; - int count = 0; + int count = 0, maxcount=0; TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); - if (argc != 2) { + if (argc < 2 || + argv[0]->type != MPR_TYPE_STRING || + argv[1]->type != MPR_TYPE_STRING) { ejsSetErrorMsg(eid, "split invalid arguments"); return -1; } - separator = argv[0]; - s = argv[1]; + separator = mprToString(argv[0]); + s = mprToString(argv[1]); + if (argc == 3) { + maxcount = mprToInt(argv[2]); + } ret = mprArray("list"); @@ -116,6 +124,9 @@ static int ejs_split(MprVarHandle eid, int argc, char **argv) mprAddArray(&ret, count++, mprString(s2)); talloc_free(s2); s = p + strlen(separator); + if (maxcount != 0 && count >= maxcount) { + break; + } } if (*s) { mprAddArray(&ret, count++, mprString(s)); @@ -481,7 +492,7 @@ static int ejs_string_init(MprVarHandle eid, int argc, struct MprVar **argv) mprSetStringCFunction(obj, "strlower", ejs_strlower); mprSetStringCFunction(obj, "strupper", ejs_strupper); mprSetStringCFunction(obj, "strstr", ejs_strstr); - mprSetStringCFunction(obj, "split", ejs_split); + mprSetCFunction(obj, "split", ejs_split); mprSetCFunction(obj, "join", ejs_join); mprSetCFunction(obj, "sprintf", ejs_sprintf); mprSetCFunction(obj, "vsprintf", ejs_vsprintf); -- cgit