3 * @brief EJScript string class
5 /********************************* Copyright **********************************/
9 * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
10 * Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
12 * This software is distributed under commercial and open source licenses.
13 * You may use the GPL open source license described below or you may acquire
14 * a commercial license from Mbedthis Software. You agree to be fully bound
15 * by the terms of either license. Consult the LICENSE.TXT distributed with
16 * this software for full details.
18 * This software is open source; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2 of the License, or (at your
21 * option) any later version. See the GNU General Public License for more
22 * details at: http://www.mbedthis.com/downloads/gplLicense.html
24 * This program is distributed WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 * This GPL license does NOT permit incorporating this software into
28 * proprietary programs. If you are unable to comply with the GPL, you must
29 * acquire a commercial license to use this software. Commercial licenses
30 * for this software and support services are available from Mbedthis
31 * Software at http://www.mbedthis.com
35 /********************************** Includes **********************************/
40 /******************************************************************************/
41 /*********************************** Constructors *****************************/
42 /******************************************************************************/
47 int ejsStringConstructor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
52 ejsSetReturnValueToString(ejs, "");
54 } else if (argc == 1) {
56 str = ejsVarToString(ejs, argv[0]);
57 ejsSetReturnValueToString(ejs, str);
60 ejsArgError(ejs, "usage: String([var])");
67 /******************************************************************************/
68 /******************************** Visible Methods *****************************/
69 /******************************************************************************/
71 * Return a string containing the character at a given index
73 * String string.charAt(Number)
76 static int charAt(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
82 ejsArgError(ejs, "usage: charAt(integer)");
86 num = ejsVarToNumber(argv[0]);
87 if (num < 0 || num >= thisObj->length) {
88 ejsError(ejs, EJS_RANGE_ERROR, "Bad index");
92 mprAssert(ejsVarIsString(thisObj));
94 buf[0] = argv[0]->string[num];
96 ejsSetReturnValueToString(ejs, buf);
101 /******************************************************************************/
103 * Return an integer containing the character at a given index
105 * Number string.charCodeAt(Number)
108 static EjsNum charCodeAt(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
113 ejsArgError(ejs, "usage: charCodeAt(integer)");
117 num = ejsVarToNumber(argv[0]);
118 if (num < 0 || num >= thisObj->length) {
119 ejsError(ejs, EJS_RANGE_ERROR, "Bad index");
122 ejsSetReturnValueToNumber(ejs, (EjsNum) argv[0]->string[num]);
127 /******************************************************************************/
131 * String string.catenate(var, ...)
134 static int concat(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
139 ejsArgError(ejs, "usage: concat(String, ...)");
143 mprAssert(ejsVarIsString(thisObj));
145 for (i = 0; i < argc; i++) {
146 if (ejsStrcat(ejs, thisObj, argv[i]) < 0) {
151 ejsSetReturnValue(ejs, thisObj);
155 /******************************************************************************/
157 * Return the position of the first occurance of a substring
159 * Number string.indexOf(String subString [, Number start])
162 static int indexOf(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
164 char *pat, *s1, *s2, *origin;
167 if (argc == 0 || argc > 2) {
168 ejsArgError(ejs, "usage: indexOf(String [, Number])");
172 pat = ejsVarToString(ejs, argv[0]);
175 start = ejsVarToNumber(argv[1]);
176 if (start > thisObj->length) {
177 start = thisObj->length;
184 for (origin = &thisObj->string[i]; i < thisObj->length; i++, origin++) {
186 for (s2 = pat; *s1 && *s2; s1++, s2++) {
192 ejsSetReturnValueToNumber(ejs, (EjsNum) (origin - thisObj->string));
196 ejsSetReturnValueToNumber(ejs, (EjsNum) -1);
200 /******************************************************************************/
202 * Return the position of the last occurance of a substring
204 * Number string.lastIndexOf(String subString [, Number start])
207 static int lastIndexOf(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
209 char *pat, *s1, *s2, *origin;
212 if (argc == 0 || argc > 2) {
213 ejsArgError(ejs, "usage: indexOf(String [, Number])");
217 pat = ejsVarToString(ejs, argv[0]);
220 start = ejsVarToNumber(argv[1]);
221 if (start > thisObj->length) {
222 start = thisObj->length;
228 origin = &thisObj->string[thisObj->length - 1];
229 for (; origin >= &thisObj->string[start]; origin--) {
232 for (s2 = pat; *s1 && *s2; s1++, s2++) {
238 ejsSetReturnValueToNumber(ejs, (EjsNum) (origin - thisObj->string));
242 ejsSetReturnValueToNumber(ejs, (EjsNum) -1);
246 /******************************************************************************/
250 * Number string.slice(Number start, Number end)
253 static int slice(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
258 ejsArgError(ejs, "usage: slice(Number, Number)");
262 start = ejsVarToNumber(argv[0]);
263 end = ejsVarToNumber(argv[1]);
264 if (start < 0 || start >= thisObj->length) {
265 ejsError(ejs, EJS_RANGE_ERROR, "Bad start index");
268 if (end < 0 || end >= thisObj->length) {
269 ejsError(ejs, EJS_RANGE_ERROR, "Bad end index");
273 mprAssert(ejsVarIsString(thisObj));
275 ejsSetReturnValueToBinaryString(ejs, (uchar*) &thisObj->string[start],
281 /******************************************************************************/
285 * Number string.split(String delimiter [, Number limit])
288 static int split(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
291 char *delim, *last, *cp;
292 int len, limit, alloc;
294 if (argc == 0 || argc > 2) {
295 ejsArgError(ejs, "usage: split(String [, Number])");
299 delim = ejsVarToStringEx(ejs, argv[0], &alloc);
301 limit = ejsVarToNumber(argv[1]);
303 array = ejsCreateArray(ejs, 0);
307 last = thisObj->string;
308 for (cp = last; *cp; cp++) {
309 if (*cp == *delim && strncmp(cp, delim, len) == 0) {
311 vp = ejsCreateBinaryStringVar(ejs, (uchar*) last, (cp - last));
312 ejsAddArrayElt(ejs, array, vp, EJS_SHALLOW_COPY);
318 ejsSetReturnValue(ejs, array);
319 ejsFreeVar(ejs, array);
328 /******************************************************************************/
330 * Create the object class
333 int ejsDefineStringClass(Ejs *ejs)
337 sc = ejsDefineClass(ejs, "String", "Object", ejsStringConstructor);
339 return MPR_ERR_CANT_INITIALIZE;
342 ejsDefineCMethod(ejs, sc, "charAt", charAt, EJS_NO_LOCAL);
343 ejsDefineCMethod(ejs, sc, "charCodeAt", charCodeAt, EJS_NO_LOCAL);
344 ejsDefineCMethod(ejs, sc, "concat", concat, EJS_NO_LOCAL);
345 ejsDefineCMethod(ejs, sc, "indexOf", indexOf, EJS_NO_LOCAL);
346 ejsDefineCMethod(ejs, sc, "lastIndexOf", lastIndexOf, EJS_NO_LOCAL);
347 ejsDefineCMethod(ejs, sc, "slice", slice, EJS_NO_LOCAL);
348 ejsDefineCMethod(ejs, sc, "split", split, EJS_NO_LOCAL);
350 ejsDefineCMethod(ejs, sc, "match", match, EJS_NO_LOCAL);
351 ejsDefineCMethod(ejs, sc, "replace", replace, EJS_NO_LOCAL);
352 ejsDefineCMethod(ejs, sc, "search", search, EJS_NO_LOCAL);
353 ejsDefineCMethod(ejs, sc, "substring", substring, EJS_NO_LOCAL);
355 ejsDefineCMethod(ejs, sc, "substr", substr, EJS_NO_LOCAL);
356 ejsDefineCMethod(ejs, sc, "toLowerCase", toLowerCase, EJS_NO_LOCAL);
357 ejsDefineCMethod(ejs, sc, "toUpperCase", toUpperCase, EJS_NO_LOCAL);
360 ejsDefineCMethod(ejs, sc, "fromCharCode", fromCharCode, 0, EJS_NO_LOCAL);
363 if (ejsObjHasErrors(sc)) {
365 return MPR_ERR_CANT_CREATE;
370 /******************************************************************************/
371 #endif /* BLD_FEATURE_EJS */
379 * vim600: sw=4 ts=4 fdm=marker