OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/jsregexp-inl.h" | 8 #include "src/jsregexp-inl.h" |
9 #include "src/jsregexp.h" | 9 #include "src/jsregexp.h" |
10 #include "src/runtime/runtime.h" | |
11 #include "src/runtime/runtime-utils.h" | 10 #include "src/runtime/runtime-utils.h" |
12 #include "src/runtime/string-builder.h" | 11 #include "src/runtime/string-builder.h" |
13 #include "src/string-search.h" | 12 #include "src/string-search.h" |
14 | 13 |
15 namespace v8 { | 14 namespace v8 { |
16 namespace internal { | 15 namespace internal { |
17 | 16 |
18 | 17 |
| 18 // Perform string match of pattern on subject, starting at start index. |
| 19 // Caller must ensure that 0 <= start_index <= sub->length(), |
| 20 // and should check that pat->length() + start_index <= sub->length(). |
| 21 int StringMatch(Isolate* isolate, Handle<String> sub, Handle<String> pat, |
| 22 int start_index) { |
| 23 DCHECK(0 <= start_index); |
| 24 DCHECK(start_index <= sub->length()); |
| 25 |
| 26 int pattern_length = pat->length(); |
| 27 if (pattern_length == 0) return start_index; |
| 28 |
| 29 int subject_length = sub->length(); |
| 30 if (start_index + pattern_length > subject_length) return -1; |
| 31 |
| 32 sub = String::Flatten(sub); |
| 33 pat = String::Flatten(pat); |
| 34 |
| 35 DisallowHeapAllocation no_gc; // ensure vectors stay valid |
| 36 // Extract flattened substrings of cons strings before getting encoding. |
| 37 String::FlatContent seq_sub = sub->GetFlatContent(); |
| 38 String::FlatContent seq_pat = pat->GetFlatContent(); |
| 39 |
| 40 // dispatch on type of strings |
| 41 if (seq_pat.IsOneByte()) { |
| 42 Vector<const uint8_t> pat_vector = seq_pat.ToOneByteVector(); |
| 43 if (seq_sub.IsOneByte()) { |
| 44 return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector, |
| 45 start_index); |
| 46 } |
| 47 return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, |
| 48 start_index); |
| 49 } |
| 50 Vector<const uc16> pat_vector = seq_pat.ToUC16Vector(); |
| 51 if (seq_sub.IsOneByte()) { |
| 52 return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector, |
| 53 start_index); |
| 54 } |
| 55 return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, start_index); |
| 56 } |
| 57 |
| 58 |
19 // This may return an empty MaybeHandle if an exception is thrown or | 59 // This may return an empty MaybeHandle if an exception is thrown or |
20 // we abort due to reaching the recursion limit. | 60 // we abort due to reaching the recursion limit. |
21 MaybeHandle<String> StringReplaceOneCharWithString( | 61 MaybeHandle<String> StringReplaceOneCharWithString( |
22 Isolate* isolate, Handle<String> subject, Handle<String> search, | 62 Isolate* isolate, Handle<String> subject, Handle<String> search, |
23 Handle<String> replace, bool* found, int recursion_limit) { | 63 Handle<String> replace, bool* found, int recursion_limit) { |
24 StackLimitCheck stackLimitCheck(isolate); | 64 StackLimitCheck stackLimitCheck(isolate); |
25 if (stackLimitCheck.HasOverflowed() || (recursion_limit == 0)) { | 65 if (stackLimitCheck.HasOverflowed() || (recursion_limit == 0)) { |
26 return MaybeHandle<String>(); | 66 return MaybeHandle<String>(); |
27 } | 67 } |
28 recursion_limit--; | 68 recursion_limit--; |
(...skipping 11 matching lines...) Expand all Loading... |
40 Handle<String> new_second; | 80 Handle<String> new_second; |
41 if (!StringReplaceOneCharWithString(isolate, second, search, replace, found, | 81 if (!StringReplaceOneCharWithString(isolate, second, search, replace, found, |
42 recursion_limit) | 82 recursion_limit) |
43 .ToHandle(&new_second)) { | 83 .ToHandle(&new_second)) { |
44 return MaybeHandle<String>(); | 84 return MaybeHandle<String>(); |
45 } | 85 } |
46 if (*found) return isolate->factory()->NewConsString(first, new_second); | 86 if (*found) return isolate->factory()->NewConsString(first, new_second); |
47 | 87 |
48 return subject; | 88 return subject; |
49 } else { | 89 } else { |
50 int index = Runtime::StringMatch(isolate, subject, search, 0); | 90 int index = StringMatch(isolate, subject, search, 0); |
51 if (index == -1) return subject; | 91 if (index == -1) return subject; |
52 *found = true; | 92 *found = true; |
53 Handle<String> first = isolate->factory()->NewSubString(subject, 0, index); | 93 Handle<String> first = isolate->factory()->NewSubString(subject, 0, index); |
54 Handle<String> cons1; | 94 Handle<String> cons1; |
55 ASSIGN_RETURN_ON_EXCEPTION( | 95 ASSIGN_RETURN_ON_EXCEPTION( |
56 isolate, cons1, isolate->factory()->NewConsString(first, replace), | 96 isolate, cons1, isolate->factory()->NewConsString(first, replace), |
57 String); | 97 String); |
58 Handle<String> second = | 98 Handle<String> second = |
59 isolate->factory()->NewSubString(subject, index + 1, subject->length()); | 99 isolate->factory()->NewSubString(subject, index + 1, subject->length()); |
60 return isolate->factory()->NewConsString(cons1, second); | 100 return isolate->factory()->NewConsString(cons1, second); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 DCHECK(args.length() == 3); | 134 DCHECK(args.length() == 3); |
95 | 135 |
96 CONVERT_ARG_HANDLE_CHECKED(String, sub, 0); | 136 CONVERT_ARG_HANDLE_CHECKED(String, sub, 0); |
97 CONVERT_ARG_HANDLE_CHECKED(String, pat, 1); | 137 CONVERT_ARG_HANDLE_CHECKED(String, pat, 1); |
98 CONVERT_ARG_HANDLE_CHECKED(Object, index, 2); | 138 CONVERT_ARG_HANDLE_CHECKED(Object, index, 2); |
99 | 139 |
100 uint32_t start_index; | 140 uint32_t start_index; |
101 if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); | 141 if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
102 | 142 |
103 RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); | 143 RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); |
104 int position = Runtime::StringMatch(isolate, sub, pat, start_index); | 144 int position = StringMatch(isolate, sub, pat, start_index); |
105 return Smi::FromInt(position); | 145 return Smi::FromInt(position); |
106 } | 146 } |
107 | 147 |
108 | 148 |
109 template <typename schar, typename pchar> | 149 template <typename schar, typename pchar> |
110 static int StringMatchBackwards(Vector<const schar> subject, | 150 static int StringMatchBackwards(Vector<const schar> subject, |
111 Vector<const pchar> pattern, int idx) { | 151 Vector<const pchar> pattern, int idx) { |
112 int pattern_length = pattern.length(); | 152 int pattern_length = pattern.length(); |
113 DCHECK(pattern_length >= 1); | 153 DCHECK(pattern_length >= 1); |
114 DCHECK(idx + pattern_length <= subject.length()); | 154 DCHECK(idx + pattern_length <= subject.length()); |
(...skipping 1142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1257 return __RT_impl_Runtime_StringAdd(args, isolate); | 1297 return __RT_impl_Runtime_StringAdd(args, isolate); |
1258 } | 1298 } |
1259 | 1299 |
1260 | 1300 |
1261 RUNTIME_FUNCTION(RuntimeReference_IsStringWrapperSafeForDefaultValueOf) { | 1301 RUNTIME_FUNCTION(RuntimeReference_IsStringWrapperSafeForDefaultValueOf) { |
1262 UNIMPLEMENTED(); | 1302 UNIMPLEMENTED(); |
1263 return NULL; | 1303 return NULL; |
1264 } | 1304 } |
1265 } | 1305 } |
1266 } // namespace v8::internal | 1306 } // namespace v8::internal |
OLD | NEW |