summaryrefslogtreecommitdiff
path: root/source4/scripting/ejs/smbcalls_string.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2005-11-04 04:05:48 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:45:45 -0500
commit218d306ece7f4c0cf962fdc4178dedc759869f4b (patch)
tree632c1c2de2be2e6f841f2466b756da8b2b2da414 /source4/scripting/ejs/smbcalls_string.c
parent794386e5c437756349317eaaf5538e75d40c5960 (diff)
downloadsamba-218d306ece7f4c0cf962fdc4178dedc759869f4b.tar.gz
samba-218d306ece7f4c0cf962fdc4178dedc759869f4b.tar.bz2
samba-218d306ece7f4c0cf962fdc4178dedc759869f4b.zip
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)
Diffstat (limited to 'source4/scripting/ejs/smbcalls_string.c')
-rw-r--r--source4/scripting/ejs/smbcalls_string.c27
1 files 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);