mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Add String.prototype.slice() (fix #376)
This commit is contained in:
parent
61140f4b39
commit
6db1009b18
@ -6,6 +6,7 @@
|
||||
Removed duplication in symbol lookup (fix #372, fix #323, fix #343)
|
||||
Fix JSON.stringify with circular references (fix #378)
|
||||
Fix String.indexOf when search string is bigger than the string being searched (fix #377)
|
||||
Add String.prototype.slice() (fix #376)
|
||||
|
||||
1v63 : Memory leak when defining functions (fix #359)
|
||||
Fix Instance properties overwrite prototype (fix #360)
|
||||
|
||||
@ -212,6 +212,26 @@ JsVar *jswrap_string_substr(JsVar *parent, JsVarInt pStart, JsVar *vLen) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/*JSON{ "type":"method", "class": "String", "name" : "slice",
|
||||
"generate" : "jswrap_string_slice",
|
||||
"params" : [ [ "start", "int", "The start character index, if negative it is from the end of the string"],
|
||||
[ "end", "JsVar", "The end character index, if negative it is from the end of the string, and if omitted it is the end of the string"] ],
|
||||
"return" : ["JsVar", "Part of this string from start for len characters"]
|
||||
}*/
|
||||
JsVar *jswrap_string_slice(JsVar *parent, JsVarInt pStart, JsVar *vEnd) {
|
||||
JsVar *res;
|
||||
JsVarInt pEnd = jsvIsUndefined(vEnd) ? JSVAPPENDSTRINGVAR_MAXLENGTH : (int)jsvGetInteger(vEnd);
|
||||
if (pStart<0) pStart += (JsVarInt)jsvGetStringLength(parent);
|
||||
if (pEnd<0) pEnd += (JsVarInt)jsvGetStringLength(parent);
|
||||
if (pStart<0) pStart = 0;
|
||||
if (pEnd<0) pEnd = 0;
|
||||
res = jsvNewWithFlags(JSV_STRING);
|
||||
if (!res) return 0; // out of memory
|
||||
jsvAppendStringVar(res, parent, (size_t)pStart, (size_t)(pEnd-pStart));
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/*JSON{ "type":"method", "class": "String", "name" : "split",
|
||||
"description" : "Return an array made by splitting this string up by the separator. eg. ```'1,2,3'.split(',')==[1,2,3]```",
|
||||
"generate" : "jswrap_string_split",
|
||||
|
||||
@ -21,5 +21,6 @@ int jswrap_string_indexOf(JsVar *parent, JsVar *substring, JsVar *fromIndex, boo
|
||||
JsVar *jswrap_string_replace(JsVar *parent, JsVar *subStr, JsVar *newSubStr);
|
||||
JsVar *jswrap_string_substring(JsVar *parent, JsVarInt pStart, JsVar *vEnd);
|
||||
JsVar *jswrap_string_substr(JsVar *parent, JsVarInt pStart, JsVar *vLen);
|
||||
JsVar *jswrap_string_slice(JsVar *parent, JsVarInt pStart, JsVar *vEnd);
|
||||
JsVar *jswrap_string_split(JsVar *parent, JsVar *split);
|
||||
JsVar *jswrap_string_toUpperLowerCase(JsVar *parent, bool upper);
|
||||
|
||||
12
tests/test_string_slice.js
Normal file
12
tests/test_string_slice.js
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
var r = [
|
||||
"foobar".slice(-1) === "r", // false, expected true
|
||||
"foobar".slice(-1, 10) === "r", // false, expected true
|
||||
"foobar".slice(-1, -1) === "", // false, expected true
|
||||
"foobar".slice(10, -10) === "", // false, expected true
|
||||
];
|
||||
|
||||
var pass = 0;
|
||||
r.forEach(function(a) { if (a) pass++; });
|
||||
|
||||
result = pass == r.length;
|
||||
Loading…
x
Reference in New Issue
Block a user