Fix String.split issue with last element (fix #352)

This commit is contained in:
Gordon Williams 2014-04-30 11:20:44 +01:00
parent 07a49baa10
commit 29f7eed9f0
3 changed files with 7 additions and 3 deletions

View File

@ -29,6 +29,7 @@
Added support for SPI LSB-first
WIZnet improvements (specifically on HTTP server)
Added WLAN.setIP for CC3000
Fix String.split issue with last element (fix #352)
1v61 : Fix toString crash for large numbers
Support floating-point literals without a leading 0 - eg '.5' (fix #296)

View File

@ -230,10 +230,10 @@ JsVar *jswrap_string_split(JsVar *parent, JsVar *split) {
int idx, last = 0;
int splitlen = jsvIsUndefined(split) ? 0 : (int)jsvGetStringLength(split);
int l = (int)jsvGetStringLength(parent) - splitlen;
int l = (int)jsvGetStringLength(parent) + 1 - splitlen;
for (idx=0;idx<=l;idx++) {
if (splitlen==0 &&idx==0) continue; // special case for where split string is ""
if (splitlen==0 && idx==0) continue; // special case for where split string is ""
if (idx==l || splitlen==0 || jsvCompareString(parent, split, (size_t)idx, 0, true)==0) {
if (idx==l) idx=l+splitlen; // if the last element, do to the end of the string
JsVar *part = jsvNewFromStringVar(parent, (size_t)last, (size_t)(idx-last));

View File

@ -2,4 +2,7 @@
var b = "1,4,7";
var a = b.split(",");
result = a.length==3 && a[0]==1 && a[1]==4 && a[2]==7;
var c = "Hello There".split("There");
result = a.length==3 && a[0]==1 && a[1]==4 && a[2]==7 && c.length==2 && c[0]=="Hello " && c[1]=="";