| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
gTestfile = 'split-002.js'; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
var SECTION = "ecma_2/String/split-002.js"; |
| 35 |
var VERSION = "ECMA_2"; |
| 36 |
var TITLE = "String.prototype.split( regexp, [,limit] )"; |
| 37 |
|
| 38 |
startTest(); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
CompareSplit( "hello", "ll" ); |
| 51 |
|
| 52 |
CompareSplit( "hello", "l" ); |
| 53 |
CompareSplit( "hello", "x" ); |
| 54 |
CompareSplit( "hello", "h" ); |
| 55 |
CompareSplit( "hello", "o" ); |
| 56 |
CompareSplit( "hello", "hello" ); |
| 57 |
CompareSplit( "hello", undefined ); |
| 58 |
|
| 59 |
CompareSplit( "hello", ""); |
| 60 |
CompareSplit( "hello", "hellothere" ); |
| 61 |
|
| 62 |
CompareSplit( new String("hello" ) ); |
| 63 |
|
| 64 |
|
| 65 |
Number.prototype.split = String.prototype.split; |
| 66 |
|
| 67 |
CompareSplit( new Number(100111122133144155), 1 ); |
| 68 |
CompareSplitWithLimit(new Number(100111122133144155), 1, 1 ); |
| 69 |
|
| 70 |
CompareSplitWithLimit(new Number(100111122133144155), 1, 2 ); |
| 71 |
CompareSplitWithLimit(new Number(100111122133144155), 1, 0 ); |
| 72 |
CompareSplitWithLimit(new Number(100111122133144155), 1, 100 ); |
| 73 |
CompareSplitWithLimit(new Number(100111122133144155), 1, void 0 ); |
| 74 |
CompareSplitWithLimit(new Number(100111122133144155), 1, Math.pow(2,32)-1 ); |
| 75 |
CompareSplitWithLimit(new Number(100111122133144155), 1, "boo" ); |
| 76 |
CompareSplitWithLimit(new Number(100111122133144155), 1, -(Math.pow(2,32)-1) ); |
| 77 |
CompareSplitWithLimit( "hello", "l", NaN ); |
| 78 |
CompareSplitWithLimit( "hello", "l", 0 ); |
| 79 |
CompareSplitWithLimit( "hello", "l", 1 ); |
| 80 |
CompareSplitWithLimit( "hello", "l", 2 ); |
| 81 |
CompareSplitWithLimit( "hello", "l", 3 ); |
| 82 |
CompareSplitWithLimit( "hello", "l", 4 ); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
test(); |
| 106 |
|
| 107 |
function CompareSplit( string, separator ) { |
| 108 |
split_1 = string.split( separator ); |
| 109 |
split_2 = string_split( string, separator ); |
| 110 |
|
| 111 |
AddTestCase( |
| 112 |
"( " + string +".split(" + separator + ") ).length" , |
| 113 |
split_2.length, |
| 114 |
split_1.length ); |
| 115 |
|
| 116 |
var limit = split_1.length > split_2.length ? |
| 117 |
split_1.length : split_2.length; |
| 118 |
|
| 119 |
for ( var split_item = 0; split_item < limit; split_item++ ) { |
| 120 |
AddTestCase( |
| 121 |
string + ".split(" + separator + ")["+split_item+"]", |
| 122 |
split_2[split_item], |
| 123 |
split_1[split_item] ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
function CompareSplitWithLimit( string, separator, splitlimit ) { |
| 128 |
split_1 = string.split( separator, splitlimit ); |
| 129 |
split_2 = string_split( string, separator, splitlimit ); |
| 130 |
|
| 131 |
AddTestCase( |
| 132 |
"( " + string +".split(" + separator + ", " + splitlimit+") ).length" , |
| 133 |
split_2.length, |
| 134 |
split_1.length ); |
| 135 |
|
| 136 |
var limit = split_1.length > split_2.length ? |
| 137 |
split_1.length : split_2.length; |
| 138 |
|
| 139 |
for ( var split_item = 0; split_item < limit; split_item++ ) { |
| 140 |
AddTestCase( |
| 141 |
string + ".split(" + separator + ", " + splitlimit+")["+split_item+"]", |
| 142 |
split_2[split_item], |
| 143 |
split_1[split_item] ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
function string_split ( __this, separator, limit ) { |
| 148 |
var S = String(__this ); |
| 149 |
|
| 150 |
var A = new Array(); |
| 151 |
|
| 152 |
if ( limit == undefined ) { |
| 153 |
lim = Math.pow(2, 31 ) -1; |
| 154 |
} else { |
| 155 |
lim = ToUint32( limit ); |
| 156 |
} |
| 157 |
|
| 158 |
var s = S.length; |
| 159 |
var p = 0; |
| 160 |
|
| 161 |
if ( separator == undefined ) { |
| 162 |
A[0] = S; |
| 163 |
return A; |
| 164 |
} |
| 165 |
|
| 166 |
if ( separator.constructor == RegExp ) |
| 167 |
R = separator; |
| 168 |
else |
| 169 |
R = separator.toString(); |
| 170 |
|
| 171 |
if (lim == 0) return A; |
| 172 |
|
| 173 |
if ( separator == undefined ) { |
| 174 |
A[0] = S; |
| 175 |
return A; |
| 176 |
} |
| 177 |
|
| 178 |
if (s == 0) { |
| 179 |
z = SplitMatch(R, S, 0); |
| 180 |
if (z != false) return A; |
| 181 |
A[0] = S; |
| 182 |
return A; |
| 183 |
} |
| 184 |
|
| 185 |
var q = p; |
| 186 |
loop: |
| 187 |
while (true ) { |
| 188 |
|
| 189 |
if ( q == s ) break; |
| 190 |
|
| 191 |
z = SplitMatch(R, S, q); |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
if (z != false) { |
| 196 |
e = z.endIndex; |
| 197 |
cap = z.captures; |
| 198 |
if (e != p) { |
| 199 |
|
| 200 |
T = S.slice(p, q); |
| 201 |
|
| 202 |
A[A.length] = T; |
| 203 |
if (A.length == lim) return A; |
| 204 |
p = e; |
| 205 |
i = 0; |
| 206 |
while (true) { |
| 207 |
if (i == cap.length) { |
| 208 |
q = p; |
| 209 |
continue loop; |
| 210 |
} |
| 211 |
i = i + 1; |
| 212 |
A[A.length] = cap[i] |
| 213 |
if (A.length == lim) return A; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
q = q + 1; |
| 219 |
} |
| 220 |
|
| 221 |
T = S.slice(p, q); |
| 222 |
A[A.length] = T; |
| 223 |
return A; |
| 224 |
} |
| 225 |
|
| 226 |
function SplitMatch(R, S, q) |
| 227 |
{ |
| 228 |
if (R.constructor == RegExp) { |
| 229 |
var reResult = R.match(S, q); |
| 230 |
if (reResult == undefined) |
| 231 |
return false; |
| 232 |
else { |
| 233 |
a = new Array(reResult.length - 1); |
| 234 |
for (var i = 1; i < reResult.length; i++) |
| 235 |
a[a.length] = reResult[i]; |
| 236 |
return { endIndex : reResult.index + reResult[0].length, captures : cap }; |
| 237 |
} |
| 238 |
} |
| 239 |
else { |
| 240 |
var r = R.length; |
| 241 |
s = S.length; |
| 242 |
if ((q + r) > s) return false; |
| 243 |
for (var i = 0; i < r; i++) { |
| 244 |
|
| 245 |
if (S.charAt(q + i) != R.charAt(i)) |
| 246 |
return false; |
| 247 |
} |
| 248 |
cap = new Array(); |
| 249 |
return { endIndex : q + r, captures : cap }; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
function ToUint32( n ) { |
| 254 |
n = Number( n ); |
| 255 |
var sign = ( n < 0 ) ? -1 : 1; |
| 256 |
|
| 257 |
if ( Math.abs( n ) == 0 |
| 258 |
|| Math.abs( n ) == Number.POSITIVE_INFINITY |
| 259 |
|| n != n) { |
| 260 |
return 0; |
| 261 |
} |
| 262 |
n = sign * Math.floor( Math.abs(n) ) |
| 263 |
|
| 264 |
n = n % Math.pow(2,32); |
| 265 |
|
| 266 |
if ( n < 0 ){ |
| 267 |
n += Math.pow(2,32); |
| 268 |
} |
| 269 |
|
| 270 |
return ( n ); |
| 271 |
} |