summaryrefslogtreecommitdiff
path: root/source4/scripting/ejs
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@samba.org>2005-08-29 20:18:51 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:34:56 -0500
commitc9ace93ce22fed9e1b4e44a853e23bf66cf99184 (patch)
treeba89ee82e66f1b9ca217e05e7413e9bb9d496561 /source4/scripting/ejs
parentd4f22109ee5b939d34fe11ad4cef29d22fd82cc8 (diff)
downloadsamba-c9ace93ce22fed9e1b4e44a853e23bf66cf99184.tar.gz
samba-c9ace93ce22fed9e1b4e44a853e23bf66cf99184.tar.bz2
samba-c9ace93ce22fed9e1b4e44a853e23bf66cf99184.zip
r9756: One-way upgrade from Samba3->Samba4 basically works now
Still need to polish some rough edges (This used to be commit a8f309aa812533f57a90410722dfb342c8cf3b48)
Diffstat (limited to 'source4/scripting/ejs')
-rw-r--r--source4/scripting/ejs/smbcalls_string.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/source4/scripting/ejs/smbcalls_string.c b/source4/scripting/ejs/smbcalls_string.c
index 1b6b7d09dd..e127cdf8af 100644
--- a/source4/scripting/ejs/smbcalls_string.c
+++ b/source4/scripting/ejs/smbcalls_string.c
@@ -4,6 +4,7 @@
provide access to string functions
Copyright (C) Andrew Tridgell 2005
+ Copyright (C) Jelmer Vernooij 2005 (substr)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -124,6 +125,61 @@ static int ejs_split(MprVarHandle eid, int argc, char **argv)
return 0;
}
+/*
+ usage:
+ str = substr(orig[, start_offset[, length]]);
+
+ special cases:
+ if start_offset < 0 then start_offset+=strlen(orig)
+ if length < 0 then length+=strlen(orig)-start_offset
+
+ (as found in many other languages)
+*/
+static int ejs_substr(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+ int start_offset = 0;
+ int length = 0;
+ const char *orig;
+ char *target;
+
+ if (argc < 1 || argc > 3 ||
+ argv[0]->type != MPR_TYPE_STRING) {
+ ejsSetErrorMsg(eid, "substr invalid arguments");
+ return -1;
+ }
+
+ if (argc == 1) {
+ mpr_Return(eid, *argv[0]);
+ return 0;
+ }
+
+ orig = mprToString(argv[0]);
+ start_offset = mprToInt(argv[1]);
+ if (start_offset < 0) start_offset += strlen(orig);
+ if (start_offset < 0 || start_offset > strlen(orig)) {
+ ejsSetErrorMsg(eid, "substr arg 2 out of bounds");
+ return -1;
+ }
+
+ if (argc == 3) {
+ length = mprToInt(argv[1]);
+ if (length < 0) length += strlen(orig) - start_offset;
+ if (length < 0 || length+start_offset > strlen(orig)) {
+ ejsSetErrorMsg(eid, "substr arg 3 out of bounds");
+ return -1;
+ }
+ } else {
+ length = strlen(orig);
+ }
+
+ target = talloc_strndup(mprMemCtx(), orig+start_offset, length);
+
+ mpr_Return(eid, mprString(target));
+
+ talloc_free(target);
+
+ return 0;
+}
/*
usage:
@@ -421,6 +477,7 @@ static int ejs_string_init(MprVarHandle eid, int argc, struct MprVar **argv)
{
struct MprVar *obj = mprInitObject(eid, "string", argc, argv);
+ mprSetCFunction(obj, "substr", ejs_substr);
mprSetStringCFunction(obj, "strlen", ejs_strlen);
mprSetStringCFunction(obj, "strlower", ejs_strlower);
mprSetStringCFunction(obj, "strupper", ejs_strupper);