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

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

Issue 11367044: Merged String subclasses into String. (Closed) Base URL: https://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 #include "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/bigint_operations.h" 10 #include "vm/bigint_operations.h"
(...skipping 2173 matching lines...) Expand 10 before | Expand all | Expand 10 after
2184 return false; 2184 return false;
2185 } 2185 }
2186 } 2186 }
2187 return true; 2187 return true;
2188 } 2188 }
2189 2189
2190 2190
2191 RawFunction* Class::LookupFunction(const String& name) const { 2191 RawFunction* Class::LookupFunction(const String& name) const {
2192 Isolate* isolate = Isolate::Current(); 2192 Isolate* isolate = Isolate::Current();
2193 ASSERT(name.IsOneByteString()); 2193 ASSERT(name.IsOneByteString());
2194 const OneByteString& lookup_name = OneByteString::Cast(name);
2195 Array& funcs = Array::Handle(isolate, functions()); 2194 Array& funcs = Array::Handle(isolate, functions());
2196 if (funcs.IsNull()) { 2195 if (funcs.IsNull()) {
2197 // This can occur, e.g., for Null classes. 2196 // This can occur, e.g., for Null classes.
2198 return Function::null(); 2197 return Function::null();
2199 } 2198 }
2200 Function& function = Function::Handle(isolate, Function::null()); 2199 Function& function = Function::Handle(isolate, Function::null());
2201 OneByteString& function_name = 2200 String& function_name =
2202 OneByteString::Handle(isolate, OneByteString::null()); 2201 OneByteString::Handle(isolate, String::null());
Tom Ball 2012/11/02 23:31:50 Done.
2203 intptr_t len = funcs.Length(); 2202 intptr_t len = funcs.Length();
2204 for (intptr_t i = 0; i < len; i++) { 2203 for (intptr_t i = 0; i < len; i++) {
2205 function ^= funcs.At(i); 2204 function ^= funcs.At(i);
2206 function_name ^= function.name(); 2205 function_name ^= function.name();
2207 if (function_name.EqualsIgnoringPrivateKey(lookup_name)) { 2206 if (OneByteString::EqualsIgnoringPrivateKey(function_name, name)) {
2208 return function.raw(); 2207 return function.raw();
2209 } 2208 }
2210 } 2209 }
2211 2210
2212 // No function found. 2211 // No function found.
2213 return Function::null(); 2212 return Function::null();
2214 } 2213 }
2215 2214
2216 2215
2217 RawFunction* Class::LookupGetterFunction(const String& name) const { 2216 RawFunction* Class::LookupGetterFunction(const String& name) const {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
2293 return field.raw(); 2292 return field.raw();
2294 } 2293 }
2295 // No field found. 2294 // No field found.
2296 return Field::null(); 2295 return Field::null();
2297 } 2296 }
2298 2297
2299 2298
2300 RawField* Class::LookupField(const String& name) const { 2299 RawField* Class::LookupField(const String& name) const {
2301 Isolate* isolate = Isolate::Current(); 2300 Isolate* isolate = Isolate::Current();
2302 ASSERT(name.IsOneByteString()); 2301 ASSERT(name.IsOneByteString());
2303 const OneByteString& lookup_name = OneByteString::Cast(name);
2304 const Array& flds = Array::Handle(isolate, fields()); 2302 const Array& flds = Array::Handle(isolate, fields());
2305 Field& field = Field::Handle(isolate, Field::null()); 2303 Field& field = Field::Handle(isolate, Field::null());
2306 OneByteString& field_name = 2304 String& field_name = String::Handle(isolate, String::null());
2307 OneByteString::Handle(isolate, OneByteString::null());
2308 intptr_t len = flds.Length(); 2305 intptr_t len = flds.Length();
2309 for (intptr_t i = 0; i < len; i++) { 2306 for (intptr_t i = 0; i < len; i++) {
2310 field ^= flds.At(i); 2307 field ^= flds.At(i);
2311 field_name ^= field.name(); 2308 field_name ^= field.name();
2312 if (field_name.EqualsIgnoringPrivateKey(lookup_name)) { 2309 if (OneByteString::EqualsIgnoringPrivateKey(field_name, name)) {
2313 return field.raw(); 2310 return field.raw();
2314 } 2311 }
2315 } 2312 }
2316 // No field found. 2313 // No field found.
2317 return Field::null(); 2314 return Field::null();
2318 } 2315 }
2319 2316
2320 2317
2321 RawLibraryPrefix* Class::LookupLibraryPrefix(const String& name) const { 2318 RawLibraryPrefix* Class::LookupLibraryPrefix(const String& name) const {
2322 Isolate* isolate = Isolate::Current(); 2319 Isolate* isolate = Isolate::Current();
(...skipping 6672 matching lines...) Expand 10 before | Expand all | Expand 10 after
8995 // Integer is an interface. No instances of Integer should exist. 8992 // Integer is an interface. No instances of Integer should exist.
8996 UNREACHABLE(); 8993 UNREACHABLE();
8997 return "Integer"; 8994 return "Integer";
8998 } 8995 }
8999 8996
9000 8997
9001 RawInteger* Integer::New(const String& str, Heap::Space space) { 8998 RawInteger* Integer::New(const String& str, Heap::Space space) {
9002 // We are not supposed to have integers represented as two byte or 8999 // We are not supposed to have integers represented as two byte or
9003 // four byte strings. 9000 // four byte strings.
9004 ASSERT(str.IsOneByteString()); 9001 ASSERT(str.IsOneByteString());
9005 const OneByteString& onestr = OneByteString::Cast(str);
9006 int64_t value; 9002 int64_t value;
9007 if (!OS::StringToInt64(onestr.ToCString(), &value)) { 9003 if (!OS::StringToInt64(str.ToCString(), &value)) {
9008 const Bigint& big = Bigint::Handle(Bigint::New(onestr, space)); 9004 const Bigint& big = Bigint::Handle(Bigint::New(str, space));
9009 ASSERT(!BigintOperations::FitsIntoSmi(big)); 9005 ASSERT(!BigintOperations::FitsIntoSmi(big));
9010 ASSERT(!BigintOperations::FitsIntoMint(big)); 9006 ASSERT(!BigintOperations::FitsIntoMint(big));
9011 return big.raw(); 9007 return big.raw();
9012 } 9008 }
9013 return Integer::New(value, space); 9009 return Integer::New(value, space);
9014 } 9010 }
9015 9011
9016 9012
9017 RawInteger* Integer::New(int64_t value, Heap::Space space) { 9013 RawInteger* Integer::New(int64_t value, Heap::Space space) {
9018 if ((value <= Smi::kMaxValue) && (value >= Smi::kMinValue)) { 9014 if ((value <= Smi::kMaxValue) && (value >= Smi::kMinValue)) {
(...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after
9763 return HashImpl(characters, len); 9759 return HashImpl(characters, len);
9764 } 9760 }
9765 9761
9766 9762
9767 intptr_t String::Hash(const uint32_t* characters, intptr_t len) { 9763 intptr_t String::Hash(const uint32_t* characters, intptr_t len) {
9768 return HashImpl(characters, len); 9764 return HashImpl(characters, len);
9769 } 9765 }
9770 9766
9771 9767
9772 int32_t String::CharAt(intptr_t index) const { 9768 int32_t String::CharAt(intptr_t index) const {
9773 // String is an abstract class. 9769 intptr_t class_id = raw()->GetClassId();
9770 ASSERT(RawObject::IsStringClassId(class_id));
9771 switch (class_id) {
9772 case kOneByteStringCid:
9773 return *OneByteString::CharAddr(*this, index);
9774 case kTwoByteStringCid:
9775 return *TwoByteString::CharAddr(*this, index);
9776 case kExternalOneByteStringCid:
9777 return *ExternalOneByteString::CharAddr(*this, index);
9778 case kExternalTwoByteStringCid:
9779 return *ExternalTwoByteString::CharAddr(*this, index);
9780 }
9774 UNREACHABLE(); 9781 UNREACHABLE();
9775 return 0; 9782 return 0;
9776 } 9783 }
9777 9784
9778 9785
9779 intptr_t String::CharSize() const { 9786 intptr_t String::CharSize() const {
9780 // String is an abstract class. 9787 intptr_t class_id = raw()->GetClassId();
9781 UNREACHABLE(); 9788 if (class_id == kOneByteStringCid || class_id == kExternalOneByteStringCid) {
9782 return 0; 9789 return kOneByteChar;
9790 }
9791 ASSERT(class_id == kTwoByteStringCid ||
9792 class_id == kExternalTwoByteStringCid);
9793 return kTwoByteChar;
9783 } 9794 }
9784 9795
9785 9796
9797 bool String::IsExternal() const {
9798 intptr_t class_id = raw()->GetClassId();
9799 return class_id == kExternalOneByteStringCid ||
9800 class_id == kExternalTwoByteStringCid;
9801 }
9802
9803
9804 void* String::GetPeer() const {
9805 intptr_t class_id = raw()->GetClassId();
9806 if (class_id == kExternalOneByteStringCid) {
9807 return ExternalOneByteString::GetPeer(*this);
9808 }
9809 ASSERT(class_id == kExternalTwoByteStringCid);
9810 return ExternalTwoByteString::GetPeer(*this);
9811 }
9812
9813
9786 bool String::Equals(const Instance& other) const { 9814 bool String::Equals(const Instance& other) const {
9787 if (this->raw() == other.raw()) { 9815 if (this->raw() == other.raw()) {
9788 // Both handles point to the same raw instance. 9816 // Both handles point to the same raw instance.
9789 return true; 9817 return true;
9790 } 9818 }
9791 9819
9792 if (!other.IsString() || other.IsNull()) { 9820 if (!other.IsString() || other.IsNull()) {
9793 return false; 9821 return false;
9794 } 9822 }
9795 9823
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
9918 return String::New(utf8_array, array_len, space); 9946 return String::New(utf8_array, array_len, space);
9919 } 9947 }
9920 9948
9921 9949
9922 RawString* String::New(const uint8_t* utf8_array, 9950 RawString* String::New(const uint8_t* utf8_array,
9923 intptr_t array_len, 9951 intptr_t array_len,
9924 Heap::Space space) { 9952 Heap::Space space) {
9925 Utf8::Type type; 9953 Utf8::Type type;
9926 intptr_t len = Utf8::CodePointCount(utf8_array, array_len, &type); 9954 intptr_t len = Utf8::CodePointCount(utf8_array, array_len, &type);
9927 if (type == Utf8::kAscii) { 9955 if (type == Utf8::kAscii) {
9928 const OneByteString& strobj 9956 const String& strobj = String::Handle(OneByteString::New(len, space));
9929 = OneByteString::Handle(OneByteString::New(len, space));
9930 if (len > 0) { 9957 if (len > 0) {
9931 NoGCScope no_gc; 9958 NoGCScope no_gc;
9932 Utf8::DecodeToAscii(utf8_array, array_len, strobj.CharAddr(0), len); 9959 Utf8::DecodeToAscii(utf8_array, array_len,
9960 OneByteString::CharAddr(strobj, 0), len);
9933 } 9961 }
9934 return strobj.raw(); 9962 return strobj.raw();
9935 } 9963 }
9936 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSMP)); 9964 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSMP));
9937 const TwoByteString& strobj = 9965 const String& strobj = String::Handle(TwoByteString::New(len, space));
9938 TwoByteString::Handle(TwoByteString::New(len, space));
9939 NoGCScope no_gc; 9966 NoGCScope no_gc;
9940 Utf8::DecodeToUTF16(utf8_array, array_len, strobj.CharAddr(0), len); 9967 Utf8::DecodeToUTF16(utf8_array, array_len,
9968 TwoByteString::CharAddr(strobj, 0), len);
9941 return strobj.raw(); 9969 return strobj.raw();
9942 } 9970 }
9943 9971
9944 9972
9945 RawString* String::New(const uint16_t* utf16_array, 9973 RawString* String::New(const uint16_t* utf16_array,
9946 intptr_t array_len, 9974 intptr_t array_len,
9947 Heap::Space space) { 9975 Heap::Space space) {
9948 bool is_one_byte_string = true; 9976 bool is_one_byte_string = true;
9949 for (intptr_t i = 0; i < array_len; ++i) { 9977 for (intptr_t i = 0; i < array_len; ++i) {
9950 if (utf16_array[i] > 0x7F) { 9978 if (utf16_array[i] > 0x7F) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
10016 } 10044 }
10017 10045
10018 10046
10019 void String::Copy(const String& dst, intptr_t dst_offset, 10047 void String::Copy(const String& dst, intptr_t dst_offset,
10020 const uint8_t* characters, 10048 const uint8_t* characters,
10021 intptr_t len) { 10049 intptr_t len) {
10022 ASSERT(dst_offset >= 0); 10050 ASSERT(dst_offset >= 0);
10023 ASSERT(len >= 0); 10051 ASSERT(len >= 0);
10024 ASSERT(len <= (dst.Length() - dst_offset)); 10052 ASSERT(len <= (dst.Length() - dst_offset));
10025 if (dst.IsOneByteString()) { 10053 if (dst.IsOneByteString()) {
10026 const OneByteString& onestr = OneByteString::Cast(dst);
10027 NoGCScope no_gc; 10054 NoGCScope no_gc;
10028 if (len > 0) { 10055 if (len > 0) {
10029 memmove(onestr.CharAddr(dst_offset), characters, len); 10056 memmove(OneByteString::CharAddr(dst, dst_offset),
10057 characters,
10058 len);
10030 } 10059 }
10031 } else if (dst.IsTwoByteString()) { 10060 } else if (dst.IsTwoByteString()) {
10032 const TwoByteString& twostr = TwoByteString::Cast(dst);
10033 NoGCScope no_gc; 10061 NoGCScope no_gc;
10062 uint16_t* dst_chars = TwoByteString::CharAddr(dst, dst_offset);
10034 for (intptr_t i = 0; i < len; ++i) { 10063 for (intptr_t i = 0; i < len; ++i) {
10035 *twostr.CharAddr(i + dst_offset) = characters[i]; 10064 dst_chars[i] = characters[i];
10036 } 10065 }
10037 } 10066 }
10038 } 10067 }
10039 10068
10040 10069
10041 void String::Copy(const String& dst, intptr_t dst_offset, 10070 void String::Copy(const String& dst, intptr_t dst_offset,
10042 const uint16_t* utf16_array, 10071 const uint16_t* utf16_array,
10043 intptr_t array_len) { 10072 intptr_t array_len) {
10044 ASSERT(dst_offset >= 0); 10073 ASSERT(dst_offset >= 0);
10045 ASSERT(array_len >= 0); 10074 ASSERT(array_len >= 0);
10046 ASSERT(array_len <= (dst.Length() - dst_offset)); 10075 ASSERT(array_len <= (dst.Length() - dst_offset));
10047 if (dst.IsOneByteString()) { 10076 if (dst.IsOneByteString()) {
10048 const OneByteString& onestr = OneByteString::Cast(dst);
10049 NoGCScope no_gc; 10077 NoGCScope no_gc;
10078 uint8_t* dst_chars = OneByteString::CharAddr(dst, dst_offset);
10050 for (intptr_t i = 0; i < array_len; ++i) { 10079 for (intptr_t i = 0; i < array_len; ++i) {
10051 ASSERT(utf16_array[i] <= 0x7F); 10080 ASSERT(utf16_array[i] <= 0x7F);
10052 *onestr.CharAddr(i + dst_offset) = utf16_array[i]; 10081 dst_chars[i + dst_offset] = utf16_array[i];
10053 } 10082 }
10054 } else { 10083 } else {
10055 ASSERT(dst.IsTwoByteString()); 10084 ASSERT(dst.IsTwoByteString());
10056 const TwoByteString& twostr = TwoByteString::Cast(dst);
10057 NoGCScope no_gc; 10085 NoGCScope no_gc;
10058 if (array_len > 0) { 10086 if (array_len > 0) {
10059 memmove(twostr.CharAddr(dst_offset), utf16_array, (array_len * 2)); 10087 memmove(TwoByteString::CharAddr(dst, dst_offset),
10088 utf16_array,
10089 array_len * 2);
10060 } 10090 }
10061 } 10091 }
10062 } 10092 }
10063 10093
10064 10094
10065 void String::Copy(const String& dst, intptr_t dst_offset, 10095 void String::Copy(const String& dst, intptr_t dst_offset,
10066 const String& src, intptr_t src_offset, 10096 const String& src, intptr_t src_offset,
10067 intptr_t len) { 10097 intptr_t len) {
10068 ASSERT(dst_offset >= 0); 10098 ASSERT(dst_offset >= 0);
10069 ASSERT(src_offset >= 0); 10099 ASSERT(src_offset >= 0);
10070 ASSERT(len >= 0); 10100 ASSERT(len >= 0);
10071 ASSERT(len <= (dst.Length() - dst_offset)); 10101 ASSERT(len <= (dst.Length() - dst_offset));
10072 ASSERT(len <= (src.Length() - src_offset)); 10102 ASSERT(len <= (src.Length() - src_offset));
10073 if (len > 0) { 10103 if (len > 0) {
10074 intptr_t char_size = src.CharSize(); 10104 intptr_t char_size = src.CharSize();
10075 if (char_size == kOneByteChar) { 10105 if (char_size == kOneByteChar) {
10076 if (src.IsOneByteString()) { 10106 if (src.IsOneByteString()) {
10077 const OneByteString& onestr = OneByteString::Cast(src);
10078 NoGCScope no_gc; 10107 NoGCScope no_gc;
10079 String::Copy(dst, dst_offset, onestr.CharAddr(0) + src_offset, len); 10108 String::Copy(dst,
10109 dst_offset,
10110 OneByteString::CharAddr(src, src_offset),
10111 len);
10080 } else { 10112 } else {
10081 ASSERT(src.IsExternalOneByteString()); 10113 ASSERT(src.IsExternalOneByteString());
10082 const ExternalOneByteString& onestr = ExternalOneByteString::Cast(src);
10083 NoGCScope no_gc; 10114 NoGCScope no_gc;
10084 String::Copy(dst, dst_offset, onestr.CharAddr(0) + src_offset, len); 10115 String::Copy(dst,
10116 dst_offset,
10117 ExternalOneByteString::CharAddr(src, src_offset),
10118 len);
10085 } 10119 }
10086 } else { 10120 } else {
10087 ASSERT(char_size == kTwoByteChar); 10121 ASSERT(char_size == kTwoByteChar);
10088 if (src.IsTwoByteString()) { 10122 if (src.IsTwoByteString()) {
10089 const TwoByteString& twostr = TwoByteString::Cast(src);
10090 NoGCScope no_gc; 10123 NoGCScope no_gc;
10091 String::Copy(dst, dst_offset, twostr.CharAddr(0) + src_offset, len); 10124 String::Copy(dst,
10125 dst_offset,
10126 TwoByteString::CharAddr(src, src_offset),
10127 len);
10092 } else { 10128 } else {
10093 ASSERT(src.IsExternalTwoByteString()); 10129 ASSERT(src.IsExternalTwoByteString());
10094 const ExternalTwoByteString& twostr = ExternalTwoByteString::Cast(src);
10095 NoGCScope no_gc; 10130 NoGCScope no_gc;
10096 String::Copy(dst, dst_offset, twostr.CharAddr(0) + src_offset, len); 10131 String::Copy(dst,
10132 dst_offset,
10133 ExternalTwoByteString::CharAddr(src, src_offset),
10134 len);
10097 } 10135 }
10098 } 10136 }
10099 } 10137 }
10100 } 10138 }
10101 10139
10102 10140
10103 RawString* String::EscapeSpecialCharacters(const String& str, bool raw_str) { 10141 RawString* String::EscapeSpecialCharacters(const String& str, bool raw_str) {
10104 if (str.IsOneByteString()) { 10142 if (str.IsOneByteString()) {
10105 const OneByteString& onestr = OneByteString::Cast(str); 10143 return OneByteString::EscapeSpecialCharacters(str, raw_str);
10106 return onestr.EscapeSpecialCharacters(raw_str);
10107 } 10144 }
10108 ASSERT(str.IsTwoByteString()); 10145 ASSERT(str.IsTwoByteString());
10109 const TwoByteString& twostr = TwoByteString::Cast(str); 10146 return TwoByteString::EscapeSpecialCharacters(str, raw_str);
10110 return twostr.EscapeSpecialCharacters(raw_str);
10111 } 10147 }
10112 10148
10113 10149
10114 RawString* String::NewFormatted(const char* format, ...) { 10150 RawString* String::NewFormatted(const char* format, ...) {
10115 va_list args; 10151 va_list args;
10116 va_start(args, format); 10152 va_start(args, format);
10117 RawString* result = NewFormattedV(format, args); 10153 RawString* result = NewFormattedV(format, args);
10118 NoGCScope no_gc; 10154 NoGCScope no_gc;
10119 va_end(args); 10155 va_end(args);
10120 return result; 10156 return result;
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
10217 Zone* zone = Isolate::Current()->current_zone(); 10253 Zone* zone = Isolate::Current()->current_zone();
10218 uint8_t* result = zone->Alloc<uint8_t>(len + 1); 10254 uint8_t* result = zone->Alloc<uint8_t>(len + 1);
10219 ToUTF8(result, len); 10255 ToUTF8(result, len);
10220 result[len] = 0; 10256 result[len] = 0;
10221 return reinterpret_cast<const char*>(result); 10257 return reinterpret_cast<const char*>(result);
10222 } 10258 }
10223 10259
10224 10260
10225 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const { 10261 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const {
10226 if (CharSize() == kOneByteChar) { 10262 if (CharSize() == kOneByteChar) {
10227 const OneByteString& obj = OneByteString::Cast(*this); 10263 const String& obj = *this;
10228 ASSERT(array_len >= obj.Length()); 10264 ASSERT(array_len >= obj.Length());
10229 if (obj.Length() > 0) { 10265 if (obj.Length() > 0) {
10230 memmove(utf8_array, obj.CharAddr(0), obj.Length()); 10266 memmove(utf8_array, OneByteString::CharAddr(obj, 0), obj.Length());
10231 } 10267 }
10232 } else { 10268 } else {
10233 ASSERT(array_len >= Utf8::Length(*this)); 10269 ASSERT(array_len >= Utf8::Length(*this));
10234 Utf8::Encode(*this, reinterpret_cast<char*>(utf8_array), array_len); 10270 Utf8::Encode(*this, reinterpret_cast<char*>(utf8_array), array_len);
10235 } 10271 }
10236 } 10272 }
10237 10273
10238 10274
10239 RawString* String::Transform(int32_t (*mapping)(int32_t ch), 10275 RawString* String::Transform(int32_t (*mapping)(int32_t ch),
10240 const String& str, 10276 const String& str,
(...skipping 27 matching lines...) Expand all
10268 return Transform(CaseMapping::ToUpper, str, space); 10304 return Transform(CaseMapping::ToUpper, str, space);
10269 } 10305 }
10270 10306
10271 10307
10272 RawString* String::ToLowerCase(const String& str, Heap::Space space) { 10308 RawString* String::ToLowerCase(const String& str, Heap::Space space) {
10273 // TODO(cshapiro): create a fast-path for OneByteString instances. 10309 // TODO(cshapiro): create a fast-path for OneByteString instances.
10274 return Transform(CaseMapping::ToLower, str, space); 10310 return Transform(CaseMapping::ToLower, str, space);
10275 } 10311 }
10276 10312
10277 10313
10278 RawOneByteString* OneByteString::EscapeSpecialCharacters(bool raw_str) const { 10314 RawOneByteString* OneByteString::EscapeSpecialCharacters(const String& str,
10279 intptr_t len = Length(); 10315 bool raw_str) {
10316 intptr_t len = str.Length();
10280 if (len > 0) { 10317 if (len > 0) {
10281 intptr_t num_escapes = 0; 10318 intptr_t num_escapes = 0;
10282 intptr_t index = 0; 10319 intptr_t index = 0;
10320 uint8_t* str_chars = CharAddr(str, 0);
10283 for (intptr_t i = 0; i < len; i++) { 10321 for (intptr_t i = 0; i < len; i++) {
10284 if (IsSpecialCharacter(*CharAddr(i)) || 10322 if (IsSpecialCharacter(str_chars[i]) ||
10285 (!raw_str && (*CharAddr(i) == '\\'))) { 10323 (!raw_str && (str_chars[i] == '\\'))) {
Tom Ball 2012/11/02 23:31:50 The code was getting uglier and harder to understa
10286 num_escapes += 1; 10324 num_escapes += 1;
10287 } 10325 }
10288 } 10326 }
10289 const OneByteString& dststr = OneByteString::Handle( 10327 const String& dststr = String::Handle(
10290 OneByteString::New(len + num_escapes, Heap::kNew)); 10328 OneByteString::New(len + num_escapes, Heap::kNew));
10329 uint8_t* dst_chars = CharAddr(dststr, 0);
10291 for (intptr_t i = 0; i < len; i++) { 10330 for (intptr_t i = 0; i < len; i++) {
10292 if (IsSpecialCharacter(*CharAddr(i))) { 10331 if (IsSpecialCharacter(str_chars[i])) {
10293 *(dststr.CharAddr(index)) = '\\'; 10332 dst_chars[index] = '\\';
10294 *(dststr.CharAddr(index + 1)) = SpecialCharacter(*CharAddr(i)); 10333 dst_chars[index + 1] = SpecialCharacter(str_chars[i]);
10295 index += 2; 10334 index += 2;
10296 } else if (!raw_str && (*CharAddr(i) == '\\')) { 10335 } else if (!raw_str && (str_chars[i] == '\\')) {
10297 *(dststr.CharAddr(index)) = '\\'; 10336 dst_chars[index] = '\\';
10298 *(dststr.CharAddr(index + 1)) = '\\'; 10337 dst_chars[index + 1] = '\\';
10299 index += 2; 10338 index += 2;
10300 } else { 10339 } else {
10301 *(dststr.CharAddr(index)) = *CharAddr(i); 10340 dst_chars[index] = str_chars[i];
10302 index += 1; 10341 index += 1;
10303 } 10342 }
10304 } 10343 }
10305 return dststr.raw(); 10344 return reinterpret_cast<RawOneByteString*>(dststr.raw());
10306 } 10345 }
10307 return OneByteString::null(); 10346 return OneByteString::null();
10308 } 10347 }
10309 10348
10310 10349
10311 // Check to see if 'name' matches 'this' as is or 10350 // Check to see if 'str1' matches 'str2' as is or
10312 // once the private key separator is stripped from name. 10351 // once the private key separator is stripped from str2.
10313 // 10352 //
10314 // Things are made more complicated by the fact that constructors are 10353 // Things are made more complicated by the fact that constructors are
10315 // added *after* the private suffix, so "foo@123.named" should match 10354 // added *after* the private suffix, so "foo@123.named" should match
10316 // "foo.named". 10355 // "foo.named".
10317 // 10356 //
10318 // Also, the private suffix can occur more than once in the name, as in: 10357 // Also, the private suffix can occur more than once in the name, as in:
10319 // 10358 //
10320 // _ReceivePortImpl@6be832b._internal@6be832b 10359 // _ReceivePortImpl@6be832b._internal@6be832b
10321 // 10360 //
10322 bool OneByteString::EqualsIgnoringPrivateKey(const OneByteString& name) const { 10361 bool OneByteString::EqualsIgnoringPrivateKey(const String& str1,
10323 if (raw() == name.raw()) { 10362 const String& str2) {
10363 if (str1.raw() == str2.raw()) {
10324 return true; // Both handles point to the same raw instance. 10364 return true; // Both handles point to the same raw instance.
10325 } 10365 }
10326 intptr_t len = Length(); 10366 intptr_t len = str1.Length();
10327 intptr_t name_len = name.Length(); 10367 intptr_t str2_len = str2.Length();
10328 if (len == name_len) { 10368 uint8_t* str1_chars = CharAddr(str1, 0);
10369 uint8_t* str2_chars = CharAddr(str2, 0);
10370 if (len == str2_len) {
10329 for (intptr_t i = 0; i < len; i++) { 10371 for (intptr_t i = 0; i < len; i++) {
10330 if (*(CharAddr(i)) != *(name.CharAddr(i))) { 10372 if (str1_chars[i] != str2_chars[i]) {
10331 return false; 10373 return false;
10332 } 10374 }
10333 } 10375 }
10334 return true; 10376 return true;
10335 } 10377 }
10336 if (len < name_len) { 10378 if (len < str2_len) {
10337 return false; // No way they can match. 10379 return false; // No way they can match.
10338 } 10380 }
10339 intptr_t pos = 0; 10381 intptr_t pos = 0;
10340 intptr_t name_pos = 0; 10382 intptr_t str2_pos = 0;
10341 while (pos < len) { 10383 while (pos < len) {
10342 int32_t ch = *(CharAddr(pos)); 10384 int32_t ch = str1_chars[pos];
10343 pos++; 10385 pos++;
10344 10386
10345 if (ch == Scanner::kPrivateKeySeparator) { 10387 if (ch == Scanner::kPrivateKeySeparator) {
10346 // Consume a private key separator. 10388 // Consume a private key separator.
10347 while (pos < len && *(CharAddr(pos)) != '.') { 10389 while ((pos < len) && (str1_chars[pos] != '.')) {
10348 pos++; 10390 pos++;
10349 } 10391 }
10350 // Resume matching characters. 10392 // Resume matching characters.
10351 continue; 10393 continue;
10352 } 10394 }
10353 if (name_pos == name_len || ch != *(name.CharAddr(name_pos))) { 10395 if ((str2_pos == str2_len) || (ch != str2_chars[str2_pos])) {
10354 return false; 10396 return false;
10355 } 10397 }
10356 name_pos++; 10398 str2_pos++;
10357 } 10399 }
10358 10400
10359 // We have reached the end of mangled_name string. 10401 // We have reached the end of mangled_name string.
10360 ASSERT(pos == len); 10402 ASSERT(pos == len);
10361 return (name_pos == name_len); 10403 return (str2_pos == str2_len);
10362 } 10404 }
10363 10405
10364 10406
10365 RawOneByteString* OneByteString::New(intptr_t len, 10407 RawOneByteString* OneByteString::New(intptr_t len,
10366 Heap::Space space) { 10408 Heap::Space space) {
10367 ASSERT(Isolate::Current() == Dart::vm_isolate() || 10409 ASSERT(Isolate::Current() == Dart::vm_isolate() ||
10368 Isolate::Current()->object_store()->one_byte_string_class() != 10410 Isolate::Current()->object_store()->one_byte_string_class() !=
10369 Class::null()); 10411 Class::null());
10370 if (len < 0 || len > kMaxElements) { 10412 if (len < 0 || len > kMaxElements) {
10371 // This should be caught before we reach here. 10413 // This should be caught before we reach here.
10372 FATAL1("Fatal error in OneByteString::New: invalid len %"Pd"\n", len); 10414 FATAL1("Fatal error in OneByteString::New: invalid len %"Pd"\n", len);
10373 } 10415 }
10374 OneByteString& result = OneByteString::Handle(); 10416 String& result = String::Handle();
10375 { 10417 {
10376 RawObject* raw = Object::Allocate(OneByteString::kClassId, 10418 RawObject* raw = Object::Allocate(kClassId,
10377 OneByteString::InstanceSize(len), 10419 OneByteString::InstanceSize(len),
10378 space); 10420 space);
10379 NoGCScope no_gc; 10421 NoGCScope no_gc;
10380 result ^= raw; 10422 result ^= raw;
10381 result.SetLength(len); 10423 result.SetLength(len);
10382 result.SetHash(0); 10424 result.SetHash(0);
10383 } 10425 }
10384 return result.raw(); 10426 return reinterpret_cast<RawOneByteString*>(result.raw());
10385 } 10427 }
10386 10428
10387 10429
10388 RawOneByteString* OneByteString::New(const uint8_t* characters, 10430 RawOneByteString* OneByteString::New(const uint8_t* characters,
10389 intptr_t len, 10431 intptr_t len,
10390 Heap::Space space) { 10432 Heap::Space space) {
10391 const OneByteString& result = 10433 const String& result = String::Handle(OneByteString::New(len, space));
10392 OneByteString::Handle(OneByteString::New(len, space));
10393 if (len > 0) { 10434 if (len > 0) {
10394 NoGCScope no_gc; 10435 NoGCScope no_gc;
10395 memmove(result.CharAddr(0), characters, len); 10436 memmove(CharAddr(result, 0), characters, len);
10396 } 10437 }
10397 return result.raw(); 10438 return reinterpret_cast<RawOneByteString*>(result.raw());
10398 } 10439 }
10399 10440
10400 10441
10401 RawOneByteString* OneByteString::New(const uint16_t* characters, 10442 RawOneByteString* OneByteString::New(const uint16_t* characters,
10402 intptr_t len, 10443 intptr_t len,
10403 Heap::Space space) { 10444 Heap::Space space) {
10404 const OneByteString& result = 10445 const String& result =String::Handle(OneByteString::New(len, space));
10405 OneByteString::Handle(OneByteString::New(len, space)); 10446 uint8_t* dst_chars = CharAddr(result, 0);
10406 for (intptr_t i = 0; i < len; ++i) { 10447 for (intptr_t i = 0; i < len; ++i) {
10407 ASSERT(characters[i] <= 0x7F); 10448 ASSERT(characters[i] <= 0x7F);
10408 *result.CharAddr(i) = characters[i]; 10449 dst_chars[i] = characters[i];
10409 } 10450 }
10410 return result.raw(); 10451 return reinterpret_cast<RawOneByteString*>(result.raw());
10411 } 10452 }
10412 10453
10413 10454
10414 RawOneByteString* OneByteString::New(const uint32_t* characters, 10455 RawOneByteString* OneByteString::New(const uint32_t* characters,
10415 intptr_t len, 10456 intptr_t len,
10416 Heap::Space space) { 10457 Heap::Space space) {
10417 const OneByteString& result = 10458 const String& result =String::Handle(OneByteString::New(len, space));
10418 OneByteString::Handle(OneByteString::New(len, space)); 10459 uint8_t* dst_chars = CharAddr(result, 0);
10419 for (intptr_t i = 0; i < len; ++i) { 10460 for (intptr_t i = 0; i < len; ++i) {
10420 ASSERT(characters[i] <= 0x7F); 10461 ASSERT(characters[i] <= 0x7F);
10421 *result.CharAddr(i) = characters[i]; 10462 dst_chars[i] = characters[i];
10422 } 10463 }
10423 return result.raw(); 10464 return reinterpret_cast<RawOneByteString*>(result.raw());
10424 } 10465 }
10425 10466
10426 10467
10427 RawOneByteString* OneByteString::New(const OneByteString& str, 10468 RawOneByteString* OneByteString::New(const OneByteString& str,
10428 Heap::Space space) { 10469 Heap::Space space) {
10429 intptr_t len = str.Length(); 10470 intptr_t len = str.Length();
10430 const OneByteString& result = 10471 const String& result = String::Handle(OneByteString::New(len, space));
10431 OneByteString::Handle(OneByteString::New(len, space));
10432 String::Copy(result, 0, str, 0, len); 10472 String::Copy(result, 0, str, 0, len);
10433 return result.raw(); 10473 return reinterpret_cast<RawOneByteString*>(result.raw());
10434 } 10474 }
10435 10475
10436 10476
10437 RawOneByteString* OneByteString::Concat(const String& str1, 10477 RawOneByteString* OneByteString::Concat(const String& str1,
10438 const String& str2, 10478 const String& str2,
10439 Heap::Space space) { 10479 Heap::Space space) {
10440 intptr_t len1 = str1.Length(); 10480 intptr_t len1 = str1.Length();
10441 intptr_t len2 = str2.Length(); 10481 intptr_t len2 = str2.Length();
10442 intptr_t len = len1 + len2; 10482 intptr_t len = len1 + len2;
10443 const OneByteString& result = 10483 const String& result = String::Handle(OneByteString::New(len, space));
10444 OneByteString::Handle(OneByteString::New(len, space));
10445 String::Copy(result, 0, str1, 0, len1); 10484 String::Copy(result, 0, str1, 0, len1);
10446 String::Copy(result, len1, str2, 0, len2); 10485 String::Copy(result, len1, str2, 0, len2);
10447 return result.raw(); 10486 return reinterpret_cast<RawOneByteString*>(result.raw());
10448 } 10487 }
10449 10488
10450 10489
10451 RawOneByteString* OneByteString::ConcatAll(const Array& strings, 10490 RawOneByteString* OneByteString::ConcatAll(const Array& strings,
10452 intptr_t len, 10491 intptr_t len,
10453 Heap::Space space) { 10492 Heap::Space space) {
10454 const OneByteString& result = 10493 const String& result = String::Handle(OneByteString::New(len, space));
10455 OneByteString::Handle(OneByteString::New(len, space)); 10494 String& str = String::Handle();
10456 OneByteString& str = OneByteString::Handle();
10457 intptr_t strings_len = strings.Length(); 10495 intptr_t strings_len = strings.Length();
10458 intptr_t pos = 0; 10496 intptr_t pos = 0;
10459 for (intptr_t i = 0; i < strings_len; i++) { 10497 for (intptr_t i = 0; i < strings_len; i++) {
10460 str ^= strings.At(i); 10498 str ^= strings.At(i);
10461 intptr_t str_len = str.Length(); 10499 intptr_t str_len = str.Length();
10462 String::Copy(result, pos, str, 0, str_len); 10500 String::Copy(result, pos, str, 0, str_len);
10463 pos += str_len; 10501 pos += str_len;
10464 } 10502 }
10465 return result.raw(); 10503 return reinterpret_cast<RawOneByteString*>(result.raw());
10466 } 10504 }
10467 10505
10468 10506
10469 RawOneByteString* OneByteString::Transform(int32_t (*mapping)(int32_t ch), 10507 RawOneByteString* OneByteString::Transform(int32_t (*mapping)(int32_t ch),
10470 const String& str, 10508 const String& str,
10471 Heap::Space space) { 10509 Heap::Space space) {
10472 ASSERT(!str.IsNull()); 10510 ASSERT(!str.IsNull());
10473 intptr_t len = str.Length(); 10511 intptr_t len = str.Length();
10474 const OneByteString& result = 10512 const String& result = String::Handle(OneByteString::New(len, space));
10475 OneByteString::Handle(OneByteString::New(len, space)); 10513 uint8_t* dst_chars = CharAddr(result, 0);
10476 for (intptr_t i = 0; i < len; ++i) { 10514 for (intptr_t i = 0; i < len; ++i) {
10477 int32_t ch = mapping(str.CharAt(i)); 10515 int32_t ch = mapping(str.CharAt(i));
10478 ASSERT(ch >= 0 && ch <= 0x7F); 10516 ASSERT(ch >= 0 && ch <= 0x7F);
10479 *result.CharAddr(i) = ch; 10517 dst_chars[i] = ch;
10480 } 10518 }
10481 return result.raw(); 10519 return reinterpret_cast<RawOneByteString*>(result.raw());
10482 } 10520 }
10483 10521
10484 10522
10485 const char* OneByteString::ToCString() const { 10523 RawTwoByteString* TwoByteString::EscapeSpecialCharacters(const String& str,
10486 return String::ToCString(); 10524 bool raw_str) {
10487 } 10525 intptr_t len = str.Length();
10488
10489
10490 RawTwoByteString* TwoByteString::EscapeSpecialCharacters(bool raw_str) const {
10491 intptr_t len = Length();
10492 if (len > 0) { 10526 if (len > 0) {
10493 intptr_t num_escapes = 0; 10527 intptr_t num_escapes = 0;
10494 intptr_t index = 0; 10528 intptr_t index = 0;
10529 uint16_t* str_chars = CharAddr(str, 0);
10495 for (intptr_t i = 0; i < len; i++) { 10530 for (intptr_t i = 0; i < len; i++) {
10496 if (IsSpecialCharacter(*CharAddr(i)) || 10531 if (IsSpecialCharacter(str_chars[i]) ||
10497 (!raw_str && (*CharAddr(i) == '\\'))) { 10532 (!raw_str && (str_chars[i] == '\\'))) {
Tom Ball 2012/11/02 23:31:50 Restored.
10498 num_escapes += 1; 10533 num_escapes += 1;
10499 } 10534 }
10500 } 10535 }
10501 const TwoByteString& dststr = TwoByteString::Handle( 10536 const String& dststr = String::Handle(
10502 TwoByteString::New(len + num_escapes, Heap::kNew)); 10537 TwoByteString::New(len + num_escapes, Heap::kNew));
10538 uint16_t* dst_chars = CharAddr(dststr, 0);
10503 for (intptr_t i = 0; i < len; i++) { 10539 for (intptr_t i = 0; i < len; i++) {
10504 if (IsSpecialCharacter(*CharAddr(i))) { 10540 if (IsSpecialCharacter(str_chars[i])) {
Tom Ball 2012/11/02 23:31:50 Restored.
Tom Ball 2012/11/02 23:31:50 Restored.
10505 *(dststr.CharAddr(index)) = '\\'; 10541 dst_chars[index] = '\\';
10506 *(dststr.CharAddr(index + 1)) = SpecialCharacter(*CharAddr(i)); 10542 dst_chars[index + 1] = SpecialCharacter(str_chars[i]);
10507 index += 2; 10543 index += 2;
10508 } else if (!raw_str && (*CharAddr(i) == '\\')) { 10544 } else if (!raw_str && (str_chars[i] == '\\')) {
10509 *(dststr.CharAddr(index)) = '\\'; 10545 dst_chars[index] = '\\';
10510 *(dststr.CharAddr(index + 1)) = '\\'; 10546 dst_chars[index + 1] = '\\';
10511 index += 2; 10547 index += 2;
10512 } else { 10548 } else {
10513 *(dststr.CharAddr(index)) = *CharAddr(i); 10549 dst_chars[index] = str_chars[i];
10514 index += 1; 10550 index += 1;
10515 } 10551 }
10516 } 10552 }
10517 return dststr.raw(); 10553 return reinterpret_cast<RawTwoByteString*>(dststr.raw());
10518 } 10554 }
10519 return TwoByteString::null(); 10555 return TwoByteString::null();
10520 } 10556 }
10521 10557
10522 10558
10523 RawTwoByteString* TwoByteString::New(intptr_t len, 10559 RawTwoByteString* TwoByteString::New(intptr_t len,
10524 Heap::Space space) { 10560 Heap::Space space) {
10525 ASSERT(Isolate::Current()->object_store()->two_byte_string_class()); 10561 ASSERT(Isolate::Current()->object_store()->two_byte_string_class());
10526 if (len < 0 || len > kMaxElements) { 10562 if (len < 0 || len > kMaxElements) {
10527 // This should be caught before we reach here. 10563 // This should be caught before we reach here.
10528 FATAL1("Fatal error in TwoByteString::New: invalid len %"Pd"\n", len); 10564 FATAL1("Fatal error in TwoByteString::New: invalid len %"Pd"\n", len);
10529 } 10565 }
10530 TwoByteString& result = TwoByteString::Handle(); 10566 String& result = String::Handle();
10531 { 10567 {
10532 RawObject* raw = Object::Allocate(TwoByteString::kClassId, 10568 RawObject* raw = Object::Allocate(kClassId,
10533 TwoByteString::InstanceSize(len), 10569 TwoByteString::InstanceSize(len),
10534 space); 10570 space);
10535 NoGCScope no_gc; 10571 NoGCScope no_gc;
10536 result ^= raw; 10572 result ^= raw;
10537 result.SetLength(len); 10573 result.SetLength(len);
10538 result.SetHash(0); 10574 result.SetHash(0);
10539 } 10575 }
10540 return result.raw(); 10576 return reinterpret_cast<RawTwoByteString*>(result.raw());
10541 } 10577 }
10542 10578
10543 10579
10544 RawTwoByteString* TwoByteString::New(const uint16_t* utf16_array, 10580 RawTwoByteString* TwoByteString::New(const uint16_t* utf16_array,
10545 intptr_t array_len, 10581 intptr_t array_len,
10546 Heap::Space space) { 10582 Heap::Space space) {
10547 ASSERT(array_len > 0); 10583 ASSERT(array_len > 0);
10548 const TwoByteString& result = 10584 const String& result = String::Handle(TwoByteString::New(array_len, space));
10549 TwoByteString::Handle(TwoByteString::New(array_len, space));
10550 { 10585 {
10551 NoGCScope no_gc; 10586 NoGCScope no_gc;
10552 memmove(result.CharAddr(0), utf16_array, (array_len * 2)); 10587 memmove(CharAddr(result, 0), utf16_array, (array_len * 2));
10553 } 10588 }
10554 return result.raw(); 10589 return reinterpret_cast<RawTwoByteString*>(result.raw());
10555 } 10590 }
10556 10591
10557 10592
10558 RawTwoByteString* TwoByteString::New(intptr_t utf16_len, 10593 RawTwoByteString* TwoByteString::New(intptr_t utf16_len,
10559 const uint32_t* utf32_array, 10594 const uint32_t* utf32_array,
10560 intptr_t array_len, 10595 intptr_t array_len,
10561 Heap::Space space) { 10596 Heap::Space space) {
10562 ASSERT((array_len > 0) && (utf16_len >= array_len)); 10597 ASSERT((array_len > 0) && (utf16_len >= array_len));
10563 const TwoByteString& result = 10598 const String& result = String::Handle(TwoByteString::New(utf16_len, space));
10564 TwoByteString::Handle(TwoByteString::New(utf16_len, space));
10565 { 10599 {
10566 NoGCScope no_gc; 10600 NoGCScope no_gc;
10567 intptr_t j = 0; 10601 intptr_t j = 0;
10602 uint16_t* dst = CharAddr(result, 0);
10568 for (intptr_t i = 0; i < array_len; ++i) { 10603 for (intptr_t i = 0; i < array_len; ++i) {
10569 if (utf32_array[i] > 0xffff) { 10604 if (utf32_array[i] > 0xffff) {
10570 ASSERT(j < (utf16_len - 1)); 10605 ASSERT(j < (utf16_len - 1));
10571 Utf8::ConvertUTF32ToUTF16(utf32_array[i], result.CharAddr(j)); 10606 Utf8::ConvertUTF32ToUTF16(utf32_array[i], &dst[j]);
10572 j += 2; 10607 j += 2;
10573 } else { 10608 } else {
10574 ASSERT(j < utf16_len); 10609 ASSERT(j < utf16_len);
10575 *result.CharAddr(j) = utf32_array[i]; 10610 dst[j] = utf32_array[i];
10576 j += 1; 10611 j += 1;
10577 } 10612 }
10578 } 10613 }
10579 } 10614 }
10580 return result.raw(); 10615 return reinterpret_cast<RawTwoByteString*>(result.raw());
10581 } 10616 }
10582 10617
10583 10618
10584 RawTwoByteString* TwoByteString::New(const TwoByteString& str, 10619 RawTwoByteString* TwoByteString::New(const TwoByteString& str,
10585 Heap::Space space) { 10620 Heap::Space space) {
10586 intptr_t len = str.Length(); 10621 intptr_t len = str.Length();
10587 const TwoByteString& result = 10622 const String& result = String::Handle(TwoByteString::New(len, space));
10588 TwoByteString::Handle(TwoByteString::New(len, space));
10589 String::Copy(result, 0, str, 0, len); 10623 String::Copy(result, 0, str, 0, len);
10590 return result.raw(); 10624 return reinterpret_cast<RawTwoByteString*>(result.raw());
10591 } 10625 }
10592 10626
10593 10627
10594 RawTwoByteString* TwoByteString::Concat(const String& str1, 10628 RawTwoByteString* TwoByteString::Concat(const String& str1,
10595 const String& str2, 10629 const String& str2,
10596 Heap::Space space) { 10630 Heap::Space space) {
10597 intptr_t len1 = str1.Length(); 10631 intptr_t len1 = str1.Length();
10598 intptr_t len2 = str2.Length(); 10632 intptr_t len2 = str2.Length();
10599 intptr_t len = len1 + len2; 10633 intptr_t len = len1 + len2;
10600 const TwoByteString& result = 10634 const String& result = String::Handle(TwoByteString::New(len, space));
10601 TwoByteString::Handle(TwoByteString::New(len, space));
10602 String::Copy(result, 0, str1, 0, len1); 10635 String::Copy(result, 0, str1, 0, len1);
10603 String::Copy(result, len1, str2, 0, len2); 10636 String::Copy(result, len1, str2, 0, len2);
10604 return result.raw(); 10637 return reinterpret_cast<RawTwoByteString*>(result.raw());
10605 } 10638 }
10606 10639
10607 10640
10608 RawTwoByteString* TwoByteString::ConcatAll(const Array& strings, 10641 RawTwoByteString* TwoByteString::ConcatAll(const Array& strings,
10609 intptr_t len, 10642 intptr_t len,
10610 Heap::Space space) { 10643 Heap::Space space) {
10611 const TwoByteString& result = 10644 const String& result = String::Handle(TwoByteString::New(len, space));
10612 TwoByteString::Handle(TwoByteString::New(len, space));
10613 String& str = String::Handle(); 10645 String& str = String::Handle();
10614 intptr_t strings_len = strings.Length(); 10646 intptr_t strings_len = strings.Length();
10615 intptr_t pos = 0; 10647 intptr_t pos = 0;
10616 for (intptr_t i = 0; i < strings_len; i++) { 10648 for (intptr_t i = 0; i < strings_len; i++) {
10617 str ^= strings.At(i); 10649 str ^= strings.At(i);
10618 intptr_t str_len = str.Length(); 10650 intptr_t str_len = str.Length();
10619 String::Copy(result, pos, str, 0, str_len); 10651 String::Copy(result, pos, str, 0, str_len);
10620 pos += str_len; 10652 pos += str_len;
10621 } 10653 }
10622 return result.raw(); 10654 return reinterpret_cast<RawTwoByteString*>(result.raw());
10623 } 10655 }
10624 10656
10625 10657
10626 RawTwoByteString* TwoByteString::Transform(int32_t (*mapping)(int32_t ch), 10658 RawTwoByteString* TwoByteString::Transform(int32_t (*mapping)(int32_t ch),
10627 const String& str, 10659 const String& str,
10628 Heap::Space space) { 10660 Heap::Space space) {
10629 ASSERT(!str.IsNull()); 10661 ASSERT(!str.IsNull());
10630 intptr_t len = str.Length(); 10662 intptr_t len = str.Length();
10631 const TwoByteString& result = 10663 const String& result = String::Handle(TwoByteString::New(len, space));
10632 TwoByteString::Handle(TwoByteString::New(len, space)); 10664 uint16_t* dst_chars = CharAddr(result, 0);
10633 for (intptr_t i = 0; i < len; ++i) { 10665 for (intptr_t i = 0; i < len; ++i) {
10634 int32_t ch = mapping(str.CharAt(i)); 10666 int32_t ch = mapping(str.CharAt(i));
10635 ASSERT(ch >= 0 && ch <= 0xFFFF); 10667 ASSERT(ch >= 0 && ch <= 0xFFFF);
10636 *result.CharAddr(i) = ch; 10668 dst_chars[i] = ch;
10637 } 10669 }
10638 return result.raw(); 10670 return reinterpret_cast<RawTwoByteString*>(result.raw());
10639 } 10671 }
10640 10672
10641 10673
10642 const char* TwoByteString::ToCString() const {
10643 return String::ToCString();
10644 }
10645
10646
10647 static void AddFinalizer(const Object& referent, 10674 static void AddFinalizer(const Object& referent,
10648 void* peer, 10675 void* peer,
10649 Dart_WeakPersistentHandleFinalizer callback) { 10676 Dart_WeakPersistentHandleFinalizer callback) {
10650 ASSERT(callback != NULL); 10677 ASSERT(callback != NULL);
10651 ApiState* state = Isolate::Current()->api_state(); 10678 ApiState* state = Isolate::Current()->api_state();
10652 ASSERT(state != NULL); 10679 ASSERT(state != NULL);
10653 FinalizablePersistentHandle* weak_ref = 10680 FinalizablePersistentHandle* weak_ref =
10654 state->weak_persistent_handles().AllocateHandle(); 10681 state->weak_persistent_handles().AllocateHandle();
10655 weak_ref->set_raw(referent); 10682 weak_ref->set_raw(referent);
10656 weak_ref->set_peer(peer); 10683 weak_ref->set_peer(peer);
10657 weak_ref->set_callback(callback); 10684 weak_ref->set_callback(callback);
10658 } 10685 }
10659 10686
10660 10687
10661 RawExternalOneByteString* ExternalOneByteString::New( 10688 RawExternalOneByteString* ExternalOneByteString::New(
10662 const uint8_t* data, 10689 const uint8_t* data,
10663 intptr_t len, 10690 intptr_t len,
10664 void* peer, 10691 void* peer,
10665 Dart_PeerFinalizer callback, 10692 Dart_PeerFinalizer callback,
10666 Heap::Space space) { 10693 Heap::Space space) {
10667 ASSERT(Isolate::Current()->object_store()->external_one_byte_string_class() != 10694 ASSERT(Isolate::Current()->object_store()->
10668 Class::null()); 10695 external_one_byte_string_class() != Class::null());
10669 if (len < 0 || len > kMaxElements) { 10696 if (len < 0 || len > kMaxElements) {
10670 // This should be caught before we reach here. 10697 // This should be caught before we reach here.
10671 FATAL1("Fatal error in ExternalOneByteString::New: invalid len %"Pd"\n", 10698 FATAL1("Fatal error in ExternalOneByteString::New: invalid len %"Pd"\n",
10672 len); 10699 len);
10673 } 10700 }
10674 ExternalOneByteString& result = ExternalOneByteString::Handle(); 10701 String& result = String::Handle();
10675 ExternalStringData<uint8_t>* external_data = 10702 ExternalStringData<uint8_t>* external_data =
10676 new ExternalStringData<uint8_t>(data, peer, callback); 10703 new ExternalStringData<uint8_t>(data, peer, callback);
10677 { 10704 {
10678 RawObject* raw = Object::Allocate(ExternalOneByteString::kClassId, 10705 RawObject* raw = Object::Allocate(kClassId,
10679 ExternalOneByteString::InstanceSize(), 10706 ExternalOneByteString::InstanceSize(),
10680 space); 10707 space);
10681 NoGCScope no_gc; 10708 NoGCScope no_gc;
10682 result ^= raw; 10709 result ^= raw;
10683 result.SetLength(len); 10710 result.SetLength(len);
10684 result.SetHash(0); 10711 result.SetHash(0);
10685 result.SetExternalData(external_data); 10712 SetExternalData(result, external_data);
10686 } 10713 }
10687 AddFinalizer(result, external_data, ExternalOneByteString::Finalize); 10714 AddFinalizer(result, external_data, ExternalOneByteString::Finalize);
10688 return result.raw(); 10715 return reinterpret_cast<RawExternalOneByteString*>(result.raw());
10689 } 10716 }
10690 10717
10691 10718
10692 static void DeleteWeakPersistentHandle(Dart_Handle handle) { 10719 static void DeleteWeakPersistentHandle(Dart_Handle handle) {
10693 ApiState* state = Isolate::Current()->api_state(); 10720 ApiState* state = Isolate::Current()->api_state();
10694 ASSERT(state != NULL); 10721 ASSERT(state != NULL);
10695 FinalizablePersistentHandle* weak_ref = 10722 FinalizablePersistentHandle* weak_ref =
10696 reinterpret_cast<FinalizablePersistentHandle*>(handle); 10723 reinterpret_cast<FinalizablePersistentHandle*>(handle);
10697 ASSERT(state->IsValidWeakPersistentHandle(handle)); 10724 ASSERT(state->IsValidWeakPersistentHandle(handle));
10698 state->weak_persistent_handles().FreeHandle(weak_ref); 10725 state->weak_persistent_handles().FreeHandle(weak_ref);
10699 } 10726 }
10700 10727
10701 10728
10702 void ExternalOneByteString::Finalize(Dart_Handle handle, void* peer) { 10729 void ExternalOneByteString::Finalize(Dart_Handle handle, void* peer) {
10703 delete reinterpret_cast<ExternalStringData<uint8_t>*>(peer); 10730 delete reinterpret_cast<ExternalStringData<uint8_t>*>(peer);
10704 DeleteWeakPersistentHandle(handle); 10731 DeleteWeakPersistentHandle(handle);
10705 } 10732 }
10706 10733
10707 10734
10708 const char* ExternalOneByteString::ToCString() const {
10709 return String::ToCString();
10710 }
10711
10712
10713 RawExternalTwoByteString* ExternalTwoByteString::New( 10735 RawExternalTwoByteString* ExternalTwoByteString::New(
10714 const uint16_t* data, 10736 const uint16_t* data,
10715 intptr_t len, 10737 intptr_t len,
10716 void* peer, 10738 void* peer,
10717 Dart_PeerFinalizer callback, 10739 Dart_PeerFinalizer callback,
10718 Heap::Space space) { 10740 Heap::Space space) {
10719 ASSERT(Isolate::Current()->object_store()->external_two_byte_string_class() != 10741 ASSERT(Isolate::Current()->object_store()->external_two_byte_string_class() !=
10720 Class::null()); 10742 Class::null());
10721 if (len < 0 || len > kMaxElements) { 10743 if (len < 0 || len > kMaxElements) {
10722 // This should be caught before we reach here. 10744 // This should be caught before we reach here.
10723 FATAL1("Fatal error in ExternalTwoByteString::New: invalid len %"Pd"\n", 10745 FATAL1("Fatal error in ExternalTwoByteString::New: invalid len %"Pd"\n",
10724 len); 10746 len);
10725 } 10747 }
10726 ExternalTwoByteString& result = ExternalTwoByteString::Handle(); 10748 String& result = String::Handle();
10727 ExternalStringData<uint16_t>* external_data = 10749 ExternalStringData<uint16_t>* external_data =
10728 new ExternalStringData<uint16_t>(data, peer, callback); 10750 new ExternalStringData<uint16_t>(data, peer, callback);
10729 { 10751 {
10730 RawObject* raw = Object::Allocate(ExternalTwoByteString::kClassId, 10752 RawObject* raw = Object::Allocate(kClassId,
10731 ExternalTwoByteString::InstanceSize(), 10753 ExternalTwoByteString::InstanceSize(),
10732 space); 10754 space);
10733 NoGCScope no_gc; 10755 NoGCScope no_gc;
10734 result ^= raw; 10756 result ^= raw;
10735 result.SetLength(len); 10757 result.SetLength(len);
10736 result.SetHash(0); 10758 result.SetHash(0);
10737 result.SetExternalData(external_data); 10759 SetExternalData(result, external_data);
10738 } 10760 }
10739 AddFinalizer(result, external_data, ExternalTwoByteString::Finalize); 10761 AddFinalizer(result, external_data, ExternalTwoByteString::Finalize);
10740 return result.raw(); 10762 return reinterpret_cast<RawExternalTwoByteString*>(result.raw());
10741 } 10763 }
10742 10764
10743 10765
10744 void ExternalTwoByteString::Finalize(Dart_Handle handle, void* peer) { 10766 void ExternalTwoByteString::Finalize(Dart_Handle handle, void* peer) {
10745 delete reinterpret_cast<ExternalStringData<uint16_t>*>(peer); 10767 delete reinterpret_cast<ExternalStringData<uint16_t>*>(peer);
10746 DeleteWeakPersistentHandle(handle); 10768 DeleteWeakPersistentHandle(handle);
10747 } 10769 }
10748 10770
10749 10771
10750 const char* ExternalTwoByteString::ToCString() const {
10751 return String::ToCString();
10752 }
10753
10754
10755 RawBool* Bool::True() { 10772 RawBool* Bool::True() {
10756 return Isolate::Current()->object_store()->true_value(); 10773 return Isolate::Current()->object_store()->true_value();
10757 } 10774 }
10758 10775
10759 10776
10760 RawBool* Bool::False() { 10777 RawBool* Bool::False() {
10761 return Isolate::Current()->object_store()->false_value(); 10778 return Isolate::Current()->object_store()->false_value();
10762 } 10779 }
10763 10780
10764 10781
(...skipping 1118 matching lines...) Expand 10 before | Expand all | Expand 10 after
11883 } 11900 }
11884 return result.raw(); 11901 return result.raw();
11885 } 11902 }
11886 11903
11887 11904
11888 const char* WeakProperty::ToCString() const { 11905 const char* WeakProperty::ToCString() const {
11889 return "_WeakProperty"; 11906 return "_WeakProperty";
11890 } 11907 }
11891 11908
11892 } // namespace dart 11909 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698