| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 // ECMA-262 section 15.5.4.3 | 55 // ECMA-262 section 15.5.4.3 |
| 56 function StringValueOf() { | 56 function StringValueOf() { |
| 57 if (!IS_STRING(this) && !%HasStringClass(this)) | 57 if (!IS_STRING(this) && !%HasStringClass(this)) |
| 58 throw new $TypeError('String.prototype.valueOf is not generic'); | 58 throw new $TypeError('String.prototype.valueOf is not generic'); |
| 59 return %_ValueOf(this); | 59 return %_ValueOf(this); |
| 60 } | 60 } |
| 61 | 61 |
| 62 | 62 |
| 63 // ECMA-262, section 15.5.4.4 | 63 // ECMA-262, section 15.5.4.4 |
| 64 function StringCharAt(pos) { | 64 function StringCharAt(pos) { |
| 65 var subject = ToString(this); | 65 var char_code = %_FastCharCodeAt(subject, index); |
| 66 var index = TO_INTEGER(pos); | 66 if (!%_IsSmi(char_code)) { |
| 67 if (index >= subject.length || index < 0) return ""; | 67 var subject = ToString(this); |
| 68 return %CharFromCode(%StringCharCodeAt(subject, index)); | 68 var index = TO_INTEGER(pos); |
| 69 if (index >= subject.length || index < 0) return ""; |
| 70 char_code = %StringCharCodeAt(subject, index); |
| 71 } |
| 72 return %CharFromCode(char_code); |
| 69 } | 73 } |
| 70 | 74 |
| 71 | 75 |
| 72 // ECMA-262 section 15.5.4.5 | 76 // ECMA-262 section 15.5.4.5 |
| 73 function StringCharCodeAt(pos) { | 77 function StringCharCodeAt(pos) { |
| 74 var fast_answer = %_FastCharCodeAt(this, pos); | 78 var fast_answer = %_FastCharCodeAt(this, pos); |
| 75 if (%_IsSmi(fast_answer)) { | 79 if (%_IsSmi(fast_answer)) { |
| 76 return fast_answer; | 80 return fast_answer; |
| 77 } | 81 } |
| 78 var subject = ToString(this); | 82 var subject = ToString(this); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 172 |
| 169 return result; | 173 return result; |
| 170 } | 174 } |
| 171 | 175 |
| 172 | 176 |
| 173 // SubString is an internal function that returns the sub string of 'string'. | 177 // SubString is an internal function that returns the sub string of 'string'. |
| 174 // If resulting string is of length 1, we use the one character cache | 178 // If resulting string is of length 1, we use the one character cache |
| 175 // otherwise we call the runtime system. | 179 // otherwise we call the runtime system. |
| 176 function SubString(string, start, end) { | 180 function SubString(string, start, end) { |
| 177 // Use the one character string cache. | 181 // Use the one character string cache. |
| 178 if (start + 1 == end) return %CharFromCode(%StringCharCodeAt(string, start)); | 182 if (start + 1 == end) { |
| 183 var char_code = %_FastCharCodeAt(string, start); |
| 184 if (!%_IsSmi(char_code)) { |
| 185 char_code = %StringCharCodeAt(string, start); |
| 186 } |
| 187 return %CharFromCode(char_code); |
| 188 } |
| 179 return %StringSlice(string, start, end); | 189 return %StringSlice(string, start, end); |
| 180 } | 190 } |
| 181 | 191 |
| 182 | 192 |
| 183 // ECMA-262, section 15.5.4.11 | 193 // ECMA-262, section 15.5.4.11 |
| 184 function StringReplace(search, replace) { | 194 function StringReplace(search, replace) { |
| 185 var subject = ToString(this); | 195 var subject = ToString(this); |
| 186 | 196 |
| 187 // Delegate to one of the regular expression variants if necessary. | 197 // Delegate to one of the regular expression variants if necessary. |
| 188 if (IS_REGEXP(search)) { | 198 if (IS_REGEXP(search)) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 // Compute the number of captures; see ECMA-262, 15.5.4.11, p. 102. | 283 // Compute the number of captures; see ECMA-262, 15.5.4.11, p. 102. |
| 274 var m = captures.length >> 1; // includes the match | 284 var m = captures.length >> 1; // includes the match |
| 275 | 285 |
| 276 if (next > 0) builder.add(SubString(string, 0, next)); | 286 if (next > 0) builder.add(SubString(string, 0, next)); |
| 277 var length = string.length; | 287 var length = string.length; |
| 278 | 288 |
| 279 while (true) { | 289 while (true) { |
| 280 var expansion = '$'; | 290 var expansion = '$'; |
| 281 var position = next + 1; | 291 var position = next + 1; |
| 282 if (position < length) { | 292 if (position < length) { |
| 283 var peek = %StringCharCodeAt(string, position); | 293 var peek = %_FastCharCodeAt(string, position); |
| 294 if (!%_IsSmi(peek)) { |
| 295 peek = %StringCharCodeAt(string, position); |
| 296 } |
| 284 if (peek == 36) { // $$ | 297 if (peek == 36) { // $$ |
| 285 ++position; | 298 ++position; |
| 286 builder.add('$'); | 299 builder.add('$'); |
| 287 } else if (peek == 38) { // $& - match | 300 } else if (peek == 38) { // $& - match |
| 288 ++position; | 301 ++position; |
| 289 builder.addSpecialSlice(captures[0], captures[1]); | 302 builder.addSpecialSlice(captures[0], captures[1]); |
| 290 } else if (peek == 96) { // $` - prefix | 303 } else if (peek == 96) { // $` - prefix |
| 291 ++position; | 304 ++position; |
| 292 builder.addSpecialSlice(0, captures[0]); | 305 builder.addSpecialSlice(0, captures[0]); |
| 293 } else if (peek == 39) { // $' - suffix | 306 } else if (peek == 39) { // $' - suffix |
| 294 ++position; | 307 ++position; |
| 295 builder.addSpecialSlice(captures[1], subject.length); | 308 builder.addSpecialSlice(captures[1], subject.length); |
| 296 } else if (peek >= 48 && peek <= 57) { // $n, 0 <= n <= 9 | 309 } else if (peek >= 48 && peek <= 57) { // $n, 0 <= n <= 9 |
| 297 ++position; | 310 ++position; |
| 298 var n = peek - 48; | 311 var n = peek - 48; |
| 299 if (position < length) { | 312 if (position < length) { |
| 300 peek = %StringCharCodeAt(string, position); | 313 peek = %_FastCharCodeAt(string, position); |
| 314 if (!%_IsSmi(peek)) { |
| 315 peek = %StringCharCodeAt(string, position); |
| 316 } |
| 301 // $nn, 01 <= nn <= 99 | 317 // $nn, 01 <= nn <= 99 |
| 302 if (n != 0 && peek == 48 || peek >= 49 && peek <= 57) { | 318 if (n != 0 && peek == 48 || peek >= 49 && peek <= 57) { |
| 303 var nn = n * 10 + (peek - 48); | 319 var nn = n * 10 + (peek - 48); |
| 304 if (nn < m) { | 320 if (nn < m) { |
| 305 // If the two digit capture reference is within range of | 321 // If the two digit capture reference is within range of |
| 306 // the captures, we use it instead of the single digit | 322 // the captures, we use it instead of the single digit |
| 307 // one. Otherwise, we fall back to using the single | 323 // one. Otherwise, we fall back to using the single |
| 308 // digit reference. This matches the behavior of | 324 // digit reference. This matches the behavior of |
| 309 // SpiderMonkey. | 325 // SpiderMonkey. |
| 310 ++position; | 326 ++position; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 | 375 |
| 360 | 376 |
| 361 // Add the string of a given PCRE capture to the ReplaceResultBuilder | 377 // Add the string of a given PCRE capture to the ReplaceResultBuilder |
| 362 function addCaptureString(builder, captures, index) { | 378 function addCaptureString(builder, captures, index) { |
| 363 // Scale the index. | 379 // Scale the index. |
| 364 var scaled = index << 1; | 380 var scaled = index << 1; |
| 365 // Compute start and end. | 381 // Compute start and end. |
| 366 var start = captures[scaled]; | 382 var start = captures[scaled]; |
| 367 var end = captures[scaled + 1]; | 383 var end = captures[scaled + 1]; |
| 368 // If either start or end is missing return. | 384 // If either start or end is missing return. |
| 369 if (start < 0 || end < 0) return; | 385 if (start < 0 || end <= start) return; |
| 370 builder.addSpecialSlice(start, end); | 386 builder.addSpecialSlice(start, end); |
| 371 }; | 387 }; |
| 372 | 388 |
| 373 | 389 |
| 374 // Helper function for replacing regular expressions with the result of a | 390 // Helper function for replacing regular expressions with the result of a |
| 375 // function application in String.prototype.replace. The function application | 391 // function application in String.prototype.replace. The function application |
| 376 // must be interleaved with the regexp matching (contrary to ECMA-262 | 392 // must be interleaved with the regexp matching (contrary to ECMA-262 |
| 377 // 15.5.4.11) to mimic SpiderMonkey and KJS behavior when the function uses | 393 // 15.5.4.11) to mimic SpiderMonkey and KJS behavior when the function uses |
| 378 // the static properties of the RegExp constructor. Example: | 394 // the static properties of the RegExp constructor. Example: |
| 379 // 'abcd'.replace(/(.)/g, function() { return RegExp.$1; } | 395 // 'abcd'.replace(/(.)/g, function() { return RegExp.$1; } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 | 446 |
| 431 // Helper function to apply a string replacement function once. | 447 // Helper function to apply a string replacement function once. |
| 432 function ApplyReplacementFunction(replace, captures, subject) { | 448 function ApplyReplacementFunction(replace, captures, subject) { |
| 433 // Compute the parameter list consisting of the match, captures, index, | 449 // Compute the parameter list consisting of the match, captures, index, |
| 434 // and subject for the replace function invocation. | 450 // and subject for the replace function invocation. |
| 435 var index = captures[0]; | 451 var index = captures[0]; |
| 436 // The number of captures plus one for the match. | 452 // The number of captures plus one for the match. |
| 437 var m = captures.length >> 1; | 453 var m = captures.length >> 1; |
| 438 if (m == 1) { | 454 if (m == 1) { |
| 439 var s = CaptureString(subject, captures, 0); | 455 var s = CaptureString(subject, captures, 0); |
| 456 // Don't call directly to avoid exposing the built-in global object. |
| 440 return ToString(replace.call(null, s, index, subject)); | 457 return ToString(replace.call(null, s, index, subject)); |
| 441 } | 458 } |
| 442 var parameters = $Array(m + 2); | 459 var parameters = $Array(m + 2); |
| 443 for (var j = 0; j < m; j++) { | 460 for (var j = 0; j < m; j++) { |
| 444 parameters[j] = CaptureString(subject, captures, j); | 461 parameters[j] = CaptureString(subject, captures, j); |
| 445 } | 462 } |
| 446 parameters[j] = index; | 463 parameters[j] = index; |
| 447 parameters[j + 1] = subject; | 464 parameters[j + 1] = subject; |
| 448 return ToString(replace.apply(null, parameters)); | 465 return ToString(replace.apply(null, parameters)); |
| 449 } | 466 } |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 866 "italics", StringItalics, | 883 "italics", StringItalics, |
| 867 "small", StringSmall, | 884 "small", StringSmall, |
| 868 "strike", StringStrike, | 885 "strike", StringStrike, |
| 869 "sub", StringSub, | 886 "sub", StringSub, |
| 870 "sup", StringSup | 887 "sup", StringSup |
| 871 )); | 888 )); |
| 872 } | 889 } |
| 873 | 890 |
| 874 | 891 |
| 875 SetupString(); | 892 SetupString(); |
| OLD | NEW |