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

Side by Side Diff: runtime/vm/object.h

Issue 11318018: - Represent strings internally in UTF-16 format, this makes it (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
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 3608 matching lines...) Expand 10 before | Expand all | Expand 10 after
3619 3619
3620 // String may not be '\0' terminated. 3620 // String may not be '\0' terminated.
3621 class String : public Instance { 3621 class String : public Instance {
3622 public: 3622 public:
3623 // We use 30 bits for the hash code so that we consistently use a 3623 // We use 30 bits for the hash code so that we consistently use a
3624 // 32bit Smi representation for the hash code on all architectures. 3624 // 32bit Smi representation for the hash code on all architectures.
3625 static const intptr_t kHashBits = 30; 3625 static const intptr_t kHashBits = 30;
3626 3626
3627 static const intptr_t kOneByteChar = 1; 3627 static const intptr_t kOneByteChar = 1;
3628 static const intptr_t kTwoByteChar = 2; 3628 static const intptr_t kTwoByteChar = 2;
3629 static const intptr_t kFourByteChar = 4;
3630 3629
3631 // All strings share the same maximum element count to keep things 3630 // All strings share the same maximum element count to keep things
3632 // simple. We choose a value that will prevent integer overflow for 3631 // simple. We choose a value that will prevent integer overflow for
3633 // 4 byte strings, since it is the worst case. 3632 // 2 byte strings, since it is the worst case.
3634 static const intptr_t kSizeofRawString = sizeof(RawObject) + (2 * kWordSize); 3633 static const intptr_t kSizeofRawString = sizeof(RawObject) + (2 * kWordSize);
3635 static const intptr_t kMaxElements = kSmiMax / kFourByteChar; 3634 static const intptr_t kMaxElements = kSmiMax / kTwoByteChar;
3636 3635
3637 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); } 3636 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); }
3638 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); } 3637 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); }
3639 3638
3640 virtual intptr_t Hash() const; 3639 virtual intptr_t Hash() const;
3641 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); } 3640 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); }
3642 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len); 3641 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len);
3643 static intptr_t Hash(const uint8_t* characters, intptr_t len); 3642 static intptr_t Hash(const uint8_t* characters, intptr_t len);
3644 static intptr_t Hash(const uint16_t* characters, intptr_t len); 3643 static intptr_t Hash(const uint16_t* characters, intptr_t len);
3645 static intptr_t Hash(const uint32_t* characters, intptr_t len); 3644 static intptr_t Hash(const uint32_t* characters, intptr_t len);
(...skipping 20 matching lines...) Expand all
3666 virtual RawInstance* Canonicalize() const; 3665 virtual RawInstance* Canonicalize() const;
3667 3666
3668 bool IsSymbol() const { return raw()->IsCanonical(); } 3667 bool IsSymbol() const { return raw()->IsCanonical(); }
3669 3668
3670 virtual bool IsExternal() const { return false; } 3669 virtual bool IsExternal() const { return false; }
3671 virtual void* GetPeer() const { 3670 virtual void* GetPeer() const {
3672 UNREACHABLE(); 3671 UNREACHABLE();
3673 return NULL; 3672 return NULL;
3674 } 3673 }
3675 3674
3676 static RawString* New(const char* str, Heap::Space space = Heap::kNew); 3675 void ToUTF8(uint8_t* utf8_array, intptr_t array_len) const;
3677 static RawString* New(const uint8_t* characters, 3676
3678 intptr_t len, 3677 // Creates a new String object from a C string that is assumed to contain
3678 // UTF-8 encoded characters and '\0' is considered a termination character.
3679 static RawString* New(const char* cstr, Heap::Space space = Heap::kNew);
3680
3681 // Creates a new String object from an array of UTF-8 encoded characters.
3682 static RawString* New(const uint8_t* utf8_array,
3683 intptr_t array_len,
3679 Heap::Space space = Heap::kNew); 3684 Heap::Space space = Heap::kNew);
3680 static RawString* New(const uint16_t* characters, 3685
3681 intptr_t len, 3686 // Creates a new String object from an array of UTF-16 encoded characters.
3687 static RawString* New(const uint16_t* utf16_array,
3688 intptr_t array_len,
3682 Heap::Space space = Heap::kNew); 3689 Heap::Space space = Heap::kNew);
3683 static RawString* New(const uint32_t* characters, 3690
3684 intptr_t len, 3691 // Creates a new String object from an array of UTF-32 encoded characters.
3692 static RawString* New(const uint32_t* utf32_array,
3693 intptr_t array_len,
3685 Heap::Space space = Heap::kNew); 3694 Heap::Space space = Heap::kNew);
3695
3696 // Create a new String object from another Dart String instance.
3686 static RawString* New(const String& str, Heap::Space space = Heap::kNew); 3697 static RawString* New(const String& str, Heap::Space space = Heap::kNew);
3687 3698
3688 static RawString* NewExternal(const uint8_t* characters, 3699 // Creates a new External String object using the specified array of
3689 intptr_t len, 3700 // UTF-8 encoded characters as the external reference.
3701 static RawString* NewExternal(const uint8_t* utf8_array,
3702 intptr_t array_len,
3690 void* peer, 3703 void* peer,
3691 Dart_PeerFinalizer callback, 3704 Dart_PeerFinalizer callback,
3692 Heap::Space = Heap::kNew); 3705 Heap::Space = Heap::kNew);
3693 static RawString* NewExternal(const uint16_t* characters, 3706
3694 intptr_t len, 3707 // Creates a new External String object using the specified array of
3695 void* peer, 3708 // UTF-16 encoded characters as the external reference.
3696 Dart_PeerFinalizer callback, 3709 static RawString* NewExternal(const uint16_t* utf16_array,
3697 Heap::Space = Heap::kNew); 3710 intptr_t array_len,
3698 static RawString* NewExternal(const uint32_t* characters,
3699 intptr_t len,
3700 void* peer, 3711 void* peer,
3701 Dart_PeerFinalizer callback, 3712 Dart_PeerFinalizer callback,
3702 Heap::Space = Heap::kNew); 3713 Heap::Space = Heap::kNew);
3703 3714
3704 static void Copy(const String& dst, 3715 static void Copy(const String& dst,
3705 intptr_t dst_offset, 3716 intptr_t dst_offset,
3706 const uint8_t* characters, 3717 const uint8_t* characters,
3707 intptr_t len); 3718 intptr_t len);
3708 static void Copy(const String& dst, 3719 static void Copy(const String& dst,
3709 intptr_t dst_offset, 3720 intptr_t dst_offset,
3710 const uint16_t* characters, 3721 const uint16_t* characters,
3711 intptr_t len); 3722 intptr_t len);
3712 static void Copy(const String& dst, 3723 static void Copy(const String& dst,
3713 intptr_t dst_offset, 3724 intptr_t dst_offset,
3714 const uint32_t* characters,
3715 intptr_t len);
3716 static void Copy(const String& dst,
3717 intptr_t dst_offset,
3718 const String& src, 3725 const String& src,
3719 intptr_t src_offset, 3726 intptr_t src_offset,
3720 intptr_t len); 3727 intptr_t len);
3721 3728
3722 static RawString* EscapeSpecialCharacters(const String& str, bool raw_str); 3729 static RawString* EscapeSpecialCharacters(const String& str, bool raw_str);
3723 3730
3724 static RawString* Concat(const String& str1, 3731 static RawString* Concat(const String& str1,
3725 const String& str2, 3732 const String& str2,
3726 Heap::Space space = Heap::kNew); 3733 Heap::Space space = Heap::kNew);
3727 static RawString* ConcatAll(const Array& strings, 3734 static RawString* ConcatAll(const Array& strings,
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
3881 ASSERT(0 <= len && len <= kMaxElements); 3888 ASSERT(0 <= len && len <= kMaxElements);
3882 return RoundedAllocationSize( 3889 return RoundedAllocationSize(
3883 sizeof(RawTwoByteString) + (len * kBytesPerElement)); 3890 sizeof(RawTwoByteString) + (len * kBytesPerElement));
3884 } 3891 }
3885 3892
3886 static RawTwoByteString* New(intptr_t len, 3893 static RawTwoByteString* New(intptr_t len,
3887 Heap::Space space); 3894 Heap::Space space);
3888 static RawTwoByteString* New(const uint16_t* characters, 3895 static RawTwoByteString* New(const uint16_t* characters,
3889 intptr_t len, 3896 intptr_t len,
3890 Heap::Space space); 3897 Heap::Space space);
3891 static RawTwoByteString* New(const uint32_t* characters, 3898 static RawTwoByteString* New(intptr_t utf16_len,
3899 const uint32_t* characters,
3892 intptr_t len, 3900 intptr_t len,
3893 Heap::Space space); 3901 Heap::Space space);
3894 static RawTwoByteString* New(const TwoByteString& str, 3902 static RawTwoByteString* New(const TwoByteString& str,
3895 Heap::Space space); 3903 Heap::Space space);
3896 3904
3897 static RawTwoByteString* Concat(const String& str1, 3905 static RawTwoByteString* Concat(const String& str1,
3898 const String& str2, 3906 const String& str2,
3899 Heap::Space space); 3907 Heap::Space space);
3900 static RawTwoByteString* ConcatAll(const Array& strings, 3908 static RawTwoByteString* ConcatAll(const Array& strings,
3901 intptr_t len, 3909 intptr_t len,
3902 Heap::Space space); 3910 Heap::Space space);
3903 3911
3904 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch), 3912 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch),
3905 const String& str, 3913 const String& str,
3906 Heap::Space space); 3914 Heap::Space space);
3907 3915
3908 private: 3916 private:
3909 uint16_t* CharAddr(intptr_t index) const { 3917 uint16_t* CharAddr(intptr_t index) const {
3910 ASSERT((index >= 0) && (index < Length())); 3918 ASSERT((index >= 0) && (index < Length()));
3911 return &raw_ptr()->data_[index]; 3919 return &raw_ptr()->data_[index];
3912 } 3920 }
3913 3921
3914 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String); 3922 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String);
3915 friend class Class; 3923 friend class Class;
3916 friend class String; 3924 friend class String;
3917 }; 3925 };
3918 3926
3919 3927
3920 class FourByteString : public String {
3921 public:
3922 virtual int32_t CharAt(intptr_t index) const {
3923 return *CharAddr(index);
3924 }
3925
3926 virtual intptr_t CharSize() const {
3927 return kFourByteChar;
3928 }
3929
3930 RawFourByteString* EscapeSpecialCharacters(bool raw_str) const;
3931
3932 static const intptr_t kBytesPerElement = 4;
3933 static const intptr_t kMaxElements = String::kMaxElements;
3934
3935 static intptr_t InstanceSize() {
3936 ASSERT(sizeof(RawFourByteString) == OFFSET_OF(RawFourByteString, data_));
3937 return 0;
3938 }
3939
3940 static intptr_t InstanceSize(intptr_t len) {
3941 ASSERT(sizeof(RawTwoByteString) == kSizeofRawString);
3942 ASSERT(0 <= len && len <= kMaxElements);
3943 return RoundedAllocationSize(
3944 sizeof(RawFourByteString) + (len * kBytesPerElement));
3945 }
3946
3947 static RawFourByteString* New(intptr_t len,
3948 Heap::Space space);
3949 static RawFourByteString* New(const uint32_t* characters,
3950 intptr_t len,
3951 Heap::Space space);
3952 static RawFourByteString* New(const FourByteString& str,
3953 Heap::Space space);
3954
3955 static RawFourByteString* Concat(const String& str1,
3956 const String& str2,
3957 Heap::Space space);
3958 static RawFourByteString* ConcatAll(const Array& strings,
3959 intptr_t len,
3960 Heap::Space space);
3961
3962 static RawFourByteString* Transform(int32_t (*mapping)(int32_t ch),
3963 const String& str,
3964 Heap::Space space);
3965
3966 private:
3967 uint32_t* CharAddr(intptr_t index) const {
3968 ASSERT((index >= 0) && (index < Length()));
3969 return &raw_ptr()->data_[index];
3970 }
3971
3972 HEAP_OBJECT_IMPLEMENTATION(FourByteString, String);
3973 friend class Class;
3974 friend class String;
3975 };
3976
3977
3978 class ExternalOneByteString : public String { 3928 class ExternalOneByteString : public String {
3979 public: 3929 public:
3980 virtual int32_t CharAt(intptr_t index) const { 3930 virtual int32_t CharAt(intptr_t index) const {
3981 return *CharAddr(index); 3931 return *CharAddr(index);
3982 } 3932 }
3983 3933
3984 virtual intptr_t CharSize() const { 3934 virtual intptr_t CharSize() const {
3985 return kOneByteChar; 3935 return kOneByteChar;
3986 } 3936 }
3987 3937
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
4064 } 4014 }
4065 4015
4066 static void Finalize(Dart_Handle handle, void* peer); 4016 static void Finalize(Dart_Handle handle, void* peer);
4067 4017
4068 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String); 4018 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String);
4069 friend class Class; 4019 friend class Class;
4070 friend class String; 4020 friend class String;
4071 }; 4021 };
4072 4022
4073 4023
4074 class ExternalFourByteString : public String {
4075 public:
4076 virtual int32_t CharAt(intptr_t index) const {
4077 return *CharAddr(index);
4078 }
4079
4080 virtual intptr_t CharSize() const {
4081 return kFourByteChar;
4082 }
4083
4084 virtual bool IsExternal() const { return true; }
4085 virtual void* GetPeer() const {
4086 return raw_ptr()->external_data_->peer();
4087 }
4088
4089 // We use the same maximum elements for all strings.
4090 static const intptr_t kBytesPerElement = 4;
4091 static const intptr_t kMaxElements = String::kMaxElements;
4092
4093 static intptr_t InstanceSize() {
4094 return RoundedAllocationSize(sizeof(RawExternalFourByteString));
4095 }
4096
4097 static RawExternalFourByteString* New(const uint32_t* characters,
4098 intptr_t len,
4099 void* peer,
4100 Dart_PeerFinalizer callback,
4101 Heap::Space space = Heap::kNew);
4102
4103 private:
4104 const uint32_t* CharAddr(intptr_t index) const {
4105 // TODO(iposva): Determine if we should throw an exception here.
4106 ASSERT((index >= 0) && (index < Length()));
4107 return &(raw_ptr()->external_data_->data()[index]);
4108 }
4109
4110 void SetExternalData(ExternalStringData<uint32_t>* data) {
4111 raw_ptr()->external_data_ = data;
4112 }
4113
4114 static void Finalize(Dart_Handle handle, void* peer);
4115
4116 HEAP_OBJECT_IMPLEMENTATION(ExternalFourByteString, String);
4117 friend class Class;
4118 friend class String;
4119 };
4120
4121
4122 // Class Bool implements Dart core class bool. 4024 // Class Bool implements Dart core class bool.
4123 class Bool : public Instance { 4025 class Bool : public Instance {
4124 public: 4026 public:
4125 bool value() const { 4027 bool value() const {
4126 return raw_ptr()->value_; 4028 return raw_ptr()->value_;
4127 } 4029 }
4128 4030
4129 static intptr_t InstanceSize() { 4031 static intptr_t InstanceSize() {
4130 return RoundedAllocationSize(sizeof(RawBool)); 4032 return RoundedAllocationSize(sizeof(RawBool));
4131 } 4033 }
(...skipping 1657 matching lines...) Expand 10 before | Expand all | Expand 10 after
5789 if (this->CharAt(i) != str.CharAt(begin_index + i)) { 5691 if (this->CharAt(i) != str.CharAt(begin_index + i)) {
5790 return false; 5692 return false;
5791 } 5693 }
5792 } 5694 }
5793 return true; 5695 return true;
5794 } 5696 }
5795 5697
5796 } // namespace dart 5698 } // namespace dart
5797 5699
5798 #endif // VM_OBJECT_H_ 5700 #endif // VM_OBJECT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698