OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // This file encapsulates all the interaction with the | 4 // This file encapsulates all the interaction with the |
5 // JSC regular expression library also referred to as pcre | 5 // JSC regular expression library also referred to as pcre |
6 | 6 |
7 #include "lib/regexp_jsc.h" | 7 #include "lib/regexp_jsc.h" |
8 | 8 |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 #include "vm/allocation.h" | 10 #include "vm/allocation.h" |
11 #include "vm/exceptions.h" | 11 #include "vm/exceptions.h" |
12 #include "vm/globals.h" | 12 #include "vm/globals.h" |
13 #include "vm/isolate.h" | 13 #include "vm/isolate.h" |
14 #include "third_party/jscre/pcre.h" | 14 #include "third_party/jscre/pcre.h" |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
17 | 17 |
18 static uint16_t* GetTwoByteData(const String& str) { | 18 static uint16_t* GetTwoByteData(const String& str) { |
19 intptr_t size = str.Length() * sizeof(uint16_t); | |
20 Zone* zone = Isolate::Current()->current_zone(); | 19 Zone* zone = Isolate::Current()->current_zone(); |
21 uint16_t* two_byte_str = reinterpret_cast<uint16_t*>(zone->Allocate(size)); | 20 uint16_t* two_byte_str = zone->Alloc<uint16_t>(str.Length()); |
22 for (intptr_t i = 0; i < str.Length(); i++) { | 21 for (intptr_t i = 0; i < str.Length(); i++) { |
23 two_byte_str[i] = str.CharAt(i); | 22 two_byte_str[i] = str.CharAt(i); |
24 } | 23 } |
25 return two_byte_str; | 24 return two_byte_str; |
26 } | 25 } |
27 | 26 |
28 | 27 |
29 static void* JSREMalloc(size_t size) { | 28 static void* JSREMalloc(size_t size) { |
30 intptr_t regexp_size = static_cast<intptr_t>(size); | 29 intptr_t regexp_size = static_cast<intptr_t>(size); |
31 ASSERT(regexp_size > 0); | 30 ASSERT(regexp_size > 0); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 const Smi& num_bracket_exprs = Smi::Handle(regex.num_bracket_expressions()); | 118 const Smi& num_bracket_exprs = Smi::Handle(regex.num_bracket_expressions()); |
120 intptr_t num_bracket_expressions = num_bracket_exprs.Value(); | 119 intptr_t num_bracket_expressions = num_bracket_exprs.Value(); |
121 Zone* zone = Isolate::Current()->current_zone(); | 120 Zone* zone = Isolate::Current()->current_zone(); |
122 // The jscre library rounds the passed in size to a multiple of 3 in order | 121 // The jscre library rounds the passed in size to a multiple of 3 in order |
123 // to reuse the passed in offsets array as a temporary chunk of working | 122 // to reuse the passed in offsets array as a temporary chunk of working |
124 // storage during matching, so we just pass in a size which is a multiple | 123 // storage during matching, so we just pass in a size which is a multiple |
125 // of 3. | 124 // of 3. |
126 const int kJscreMultiple = 3; | 125 const int kJscreMultiple = 3; |
127 int offsets_length = (num_bracket_expressions + 1) * kJscreMultiple; | 126 int offsets_length = (num_bracket_expressions + 1) * kJscreMultiple; |
128 int* offsets = NULL; | 127 int* offsets = NULL; |
129 int offsets_array_size = offsets_length * sizeof(offsets[0]); | 128 offsets = zone->Alloc<int>(offsets_length); |
130 offsets = reinterpret_cast<int*>(zone->Allocate(offsets_array_size)); | |
131 int retval = jscre::jsRegExpExecute(jscregexp, | 129 int retval = jscre::jsRegExpExecute(jscregexp, |
132 two_byte_str, | 130 two_byte_str, |
133 str.Length(), | 131 str.Length(), |
134 start_index, | 132 start_index, |
135 offsets, | 133 offsets, |
136 offsets_length); | 134 offsets_length); |
137 | 135 |
138 // The KJS JavaScript engine returns null (ie, a failed match) when | 136 // The KJS JavaScript engine returns null (ie, a failed match) when |
139 // JSRE's internal match limit is exceeded. We duplicate that behavior here. | 137 // JSRE's internal match limit is exceeded. We duplicate that behavior here. |
140 if (retval == jscre::JSRegExpErrorNoMatch | 138 if (retval == jscre::JSRegExpErrorNoMatch |
(...skipping 24 matching lines...) Expand all Loading... |
165 i += kMatchPair) { | 163 i += kMatchPair) { |
166 start = Smi::New(offsets[i]); | 164 start = Smi::New(offsets[i]); |
167 end = Smi::New(offsets[i + 1]); | 165 end = Smi::New(offsets[i + 1]); |
168 array.SetAt(i, start); | 166 array.SetAt(i, start); |
169 array.SetAt(i+1, end); | 167 array.SetAt(i+1, end); |
170 } | 168 } |
171 return array.raw(); | 169 return array.raw(); |
172 } | 170 } |
173 | 171 |
174 } // namespace dart | 172 } // namespace dart |
OLD | NEW |