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

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

Issue 864463002: Create string efficiently from Uint16List/View. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add tests for external typed-data Created 5 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
« no previous file with comments | « no previous file | runtime/lib/string_patch.dart » ('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 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 OneByteString::SetCharAt(receiver, index_obj.Value(), code_point_obj.Value()); 400 OneByteString::SetCharAt(receiver, index_obj.Value(), code_point_obj.Value());
401 return Object::null(); 401 return Object::null();
402 } 402 }
403 403
404 404
405 DEFINE_NATIVE_ENTRY(ExternalOneByteString_getCid, 0) { 405 DEFINE_NATIVE_ENTRY(ExternalOneByteString_getCid, 0) {
406 return Smi::New(kExternalOneByteStringCid); 406 return Smi::New(kExternalOneByteStringCid);
407 } 407 }
408 408
409 409
410 DEFINE_NATIVE_ENTRY(TwoByteString_allocateFromTwoByteList, 3) {
411 Instance& list = Instance::CheckedHandle(arguments->NativeArgAt(0));
412 GET_NON_NULL_NATIVE_ARGUMENT(Smi, start_obj, arguments->NativeArgAt(1));
413 GET_NON_NULL_NATIVE_ARGUMENT(Smi, end_obj, arguments->NativeArgAt(2));
414
415 intptr_t start = start_obj.Value();
416 intptr_t end = end_obj.Value();
417 if (start < 0) {
418 Exceptions::ThrowArgumentError(start_obj);
419 }
420 intptr_t length = end - start;
421 if (length < 0) {
422 Exceptions::ThrowArgumentError(end_obj);
423 }
424
425 Heap::Space space = isolate->heap()->SpaceForAllocation(kTwoByteStringCid);
426 if (list.IsTypedData()) {
427 const TypedData& array = TypedData::Cast(list);
428 if (array.ElementType() != kUint16ArrayElement) {
429 Exceptions::ThrowArgumentError(list);
430 }
431 if (end > array.Length()) {
432 Exceptions::ThrowArgumentError(end_obj);
433 }
434 return TwoByteString::New(array, start * sizeof(uint16_t), length, space);
435 } else if (list.IsExternalTypedData()) {
436 const ExternalTypedData& array = ExternalTypedData::Cast(list);
437 if (array.ElementType() != kUint16ArrayElement) {
438 Exceptions::ThrowArgumentError(list);
439 }
440 if (end > array.Length()) {
441 Exceptions::ThrowArgumentError(end_obj);
442 }
443 return TwoByteString::New(array, start * sizeof(uint16_t), length, space);
444 } else if (RawObject::IsTypedDataViewClassId(list.GetClassId())) {
445 const intptr_t cid = list.GetClassId();
446 if (cid != kTypedDataUint16ArrayViewCid) {
447 Exceptions::ThrowArgumentError(list);
448 }
449 if (end > Smi::Value(TypedDataView::Length(list))) {
450 Exceptions::ThrowArgumentError(end_obj);
451 }
452 const Instance& data_obj = Instance::Handle(isolate,
453 TypedDataView::Data(list));
454 intptr_t data_offset = Smi::Value(TypedDataView::OffsetInBytes(list));
455 if (data_obj.IsTypedData()) {
456 const TypedData& array = TypedData::Cast(data_obj);
457 return TwoByteString::New(array, data_offset + start * sizeof(uint16_t),
458 length, space);
459 } else if (data_obj.IsExternalTypedData()) {
460 const ExternalTypedData& array = ExternalTypedData::Cast(data_obj);
461 return TwoByteString::New(array, data_offset + start * sizeof(uint16_t),
462 length, space);
463 }
464 } else if (list.IsArray()) {
465 const Array& array = Array::Cast(list);
466 if (end > array.Length()) {
467 Exceptions::ThrowArgumentError(end_obj);
468 }
469 const String& string = String::Handle(isolate,
470 TwoByteString::New(length, space));
471 for (int i = 0; i < length; i++) {
472 intptr_t value =
473 Smi::Value(reinterpret_cast<RawSmi*>(array.At(start + i)));
474 TwoByteString::SetCharAt(string, i, value);
475 }
476 return string.raw();
477 } else if (list.IsGrowableObjectArray()) {
478 const GrowableObjectArray& array = GrowableObjectArray::Cast(list);
479 if (end > array.Length()) {
480 Exceptions::ThrowArgumentError(end_obj);
481 }
482 const String& string = String::Handle(isolate,
483 TwoByteString::New(length, space));
484 for (int i = 0; i < length; i++) {
485 intptr_t value =
486 Smi::Value(reinterpret_cast<RawSmi*>(array.At(start + i)));
487 TwoByteString::SetCharAt(string, i, value);
488 }
489 return string.raw();
490 }
491 UNREACHABLE();
492 return Object::null();
493 }
494
495
410 DEFINE_NATIVE_ENTRY(String_getHashCode, 1) { 496 DEFINE_NATIVE_ENTRY(String_getHashCode, 1) {
411 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 497 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
412 intptr_t hash_val = receiver.Hash(); 498 intptr_t hash_val = receiver.Hash();
413 ASSERT(hash_val > 0); 499 ASSERT(hash_val > 0);
414 ASSERT(Smi::IsValid(hash_val)); 500 ASSERT(Smi::IsValid(hash_val));
415 return Smi::New(hash_val); 501 return Smi::New(hash_val);
416 } 502 }
417 503
418 504
419 DEFINE_NATIVE_ENTRY(String_getLength, 1) { 505 DEFINE_NATIVE_ENTRY(String_getLength, 1) {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 ? String::Handle(OneByteString::New(length_value, Heap::kNew)) 609 ? String::Handle(OneByteString::New(length_value, Heap::kNew))
524 : String::Handle(TwoByteString::New(length_value, Heap::kNew)); 610 : String::Handle(TwoByteString::New(length_value, Heap::kNew));
525 NoGCScope no_gc; 611 NoGCScope no_gc;
526 612
527 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0)); 613 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0));
528 String::Copy(result, 0, data_position, length_value); 614 String::Copy(result, 0, data_position, length_value);
529 return result.raw(); 615 return result.raw();
530 } 616 }
531 617
532 } // namespace dart 618 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/string_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698