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

Side by Side Diff: vm/object.h

Issue 11275008: - Represent strings internally in UTF-16 format, this makes it (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 1 month 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) 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 4
5 #ifndef VM_OBJECT_H_ 5 #ifndef VM_OBJECT_H_
6 #define VM_OBJECT_H_ 6 #define VM_OBJECT_H_
7 7
8 #include "include/dart_api.h" 8 #include "include/dart_api.h"
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "platform/utils.h" 10 #include "platform/utils.h"
(...skipping 3594 matching lines...) Expand 10 before | Expand all | Expand 10 after
3605 3605
3606 // String may not be '\0' terminated. 3606 // String may not be '\0' terminated.
3607 class String : public Instance { 3607 class String : public Instance {
3608 public: 3608 public:
3609 // We use 30 bits for the hash code so that we consistently use a 3609 // We use 30 bits for the hash code so that we consistently use a
3610 // 32bit Smi representation for the hash code on all architectures. 3610 // 32bit Smi representation for the hash code on all architectures.
3611 static const intptr_t kHashBits = 30; 3611 static const intptr_t kHashBits = 30;
3612 3612
3613 static const intptr_t kOneByteChar = 1; 3613 static const intptr_t kOneByteChar = 1;
3614 static const intptr_t kTwoByteChar = 2; 3614 static const intptr_t kTwoByteChar = 2;
3615 static const intptr_t kFourByteChar = 4;
3616 3615
3617 // All strings share the same maximum element count to keep things 3616 // All strings share the same maximum element count to keep things
3618 // simple. We choose a value that will prevent integer overflow for 3617 // simple. We choose a value that will prevent integer overflow for
3619 // 4 byte strings, since it is the worst case. 3618 // 2 byte strings, since it is the worst case.
3620 static const intptr_t kSizeofRawString = sizeof(RawObject) + (2 * kWordSize); 3619 static const intptr_t kSizeofRawString = sizeof(RawObject) + (2 * kWordSize);
3621 static const intptr_t kMaxElements = kSmiMax / kFourByteChar; 3620 static const intptr_t kMaxElements = kSmiMax / kTwoByteChar;
3622 3621
3623 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); } 3622 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); }
3624 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); } 3623 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); }
3625 3624
3626 virtual intptr_t Hash() const; 3625 virtual intptr_t Hash() const;
3627 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); } 3626 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); }
3628 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len); 3627 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len);
3629 static intptr_t Hash(const uint8_t* characters, intptr_t len); 3628 static intptr_t Hash(const uint8_t* characters, intptr_t len);
3630 static intptr_t Hash(const uint16_t* characters, intptr_t len); 3629 static intptr_t Hash(const uint16_t* characters, intptr_t len);
3631 static intptr_t Hash(const uint32_t* characters, intptr_t len); 3630 static intptr_t Hash(const uint32_t* characters, intptr_t len);
(...skipping 20 matching lines...) Expand all
3652 virtual RawInstance* Canonicalize() const; 3651 virtual RawInstance* Canonicalize() const;
3653 3652
3654 bool IsSymbol() const { return raw()->IsCanonical(); } 3653 bool IsSymbol() const { return raw()->IsCanonical(); }
3655 3654
3656 virtual bool IsExternal() const { return false; } 3655 virtual bool IsExternal() const { return false; }
3657 virtual void* GetPeer() const { 3656 virtual void* GetPeer() const {
3658 UNREACHABLE(); 3657 UNREACHABLE();
3659 return NULL; 3658 return NULL;
3660 } 3659 }
3661 3660
3662 static RawString* New(const char* str, Heap::Space space = Heap::kNew); 3661 void ToUTF8(uint8_t* utf8_array, intptr_t array_len) const;
3663 static RawString* New(const uint8_t* characters, 3662
3664 intptr_t len, 3663 // Creates a new String object from a C string that is assumed to contain
3664 // UTF-8 encoded characters and '\0' is considered a termination character.
3665 static RawString* New(const char* cstr, Heap::Space space = Heap::kNew);
3666
3667 // Creates a new String object from an array of UTF-8 encoded characters.
3668 static RawString* New(const uint8_t* utf8_array,
3669 intptr_t array_len,
3665 Heap::Space space = Heap::kNew); 3670 Heap::Space space = Heap::kNew);
3666 static RawString* New(const uint16_t* characters, 3671
3667 intptr_t len, 3672 // Creates a new String object from an array of UTF-16 encoded characters.
3673 static RawString* New(const uint16_t* utf16_array,
3674 intptr_t array_len,
3668 Heap::Space space = Heap::kNew); 3675 Heap::Space space = Heap::kNew);
3669 static RawString* New(const uint32_t* characters, 3676
3670 intptr_t len, 3677 // Creates a new String object from an array of UTF-32 encoded characters.
3678 static RawString* New(const uint32_t* utf32_array,
3679 intptr_t array_len,
3671 Heap::Space space = Heap::kNew); 3680 Heap::Space space = Heap::kNew);
3681
3682 // Create a new String object from another Dart String instance.
3672 static RawString* New(const String& str, Heap::Space space = Heap::kNew); 3683 static RawString* New(const String& str, Heap::Space space = Heap::kNew);
3673 3684
3674 static RawString* NewExternal(const uint8_t* characters, 3685 // Creates a new External String object using the specified array of
3675 intptr_t len, 3686 // UTF-8 encoded characters as the external reference.
3687 static RawString* NewExternal(const uint8_t* utf8_array,
3688 intptr_t array_len,
3676 void* peer, 3689 void* peer,
3677 Dart_PeerFinalizer callback, 3690 Dart_PeerFinalizer callback,
3678 Heap::Space = Heap::kNew); 3691 Heap::Space = Heap::kNew);
3679 static RawString* NewExternal(const uint16_t* characters, 3692
3680 intptr_t len, 3693 // Creates a new External String object using the specified array of
3681 void* peer, 3694 // UTF-16 encoded characters as the external reference.
3682 Dart_PeerFinalizer callback, 3695 static RawString* NewExternal(const uint16_t* utf16_array,
3683 Heap::Space = Heap::kNew); 3696 intptr_t array_len,
3684 static RawString* NewExternal(const uint32_t* characters,
3685 intptr_t len,
3686 void* peer, 3697 void* peer,
3687 Dart_PeerFinalizer callback, 3698 Dart_PeerFinalizer callback,
3688 Heap::Space = Heap::kNew); 3699 Heap::Space = Heap::kNew);
3689 3700
3690 static void Copy(const String& dst, 3701 static void Copy(const String& dst,
3691 intptr_t dst_offset, 3702 intptr_t dst_offset,
3692 const uint8_t* characters, 3703 const uint8_t* characters,
3693 intptr_t len); 3704 intptr_t len);
3694 static void Copy(const String& dst, 3705 static void Copy(const String& dst,
3695 intptr_t dst_offset, 3706 intptr_t dst_offset,
3696 const uint16_t* characters, 3707 const uint16_t* characters,
3697 intptr_t len); 3708 intptr_t len);
3698 static void Copy(const String& dst, 3709 static void Copy(const String& dst,
3699 intptr_t dst_offset, 3710 intptr_t dst_offset,
3700 const uint32_t* characters,
3701 intptr_t len);
3702 static void Copy(const String& dst,
3703 intptr_t dst_offset,
3704 const String& src, 3711 const String& src,
3705 intptr_t src_offset, 3712 intptr_t src_offset,
3706 intptr_t len); 3713 intptr_t len);
3707 3714
3708 static RawString* EscapeSpecialCharacters(const String& str, bool raw_str); 3715 static RawString* EscapeSpecialCharacters(const String& str, bool raw_str);
3709 3716
3710 static RawString* Concat(const String& str1, 3717 static RawString* Concat(const String& str1,
3711 const String& str2, 3718 const String& str2,
3712 Heap::Space space = Heap::kNew); 3719 Heap::Space space = Heap::kNew);
3713 static RawString* ConcatAll(const Array& strings, 3720 static RawString* ConcatAll(const Array& strings,
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
3867 ASSERT(0 <= len && len <= kMaxElements); 3874 ASSERT(0 <= len && len <= kMaxElements);
3868 return RoundedAllocationSize( 3875 return RoundedAllocationSize(
3869 sizeof(RawTwoByteString) + (len * kBytesPerElement)); 3876 sizeof(RawTwoByteString) + (len * kBytesPerElement));
3870 } 3877 }
3871 3878
3872 static RawTwoByteString* New(intptr_t len, 3879 static RawTwoByteString* New(intptr_t len,
3873 Heap::Space space); 3880 Heap::Space space);
3874 static RawTwoByteString* New(const uint16_t* characters, 3881 static RawTwoByteString* New(const uint16_t* characters,
3875 intptr_t len, 3882 intptr_t len,
3876 Heap::Space space); 3883 Heap::Space space);
3877 static RawTwoByteString* New(const uint32_t* characters, 3884 static RawTwoByteString* New(intptr_t utf16_len,
3885 const uint32_t* characters,
3878 intptr_t len, 3886 intptr_t len,
3879 Heap::Space space); 3887 Heap::Space space);
3880 static RawTwoByteString* New(const TwoByteString& str, 3888 static RawTwoByteString* New(const TwoByteString& str,
3881 Heap::Space space); 3889 Heap::Space space);
3882 3890
3883 static RawTwoByteString* Concat(const String& str1, 3891 static RawTwoByteString* Concat(const String& str1,
3884 const String& str2, 3892 const String& str2,
3885 Heap::Space space); 3893 Heap::Space space);
3886 static RawTwoByteString* ConcatAll(const Array& strings, 3894 static RawTwoByteString* ConcatAll(const Array& strings,
3887 intptr_t len, 3895 intptr_t len,
3888 Heap::Space space); 3896 Heap::Space space);
3889 3897
3890 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch), 3898 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch),
3891 const String& str, 3899 const String& str,
3892 Heap::Space space); 3900 Heap::Space space);
3893 3901
3894 private: 3902 private:
3895 uint16_t* CharAddr(intptr_t index) const { 3903 uint16_t* CharAddr(intptr_t index) const {
3896 ASSERT((index >= 0) && (index < Length())); 3904 ASSERT((index >= 0) && (index < Length()));
3897 return &raw_ptr()->data_[index]; 3905 return &raw_ptr()->data_[index];
3898 } 3906 }
3899 3907
3900 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String); 3908 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String);
3901 friend class Class; 3909 friend class Class;
3902 friend class String; 3910 friend class String;
3903 }; 3911 };
3904 3912
3905 3913
3906 class FourByteString : public String {
3907 public:
3908 virtual int32_t CharAt(intptr_t index) const {
3909 return *CharAddr(index);
3910 }
3911
3912 virtual intptr_t CharSize() const {
3913 return kFourByteChar;
3914 }
3915
3916 RawFourByteString* EscapeSpecialCharacters(bool raw_str) const;
3917
3918 static const intptr_t kBytesPerElement = 4;
3919 static const intptr_t kMaxElements = String::kMaxElements;
3920
3921 static intptr_t InstanceSize() {
3922 ASSERT(sizeof(RawFourByteString) == OFFSET_OF(RawFourByteString, data_));
3923 return 0;
3924 }
3925
3926 static intptr_t InstanceSize(intptr_t len) {
3927 ASSERT(sizeof(RawTwoByteString) == kSizeofRawString);
3928 ASSERT(0 <= len && len <= kMaxElements);
3929 return RoundedAllocationSize(
3930 sizeof(RawFourByteString) + (len * kBytesPerElement));
3931 }
3932
3933 static RawFourByteString* New(intptr_t len,
3934 Heap::Space space);
3935 static RawFourByteString* New(const uint32_t* characters,
3936 intptr_t len,
3937 Heap::Space space);
3938 static RawFourByteString* New(const FourByteString& str,
3939 Heap::Space space);
3940
3941 static RawFourByteString* Concat(const String& str1,
3942 const String& str2,
3943 Heap::Space space);
3944 static RawFourByteString* ConcatAll(const Array& strings,
3945 intptr_t len,
3946 Heap::Space space);
3947
3948 static RawFourByteString* Transform(int32_t (*mapping)(int32_t ch),
3949 const String& str,
3950 Heap::Space space);
3951
3952 private:
3953 uint32_t* CharAddr(intptr_t index) const {
3954 ASSERT((index >= 0) && (index < Length()));
3955 return &raw_ptr()->data_[index];
3956 }
3957
3958 HEAP_OBJECT_IMPLEMENTATION(FourByteString, String);
3959 friend class Class;
3960 friend class String;
3961 };
3962
3963
3964 class ExternalOneByteString : public String { 3914 class ExternalOneByteString : public String {
3965 public: 3915 public:
3966 virtual int32_t CharAt(intptr_t index) const { 3916 virtual int32_t CharAt(intptr_t index) const {
3967 return *CharAddr(index); 3917 return *CharAddr(index);
3968 } 3918 }
3969 3919
3970 virtual intptr_t CharSize() const { 3920 virtual intptr_t CharSize() const {
3971 return kOneByteChar; 3921 return kOneByteChar;
3972 } 3922 }
3973 3923
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
4050 } 4000 }
4051 4001
4052 static void Finalize(Dart_Handle handle, void* peer); 4002 static void Finalize(Dart_Handle handle, void* peer);
4053 4003
4054 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String); 4004 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String);
4055 friend class Class; 4005 friend class Class;
4056 friend class String; 4006 friend class String;
4057 }; 4007 };
4058 4008
4059 4009
4060 class ExternalFourByteString : public String {
4061 public:
4062 virtual int32_t CharAt(intptr_t index) const {
4063 return *CharAddr(index);
4064 }
4065
4066 virtual intptr_t CharSize() const {
4067 return kFourByteChar;
4068 }
4069
4070 virtual bool IsExternal() const { return true; }
4071 virtual void* GetPeer() const {
4072 return raw_ptr()->external_data_->peer();
4073 }
4074
4075 // We use the same maximum elements for all strings.
4076 static const intptr_t kBytesPerElement = 4;
4077 static const intptr_t kMaxElements = String::kMaxElements;
4078
4079 static intptr_t InstanceSize() {
4080 return RoundedAllocationSize(sizeof(RawExternalFourByteString));
4081 }
4082
4083 static RawExternalFourByteString* New(const uint32_t* characters,
4084 intptr_t len,
4085 void* peer,
4086 Dart_PeerFinalizer callback,
4087 Heap::Space space = Heap::kNew);
4088
4089 private:
4090 const uint32_t* CharAddr(intptr_t index) const {
4091 // TODO(iposva): Determine if we should throw an exception here.
4092 ASSERT((index >= 0) && (index < Length()));
4093 return &(raw_ptr()->external_data_->data()[index]);
4094 }
4095
4096 void SetExternalData(ExternalStringData<uint32_t>* data) {
4097 raw_ptr()->external_data_ = data;
4098 }
4099
4100 static void Finalize(Dart_Handle handle, void* peer);
4101
4102 HEAP_OBJECT_IMPLEMENTATION(ExternalFourByteString, String);
4103 friend class Class;
4104 friend class String;
4105 };
4106
4107
4108 // Class Bool implements Dart core class bool. 4010 // Class Bool implements Dart core class bool.
4109 class Bool : public Instance { 4011 class Bool : public Instance {
4110 public: 4012 public:
4111 bool value() const { 4013 bool value() const {
4112 return raw_ptr()->value_; 4014 return raw_ptr()->value_;
4113 } 4015 }
4114 4016
4115 static intptr_t InstanceSize() { 4017 static intptr_t InstanceSize() {
4116 return RoundedAllocationSize(sizeof(RawBool)); 4018 return RoundedAllocationSize(sizeof(RawBool));
4117 } 4019 }
(...skipping 1657 matching lines...) Expand 10 before | Expand all | Expand 10 after
5775 if (this->CharAt(i) != str.CharAt(begin_index + i)) { 5677 if (this->CharAt(i) != str.CharAt(begin_index + i)) {
5776 return false; 5678 return false;
5777 } 5679 }
5778 } 5680 }
5779 return true; 5681 return true;
5780 } 5682 }
5781 5683
5782 } // namespace dart 5684 } // namespace dart
5783 5685
5784 #endif // VM_OBJECT_H_ 5686 #endif // VM_OBJECT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698