Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: runtime/lib/string.cc

Issue 560113002: Narrow String::CharAt from int32_t to uint16_t. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/vm/dart_api_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 4
5 #include "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "vm/exceptions.h" 8 #include "vm/exceptions.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/isolate.h" 10 #include "vm/isolate.h"
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 return Smi::New(hash_val); 211 return Smi::New(hash_val);
212 } 212 }
213 213
214 214
215 DEFINE_NATIVE_ENTRY(String_getLength, 1) { 215 DEFINE_NATIVE_ENTRY(String_getLength, 1) {
216 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 216 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
217 return Smi::New(receiver.Length()); 217 return Smi::New(receiver.Length());
218 } 218 }
219 219
220 220
221 static int32_t StringValueAt(const String& str, const Integer& index) { 221 static uint16_t StringValueAt(const String& str, const Integer& index) {
222 if (index.IsSmi()) { 222 if (index.IsSmi()) {
223 const intptr_t index_value = Smi::Cast(index).Value(); 223 const intptr_t index_value = Smi::Cast(index).Value();
224 if ((0 <= index_value) && (index_value < str.Length())) { 224 if ((0 <= index_value) && (index_value < str.Length())) {
225 return str.CharAt(index_value); 225 return str.CharAt(index_value);
226 } 226 }
227 } 227 }
228 228
229 // An index larger than Smi is always illegal. 229 // An index larger than Smi is always illegal.
230 Exceptions::ThrowRangeError("index", index, 0, str.Length()); 230 Exceptions::ThrowRangeError("index", index, 0, str.Length());
231 return 0; 231 return 0;
232 } 232 }
233 233
234 234
235 DEFINE_NATIVE_ENTRY(String_charAt, 2) { 235 DEFINE_NATIVE_ENTRY(String_charAt, 2) {
236 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 236 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
237 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); 237 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1));
238 uint32_t value = StringValueAt(receiver, index); 238 uint16_t value = StringValueAt(receiver, index);
239 ASSERT(value <= 0x10FFFF); 239 return Symbols::FromCharCode(static_cast<int32_t>(value));
240 return Symbols::FromCharCode(value);
241 } 240 }
242 241
243 242
243 // Returns the 16-bit UTF-16 code unit at the given index.
244 DEFINE_NATIVE_ENTRY(String_codeUnitAt, 2) { 244 DEFINE_NATIVE_ENTRY(String_codeUnitAt, 2) {
245 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 245 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
246 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); 246 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1));
247 247 uint16_t value = StringValueAt(receiver, index);
248 int32_t value = StringValueAt(receiver, index); 248 return Smi::New(static_cast<intptr_t>(value));
249 ASSERT(value >= 0);
250 ASSERT(value <= 0xFFFF);
251 return Smi::New(value);
252 } 249 }
253 250
254 251
255 DEFINE_NATIVE_ENTRY(String_concat, 2) { 252 DEFINE_NATIVE_ENTRY(String_concat, 2) {
256 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 253 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
257 GET_NON_NULL_NATIVE_ARGUMENT(String, b, arguments->NativeArgAt(1)); 254 GET_NON_NULL_NATIVE_ARGUMENT(String, b, arguments->NativeArgAt(1));
258 return String::Concat(receiver, b); 255 return String::Concat(receiver, b);
259 } 256 }
260 257
261 258
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 ? String::Handle(OneByteString::New(length_value, Heap::kNew)) 319 ? String::Handle(OneByteString::New(length_value, Heap::kNew))
323 : String::Handle(TwoByteString::New(length_value, Heap::kNew)); 320 : String::Handle(TwoByteString::New(length_value, Heap::kNew));
324 NoGCScope no_gc; 321 NoGCScope no_gc;
325 322
326 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0)); 323 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0));
327 String::Copy(result, 0, data_position, length_value); 324 String::Copy(result, 0, data_position, length_value);
328 return result.raw(); 325 return result.raw();
329 } 326 }
330 327
331 } // namespace dart 328 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/dart_api_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698