Author: tridge
Date: 2005-11-04 04:05:48 +0000 (Fri, 04 Nov 2005)
New Revision: 11498

WebSVN: 
http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=11498

Log:

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


Modified:
   branches/SAMBA_4_0/source/scripting/ejs/smbcalls_string.c


Changeset:
Modified: branches/SAMBA_4_0/source/scripting/ejs/smbcalls_string.c
===================================================================
--- branches/SAMBA_4_0/source/scripting/ejs/smbcalls_string.c   2005-11-04 
03:30:47 UTC (rev 11497)
+++ branches/SAMBA_4_0/source/scripting/ejs/smbcalls_string.c   2005-11-04 
04:05:48 UTC (rev 11498)
@@ -92,22 +92,30 @@
 /*
   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 @@
                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 @@
        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);

Reply via email to