| OLD | NEW |
| 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 var start = %StringIndexOf(subject, search, 0); | 244 var start = %StringIndexOf(subject, search, 0); |
| 245 if (start < 0) return subject; | 245 if (start < 0) return subject; |
| 246 var end = start + search.length; | 246 var end = start + search.length; |
| 247 | 247 |
| 248 var builder = new ReplaceResultBuilder(subject); | 248 var builder = new ReplaceResultBuilder(subject); |
| 249 // prefix | 249 // prefix |
| 250 builder.addSpecialSlice(0, start); | 250 builder.addSpecialSlice(0, start); |
| 251 | 251 |
| 252 // Compute the string to replace with. | 252 // Compute the string to replace with. |
| 253 if (IS_FUNCTION(replace)) { | 253 if (IS_FUNCTION(replace)) { |
| 254 var receiver = | 254 var receiver = %GetDefaultReceiver(replace); |
| 255 %_IsNativeOrStrictMode(replace) ? void 0 : %GetGlobalReceiver(); | |
| 256 builder.add(%_CallFunction(receiver, | 255 builder.add(%_CallFunction(receiver, |
| 257 search, | 256 search, |
| 258 start, | 257 start, |
| 259 subject, | 258 subject, |
| 260 replace)); | 259 replace)); |
| 261 } else { | 260 } else { |
| 262 reusableMatchInfo[CAPTURE0] = start; | 261 reusableMatchInfo[CAPTURE0] = start; |
| 263 reusableMatchInfo[CAPTURE1] = end; | 262 reusableMatchInfo[CAPTURE1] = end; |
| 264 replace = TO_STRING_INLINE(replace); | 263 replace = TO_STRING_INLINE(replace); |
| 265 ExpandReplacement(replace, subject, reusableMatchInfo, builder); | 264 ExpandReplacement(replace, subject, reusableMatchInfo, builder); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 if (IS_NULL(res)) { | 412 if (IS_NULL(res)) { |
| 414 // No matches at all. | 413 // No matches at all. |
| 415 reusableReplaceArray = resultArray; | 414 reusableReplaceArray = resultArray; |
| 416 return subject; | 415 return subject; |
| 417 } | 416 } |
| 418 var len = res.length; | 417 var len = res.length; |
| 419 var i = 0; | 418 var i = 0; |
| 420 if (NUMBER_OF_CAPTURES(lastMatchInfo) == 2) { | 419 if (NUMBER_OF_CAPTURES(lastMatchInfo) == 2) { |
| 421 var match_start = 0; | 420 var match_start = 0; |
| 422 var override = new InternalArray(null, 0, subject); | 421 var override = new InternalArray(null, 0, subject); |
| 423 var receiver = | 422 var receiver = %GetDefaultReceiver(replace); |
| 424 %_IsNativeOrStrictMode(replace) ? void 0 : %GetGlobalReceiver(); | |
| 425 while (i < len) { | 423 while (i < len) { |
| 426 var elem = res[i]; | 424 var elem = res[i]; |
| 427 if (%_IsSmi(elem)) { | 425 if (%_IsSmi(elem)) { |
| 428 if (elem > 0) { | 426 if (elem > 0) { |
| 429 match_start = (elem >> 11) + (elem & 0x7ff); | 427 match_start = (elem >> 11) + (elem & 0x7ff); |
| 430 } else { | 428 } else { |
| 431 match_start = res[++i] - elem; | 429 match_start = res[++i] - elem; |
| 432 } | 430 } |
| 433 } else { | 431 } else { |
| 434 override[0] = elem; | 432 override[0] = elem; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 var endOfMatch = matchInfo[CAPTURE1]; | 469 var endOfMatch = matchInfo[CAPTURE1]; |
| 472 // Compute the parameter list consisting of the match, captures, index, | 470 // Compute the parameter list consisting of the match, captures, index, |
| 473 // and subject for the replace function invocation. | 471 // and subject for the replace function invocation. |
| 474 // The number of captures plus one for the match. | 472 // The number of captures plus one for the match. |
| 475 var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; | 473 var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; |
| 476 var replacement; | 474 var replacement; |
| 477 if (m == 1) { | 475 if (m == 1) { |
| 478 // No captures, only the match, which is always valid. | 476 // No captures, only the match, which is always valid. |
| 479 var s = SubString(subject, index, endOfMatch); | 477 var s = SubString(subject, index, endOfMatch); |
| 480 // Don't call directly to avoid exposing the built-in global object. | 478 // Don't call directly to avoid exposing the built-in global object. |
| 481 var receiver = | 479 var receiver = %GetDefaultReceiver(replace); |
| 482 %_IsNativeOrStrictMode(replace) ? void 0 : %GetGlobalReceiver(); | |
| 483 replacement = | 480 replacement = |
| 484 %_CallFunction(receiver, s, index, subject, replace); | 481 %_CallFunction(receiver, s, index, subject, replace); |
| 485 } else { | 482 } else { |
| 486 var parameters = new InternalArray(m + 2); | 483 var parameters = new InternalArray(m + 2); |
| 487 for (var j = 0; j < m; j++) { | 484 for (var j = 0; j < m; j++) { |
| 488 parameters[j] = CaptureString(subject, matchInfo, j); | 485 parameters[j] = CaptureString(subject, matchInfo, j); |
| 489 } | 486 } |
| 490 parameters[j] = index; | 487 parameters[j] = index; |
| 491 parameters[j + 1] = subject; | 488 parameters[j + 1] = subject; |
| 492 | 489 |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 907 // ReplaceResultBuilder support. | 904 // ReplaceResultBuilder support. |
| 908 function ReplaceResultBuilder(str) { | 905 function ReplaceResultBuilder(str) { |
| 909 if (%_ArgumentsLength() > 1) { | 906 if (%_ArgumentsLength() > 1) { |
| 910 this.elements = %_Arguments(1); | 907 this.elements = %_Arguments(1); |
| 911 } else { | 908 } else { |
| 912 this.elements = new InternalArray(); | 909 this.elements = new InternalArray(); |
| 913 } | 910 } |
| 914 this.special_string = str; | 911 this.special_string = str; |
| 915 } | 912 } |
| 916 | 913 |
| 917 ReplaceResultBuilder.prototype.__proto__ = null; | 914 SetUpLockedPrototype(ReplaceResultBuilder, |
| 918 | 915 $Array("elements", "special_string"), $Array( |
| 919 | 916 "add", function(str) { |
| 920 ReplaceResultBuilder.prototype.add = function(str) { | 917 str = TO_STRING_INLINE(str); |
| 921 str = TO_STRING_INLINE(str); | 918 if (str.length > 0) this.elements.push(str); |
| 922 if (str.length > 0) this.elements.push(str); | 919 }, |
| 923 } | 920 "addSpecialSlice", function(start, end) { |
| 924 | 921 var len = end - start; |
| 925 | 922 if (start < 0 || len <= 0) return; |
| 926 ReplaceResultBuilder.prototype.addSpecialSlice = function(start, end) { | 923 if (start < 0x80000 && len < 0x800) { |
| 927 var len = end - start; | 924 this.elements.push((start << 11) | len); |
| 928 if (start < 0 || len <= 0) return; | 925 } else { |
| 929 if (start < 0x80000 && len < 0x800) { | 926 // 0 < len <= String::kMaxLength and Smi::kMaxValue >= String::kMaxLength, |
| 930 this.elements.push((start << 11) | len); | 927 // so -len is a smi. |
| 931 } else { | 928 var elements = this.elements; |
| 932 // 0 < len <= String::kMaxLength and Smi::kMaxValue >= String::kMaxLength, | 929 elements.push(-len); |
| 933 // so -len is a smi. | 930 elements.push(start); |
| 931 } |
| 932 }, |
| 933 "generate", function() { |
| 934 var elements = this.elements; | 934 var elements = this.elements; |
| 935 elements.push(-len); | 935 return %StringBuilderConcat(elements, elements.length, this.special_string); |
| 936 elements.push(start); | |
| 937 } | 936 } |
| 938 } | 937 )); |
| 939 | |
| 940 | |
| 941 ReplaceResultBuilder.prototype.generate = function() { | |
| 942 var elements = this.elements; | |
| 943 return %StringBuilderConcat(elements, elements.length, this.special_string); | |
| 944 } | |
| 945 | 938 |
| 946 | 939 |
| 947 // ------------------------------------------------------------------- | 940 // ------------------------------------------------------------------- |
| 948 | 941 |
| 949 function SetupString() { | 942 function SetUpString() { |
| 950 // Setup the constructor property on the String prototype object. | 943 %CheckIsBootstrapping(); |
| 944 // Set up the constructor property on the String prototype object. |
| 951 %SetProperty($String.prototype, "constructor", $String, DONT_ENUM); | 945 %SetProperty($String.prototype, "constructor", $String, DONT_ENUM); |
| 952 | 946 |
| 953 | 947 |
| 954 // Setup the non-enumerable functions on the String object. | 948 // Set up the non-enumerable functions on the String object. |
| 955 InstallFunctions($String, DONT_ENUM, $Array( | 949 InstallFunctions($String, DONT_ENUM, $Array( |
| 956 "fromCharCode", StringFromCharCode | 950 "fromCharCode", StringFromCharCode |
| 957 )); | 951 )); |
| 958 | 952 |
| 959 | 953 |
| 960 // Setup the non-enumerable functions on the String prototype object. | 954 // Set up the non-enumerable functions on the String prototype object. |
| 961 InstallFunctionsOnHiddenPrototype($String.prototype, DONT_ENUM, $Array( | 955 InstallFunctionsOnHiddenPrototype($String.prototype, DONT_ENUM, $Array( |
| 962 "valueOf", StringValueOf, | 956 "valueOf", StringValueOf, |
| 963 "toString", StringToString, | 957 "toString", StringToString, |
| 964 "charAt", StringCharAt, | 958 "charAt", StringCharAt, |
| 965 "charCodeAt", StringCharCodeAt, | 959 "charCodeAt", StringCharCodeAt, |
| 966 "concat", StringConcat, | 960 "concat", StringConcat, |
| 967 "indexOf", StringIndexOf, | 961 "indexOf", StringIndexOf, |
| 968 "lastIndexOf", StringLastIndexOf, | 962 "lastIndexOf", StringLastIndexOf, |
| 969 "localeCompare", StringLocaleCompare, | 963 "localeCompare", StringLocaleCompare, |
| 970 "match", StringMatch, | 964 "match", StringMatch, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 990 "bold", StringBold, | 984 "bold", StringBold, |
| 991 "fixed", StringFixed, | 985 "fixed", StringFixed, |
| 992 "italics", StringItalics, | 986 "italics", StringItalics, |
| 993 "small", StringSmall, | 987 "small", StringSmall, |
| 994 "strike", StringStrike, | 988 "strike", StringStrike, |
| 995 "sub", StringSub, | 989 "sub", StringSub, |
| 996 "sup", StringSup | 990 "sup", StringSup |
| 997 )); | 991 )); |
| 998 } | 992 } |
| 999 | 993 |
| 1000 | 994 SetUpString(); |
| 1001 SetupString(); | |
| OLD | NEW |