| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 var result = %StringMatch(subject, regexp, lastMatchInfo); | 192 var result = %StringMatch(subject, regexp, lastMatchInfo); |
| 193 if (result !== null) lastMatchInfoOverride = null; | 193 if (result !== null) lastMatchInfoOverride = null; |
| 194 return result; | 194 return result; |
| 195 } | 195 } |
| 196 // Non-regexp argument. | 196 // Non-regexp argument. |
| 197 regexp = new $RegExp(regexp); | 197 regexp = new $RegExp(regexp); |
| 198 return RegExpExecNoTests(regexp, subject, 0); | 198 return RegExpExecNoTests(regexp, subject, 0); |
| 199 } | 199 } |
| 200 | 200 |
| 201 | 201 |
| 202 function StringMatchNoResult(regexp) { |
| 203 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 204 throw MakeTypeError("called_on_null_or_undefined", |
| 205 ["String.prototype.match"]); |
| 206 } |
| 207 var subject = TO_STRING_INLINE(this); |
| 208 if (!IS_REGEXP(regexp)) |
| 209 regexp = new $RegExp(regexp); |
| 210 if (!regexp.global) { |
| 211 %_CallFunction(regexp, subject, RegExpExecNoResult); |
| 212 } else { |
| 213 regexp.lastIndex = 0; |
| 214 var previousLastIndex = 0; |
| 215 var lastMatch = true; |
| 216 while (lastMatch) { |
| 217 var result = %_CallFunction(regexp, subject, RegExpExecNoResult); |
| 218 if (result == null) |
| 219 lastMatch = false; |
| 220 else { |
| 221 var thisIndex = regexp.lastIndex; |
| 222 if (thisIndex == previousLastIndex) { |
| 223 regexp.lastIndex += 1; |
| 224 previousLastIndex += 1; |
| 225 } else { |
| 226 previousLastIndex = thisIndex; |
| 227 } |
| 228 } |
| 229 } |
| 230 } |
| 231 } |
| 232 |
| 202 // SubString is an internal function that returns the sub string of 'string'. | 233 // SubString is an internal function that returns the sub string of 'string'. |
| 203 // If resulting string is of length 1, we use the one character cache | 234 // If resulting string is of length 1, we use the one character cache |
| 204 // otherwise we call the runtime system. | 235 // otherwise we call the runtime system. |
| 205 function SubString(string, start, end) { | 236 function SubString(string, start, end) { |
| 206 // Use the one character string cache. | 237 // Use the one character string cache. |
| 207 if (start + 1 == end) return %_StringCharAt(string, start); | 238 if (start + 1 == end) return %_StringCharAt(string, start); |
| 208 return %_SubString(string, start, end); | 239 return %_SubString(string, start, end); |
| 209 } | 240 } |
| 210 | 241 |
| 211 | 242 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 reusableMatchInfo[CAPTURE0] = start; | 318 reusableMatchInfo[CAPTURE0] = start; |
| 288 reusableMatchInfo[CAPTURE1] = end; | 319 reusableMatchInfo[CAPTURE1] = end; |
| 289 replace = TO_STRING_INLINE(replace); | 320 replace = TO_STRING_INLINE(replace); |
| 290 result = ExpandReplacement(replace, subject, reusableMatchInfo, result); | 321 result = ExpandReplacement(replace, subject, reusableMatchInfo, result); |
| 291 } | 322 } |
| 292 | 323 |
| 293 return result + SubString(subject, end, subject.length); | 324 return result + SubString(subject, end, subject.length); |
| 294 } | 325 } |
| 295 | 326 |
| 296 | 327 |
| 328 function StringReplaceNoResult(search, replace) { |
| 329 if (IS_FUNCTION(replace)) { |
| 330 // The function may have side effects, so we punt to the real method |
| 331 return %_CallFunction(this, search, replace, StringReplace); |
| 332 } |
| 333 |
| 334 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 335 throw MakeTypeError("called_on_null_or_undefined", |
| 336 ["String.prototype.replace"]); |
| 337 } |
| 338 |
| 339 var subject = TO_STRING_INLINE(this); |
| 340 |
| 341 if (IS_REGEXP(search)) { |
| 342 %_CallFunction(subject, search, StringMatchNoResult); |
| 343 } else { |
| 344 TO_STRING_INLINE(search); |
| 345 } |
| 346 TO_STRING_INLINE(replace); |
| 347 } |
| 348 |
| 349 |
| 297 // Expand the $-expressions in the string and return a new string with | 350 // Expand the $-expressions in the string and return a new string with |
| 298 // the result. | 351 // the result. |
| 299 function ExpandReplacement(string, subject, matchInfo, result) { | 352 function ExpandReplacement(string, subject, matchInfo, result) { |
| 300 var length = string.length; | 353 var length = string.length; |
| 301 var next = %StringIndexOf(string, '$', 0); | 354 var next = %StringIndexOf(string, '$', 0); |
| 302 if (next < 0) { | 355 if (next < 0) { |
| 303 if (length > 0) result += string; | 356 if (length > 0) result += string; |
| 304 return result; | 357 return result; |
| 305 } | 358 } |
| 306 | 359 |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 } | 700 } |
| 648 if (result.length === limit) break outer_loop; | 701 if (result.length === limit) break outer_loop; |
| 649 } | 702 } |
| 650 | 703 |
| 651 startIndex = currentIndex = endIndex; | 704 startIndex = currentIndex = endIndex; |
| 652 } | 705 } |
| 653 return result; | 706 return result; |
| 654 } | 707 } |
| 655 | 708 |
| 656 | 709 |
| 710 function StringSplitNoResult(separator, limit) { |
| 711 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 712 throw MakeTypeError("called_on_null_or_undefined", |
| 713 ["String.prototype.split"]); |
| 714 } |
| 715 TO_STRING_INLINE(this); |
| 716 limit = (IS_UNDEFINED(limit)) ? 0xffffffff : TO_UINT32(limit); |
| 717 if (limit === 0) return; |
| 718 |
| 719 // ECMA-262 says that if separator is undefined, the result should |
| 720 // be an array of size 1 containing the entire string. SpiderMonkey |
| 721 // and KJS have this behavior only when no separator is given. If |
| 722 // undefined is explicitly given, they convert it to a string and |
| 723 // use that. We do as SpiderMonkey and KJS. |
| 724 if (%_ArgumentsLength() === 0) { |
| 725 return; |
| 726 } |
| 727 |
| 728 if (!IS_REGEXP(separator)) |
| 729 TO_STRING_INLINE(separator); |
| 730 } |
| 731 |
| 732 |
| 657 // ECMA-262 section 15.5.4.15 | 733 // ECMA-262 section 15.5.4.15 |
| 658 function StringSubstring(start, end) { | 734 function StringSubstring(start, end) { |
| 659 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 735 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 660 throw MakeTypeError("called_on_null_or_undefined", | 736 throw MakeTypeError("called_on_null_or_undefined", |
| 661 ["String.prototype.subString"]); | 737 ["String.prototype.subString"]); |
| 662 } | 738 } |
| 663 var s = TO_STRING_INLINE(this); | 739 var s = TO_STRING_INLINE(this); |
| 664 var s_len = s.length; | 740 var s_len = s.length; |
| 665 | 741 |
| 666 var start_i = TO_INTEGER(start); | 742 var start_i = TO_INTEGER(start); |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 983 "fixed", StringFixed, | 1059 "fixed", StringFixed, |
| 984 "italics", StringItalics, | 1060 "italics", StringItalics, |
| 985 "small", StringSmall, | 1061 "small", StringSmall, |
| 986 "strike", StringStrike, | 1062 "strike", StringStrike, |
| 987 "sub", StringSub, | 1063 "sub", StringSub, |
| 988 "sup", StringSup | 1064 "sup", StringSup |
| 989 )); | 1065 )); |
| 990 } | 1066 } |
| 991 | 1067 |
| 992 SetUpString(); | 1068 SetUpString(); |
| OLD | NEW |