OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 2573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2584 int data; | 2584 int data; |
2585 }; | 2585 }; |
2586 | 2586 |
2587 template<typename Char> | 2587 template<typename Char> |
2588 static bool ParseReplacementPattern(ZoneList<ReplacementPart>* parts, | 2588 static bool ParseReplacementPattern(ZoneList<ReplacementPart>* parts, |
2589 Vector<Char> characters, | 2589 Vector<Char> characters, |
2590 int capture_count, | 2590 int capture_count, |
2591 int subject_length) { | 2591 int subject_length) { |
2592 int length = characters.length(); | 2592 int length = characters.length(); |
2593 int last = 0; | 2593 int last = 0; |
2594 bool simple = true; | |
2595 for (int i = 0; i < length; i++) { | 2594 for (int i = 0; i < length; i++) { |
2596 Char c = characters[i]; | 2595 Char c = characters[i]; |
2597 if (c == '$') { | 2596 if (c == '$') { |
2598 simple = false; | |
2599 int next_index = i + 1; | 2597 int next_index = i + 1; |
2600 if (next_index == length) { // No next character! | 2598 if (next_index == length) { // No next character! |
2601 break; | 2599 break; |
2602 } | 2600 } |
2603 Char c2 = characters[next_index]; | 2601 Char c2 = characters[next_index]; |
2604 switch (c2) { | 2602 switch (c2) { |
2605 case '$': | 2603 case '$': |
2606 if (i > last) { | 2604 if (i > last) { |
2607 // There is a substring before. Include the first "$". | 2605 // There is a substring before. Include the first "$". |
2608 parts->Add(ReplacementPart::ReplacementSubString(last, next_index)); | 2606 parts->Add(ReplacementPart::ReplacementSubString(last, next_index)); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2677 } | 2675 } |
2678 default: | 2676 default: |
2679 i = next_index; | 2677 i = next_index; |
2680 break; | 2678 break; |
2681 } | 2679 } |
2682 } | 2680 } |
2683 } | 2681 } |
2684 if (length > last) { | 2682 if (length > last) { |
2685 if (last == 0) { | 2683 if (last == 0) { |
2686 parts->Add(ReplacementPart::ReplacementString()); | 2684 parts->Add(ReplacementPart::ReplacementString()); |
| 2685 return true; |
2687 } else { | 2686 } else { |
2688 parts->Add(ReplacementPart::ReplacementSubString(last, length)); | 2687 parts->Add(ReplacementPart::ReplacementSubString(last, length)); |
2689 } | 2688 } |
2690 } | 2689 } |
2691 return simple; | 2690 return false; |
2692 } | 2691 } |
2693 | 2692 |
2694 ZoneList<ReplacementPart> parts_; | 2693 ZoneList<ReplacementPart> parts_; |
2695 ZoneList<Handle<String> > replacement_substrings_; | 2694 ZoneList<Handle<String> > replacement_substrings_; |
2696 bool simple_hint_; | 2695 bool simple_hint_; |
2697 }; | 2696 }; |
2698 | 2697 |
2699 | 2698 |
2700 void CompiledReplacement::Compile(Handle<String> replacement, | 2699 void CompiledReplacement::Compile(Handle<String> replacement, |
2701 int capture_count, | 2700 int capture_count, |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2872 } | 2871 } |
2873 } | 2872 } |
2874 } | 2873 } |
2875 | 2874 |
2876 | 2875 |
2877 template<typename ResultSeqString> | 2876 template<typename ResultSeqString> |
2878 MUST_USE_RESULT static MaybeObject* StringReplaceStringWithString( | 2877 MUST_USE_RESULT static MaybeObject* StringReplaceStringWithString( |
2879 Isolate* isolate, | 2878 Isolate* isolate, |
2880 Handle<String> subject, | 2879 Handle<String> subject, |
2881 Handle<JSRegExp> pattern_regexp, | 2880 Handle<JSRegExp> pattern_regexp, |
2882 Handle<String> replacement = Handle<String>::null()) { | 2881 Handle<String> replacement) { |
2883 ASSERT(subject->IsFlat()); | 2882 ASSERT(subject->IsFlat()); |
2884 ASSERT(replacement.is_null() || replacement->IsFlat()); | 2883 ASSERT(replacement->IsFlat()); |
2885 | 2884 |
2886 ZoneScope zone_space(isolate, DELETE_ON_EXIT); | 2885 ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
2887 ZoneList<int> indices(8); | 2886 ZoneList<int> indices(8); |
| 2887 ASSERT_EQ(JSRegExp::ATOM, pattern_regexp->TypeTag()); |
2888 String* pattern = | 2888 String* pattern = |
2889 String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex)); | 2889 String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex)); |
2890 int subject_len = subject->length(); | 2890 int subject_len = subject->length(); |
2891 int pattern_len = pattern->length(); | 2891 int pattern_len = pattern->length(); |
2892 int replacement_len = (replacement.is_null()) ? 0 : replacement->length(); | 2892 int replacement_len = replacement->length(); |
2893 | 2893 |
2894 FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff); | 2894 FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff); |
2895 | 2895 |
2896 int matches = indices.length(); | 2896 int matches = indices.length(); |
2897 if (matches == 0) return *subject; | 2897 if (matches == 0) return *subject; |
2898 | 2898 |
2899 int result_len = (replacement_len - pattern_len) * matches + subject_len; | 2899 int result_len = (replacement_len - pattern_len) * matches + subject_len; |
2900 int subject_pos = 0; | 2900 int subject_pos = 0; |
2901 int result_pos = 0; | 2901 int result_pos = 0; |
2902 | 2902 |
2903 Handle<ResultSeqString> result; | 2903 Handle<ResultSeqString> result; |
2904 if (ResultSeqString::kHasAsciiEncoding) { | 2904 if (ResultSeqString::kHasAsciiEncoding) { |
2905 result = Handle<ResultSeqString>::cast( | 2905 result = Handle<ResultSeqString>::cast( |
2906 isolate->factory()->NewRawAsciiString(result_len)); | 2906 isolate->factory()->NewRawAsciiString(result_len)); |
2907 } else { | 2907 } else { |
2908 result = Handle<ResultSeqString>::cast( | 2908 result = Handle<ResultSeqString>::cast( |
2909 isolate->factory()->NewRawTwoByteString(result_len)); | 2909 isolate->factory()->NewRawTwoByteString(result_len)); |
2910 } | 2910 } |
2911 | 2911 |
2912 for (int i = 0; i < matches; i++) { | 2912 for (int i = 0; i < matches; i++) { |
2913 // Copy non-matched subject content. | 2913 // Copy non-matched subject content. |
2914 String::WriteToFlat(*subject, | 2914 if (subject_pos < indices.at(i)) { |
2915 result->GetChars() + result_pos, | 2915 String::WriteToFlat(*subject, |
2916 subject_pos, | 2916 result->GetChars() + result_pos, |
2917 indices.at(i)); | 2917 subject_pos, |
2918 result_pos += indices.at(i) - subject_pos; | 2918 indices.at(i)); |
| 2919 result_pos += indices.at(i) - subject_pos; |
| 2920 } |
| 2921 |
2919 // Replace match. | 2922 // Replace match. |
2920 | |
2921 if (replacement_len > 0) { | 2923 if (replacement_len > 0) { |
2922 String::WriteToFlat(*replacement, | 2924 String::WriteToFlat(*replacement, |
2923 result->GetChars() + result_pos, | 2925 result->GetChars() + result_pos, |
2924 0, | 2926 0, |
2925 replacement_len); | 2927 replacement_len); |
2926 result_pos += replacement_len; | 2928 result_pos += replacement_len; |
2927 } | 2929 } |
2928 | 2930 |
2929 subject_pos = indices.at(i) + pattern_len; | 2931 subject_pos = indices.at(i) + pattern_len; |
2930 } | 2932 } |
2931 String::WriteToFlat(*subject, | 2933 // Add remaining subject content at the end. |
2932 result->GetChars() + result_pos, | 2934 if (subject_pos < subject_len) { |
2933 subject_pos, | 2935 String::WriteToFlat(*subject, |
2934 subject_len); | 2936 result->GetChars() + result_pos, |
| 2937 subject_pos, |
| 2938 subject_len); |
| 2939 } |
2935 return *result; | 2940 return *result; |
2936 } | 2941 } |
2937 | 2942 |
2938 | 2943 |
2939 MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithString( | 2944 MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithString( |
2940 Isolate* isolate, | 2945 Isolate* isolate, |
2941 String* subject, | 2946 String* subject, |
2942 JSRegExp* regexp, | 2947 JSRegExp* regexp, |
2943 String* replacement, | 2948 String* replacement, |
2944 JSArray* last_match_info) { | 2949 JSArray* last_match_info) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3070 ASSERT(subject->IsFlat()); | 3075 ASSERT(subject->IsFlat()); |
3071 | 3076 |
3072 HandleScope handles(isolate); | 3077 HandleScope handles(isolate); |
3073 | 3078 |
3074 Handle<String> subject_handle(subject); | 3079 Handle<String> subject_handle(subject); |
3075 Handle<JSRegExp> regexp_handle(regexp); | 3080 Handle<JSRegExp> regexp_handle(regexp); |
3076 | 3081 |
3077 // Shortcut for simple non-regexp global replacements | 3082 // Shortcut for simple non-regexp global replacements |
3078 if (regexp_handle->GetFlags().is_global() && | 3083 if (regexp_handle->GetFlags().is_global() && |
3079 regexp_handle->TypeTag() == JSRegExp::ATOM) { | 3084 regexp_handle->TypeTag() == JSRegExp::ATOM) { |
| 3085 Handle<String> empty_string_handle(HEAP->empty_string()); |
3080 if (subject_handle->HasOnlyAsciiChars()) { | 3086 if (subject_handle->HasOnlyAsciiChars()) { |
3081 return StringReplaceStringWithString<SeqAsciiString>( | 3087 return StringReplaceStringWithString<SeqAsciiString>( |
3082 isolate, subject_handle, regexp_handle); | 3088 isolate, subject_handle, regexp_handle, empty_string_handle); |
3083 } else { | 3089 } else { |
3084 return StringReplaceStringWithString<SeqTwoByteString>( | 3090 return StringReplaceStringWithString<SeqTwoByteString>( |
3085 isolate, subject_handle, regexp_handle); | 3091 isolate, subject_handle, regexp_handle, empty_string_handle); |
3086 } | 3092 } |
3087 } | 3093 } |
3088 | 3094 |
3089 Handle<JSArray> last_match_info_handle(last_match_info); | 3095 Handle<JSArray> last_match_info_handle(last_match_info); |
3090 Handle<Object> match = RegExpImpl::Exec(regexp_handle, | 3096 Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
3091 subject_handle, | 3097 subject_handle, |
3092 0, | 3098 0, |
3093 last_match_info_handle); | 3099 last_match_info_handle); |
3094 if (match.is_null()) return Failure::Exception(); | 3100 if (match.is_null()) return Failure::Exception(); |
3095 if (match->IsNull()) return *subject_handle; | 3101 if (match->IsNull()) return *subject_handle; |
(...skipping 10032 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13128 } else { | 13134 } else { |
13129 // Handle last resort GC and make sure to allow future allocations | 13135 // Handle last resort GC and make sure to allow future allocations |
13130 // to grow the heap without causing GCs (if possible). | 13136 // to grow the heap without causing GCs (if possible). |
13131 isolate->counters()->gc_last_resort_from_js()->Increment(); | 13137 isolate->counters()->gc_last_resort_from_js()->Increment(); |
13132 isolate->heap()->CollectAllGarbage(false); | 13138 isolate->heap()->CollectAllGarbage(false); |
13133 } | 13139 } |
13134 } | 13140 } |
13135 | 13141 |
13136 | 13142 |
13137 } } // namespace v8::internal | 13143 } } // namespace v8::internal |
OLD | NEW |