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

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

Issue 169893003: Another round of cleanups for http://www.dartbug.com/15922 (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 10 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
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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 int32_t* utf32_array = zone->Alloc<int32_t>(array_len); 66 int32_t* utf32_array = zone->Alloc<int32_t>(array_len);
67 Instance& index_object = Instance::Handle(isolate); 67 Instance& index_object = Instance::Handle(isolate);
68 for (intptr_t i = 0; i < array_len; i++) { 68 for (intptr_t i = 0; i < array_len; i++) {
69 index_object ^= a.At(i); 69 index_object ^= a.At(i);
70 if (!index_object.IsSmi()) { 70 if (!index_object.IsSmi()) {
71 Exceptions::ThrowArgumentError(index_object); 71 Exceptions::ThrowArgumentError(index_object);
72 } 72 }
73 intptr_t value = Smi::Cast(index_object).Value(); 73 intptr_t value = Smi::Cast(index_object).Value();
74 if (Utf::IsOutOfRange(value)) { 74 if (Utf::IsOutOfRange(value)) {
75 Exceptions::ThrowByType(Exceptions::kArgument, Object::empty_array()); 75 Exceptions::ThrowByType(Exceptions::kArgument, Object::empty_array());
76 } else { 76 UNREACHABLE();
77 if (!Utf::IsLatin1(value)) { 77 }
78 is_one_byte_string = false; 78 // Now it is safe to cast the value.
79 if (Utf::IsSupplementary(value)) { 79 int32_t value32 = static_cast<int32_t>(value);
80 utf16_len += 1; 80 if (!Utf::IsLatin1(value32)) {
81 } 81 is_one_byte_string = false;
82 if (Utf::IsSupplementary(value32)) {
83 utf16_len += 1;
82 } 84 }
83 } 85 }
84 utf32_array[i] = value; 86 utf32_array[i] = value32;
85 } 87 }
86 if (is_one_byte_string) { 88 if (is_one_byte_string) {
87 return OneByteString::New(utf32_array, array_len, Heap::kNew); 89 return OneByteString::New(utf32_array, array_len, Heap::kNew);
88 } 90 }
89 return TwoByteString::New(utf16_len, utf32_array, array_len, Heap::kNew); 91 return TwoByteString::New(utf16_len, utf32_array, array_len, Heap::kNew);
90 } 92 }
91 93
92 94
93 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) { 95 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) {
94 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 96 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 232
231 DEFINE_NATIVE_ENTRY(String_getLength, 1) { 233 DEFINE_NATIVE_ENTRY(String_getLength, 1) {
232 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 234 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
233 return Smi::New(receiver.Length()); 235 return Smi::New(receiver.Length());
234 } 236 }
235 237
236 238
237 static int32_t StringValueAt(const String& str, const Integer& index) { 239 static int32_t StringValueAt(const String& str, const Integer& index) {
238 if (index.IsSmi()) { 240 if (index.IsSmi()) {
239 const Smi& smi = Smi::Cast(index); 241 const Smi& smi = Smi::Cast(index);
240 int32_t index = smi.Value(); 242 intptr_t index = smi.Value();
241 if ((index < 0) || (index >= str.Length())) { 243 if ((index < 0) || (index >= str.Length())) {
242 const Array& args = Array::Handle(Array::New(1)); 244 const Array& args = Array::Handle(Array::New(1));
243 args.SetAt(0, smi); 245 args.SetAt(0, smi);
244 Exceptions::ThrowByType(Exceptions::kRange, args); 246 Exceptions::ThrowByType(Exceptions::kRange, args);
245 } 247 }
246 return str.CharAt(index); 248 return str.CharAt(index);
247 } else { 249 } else {
248 // An index larger than Smi is always illegal. 250 // An index larger than Smi is always illegal.
249 const Array& args = Array::Handle(Array::New(1)); 251 const Array& args = Array::Handle(Array::New(1));
250 args.SetAt(0, index); 252 args.SetAt(0, index);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 ? String::Handle(OneByteString::New(length_value, Heap::kNew)) 348 ? String::Handle(OneByteString::New(length_value, Heap::kNew))
347 : String::Handle(TwoByteString::New(length_value, Heap::kNew)); 349 : String::Handle(TwoByteString::New(length_value, Heap::kNew));
348 NoGCScope no_gc; 350 NoGCScope no_gc;
349 351
350 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0)); 352 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0));
351 String::Copy(result, 0, data_position, length_value); 353 String::Copy(result, 0, data_position, length_value);
352 return result.raw(); 354 return result.raw();
353 } 355 }
354 356
355 } // namespace dart 357 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698