OLD | NEW |
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 10709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
10720 intptr_t String::Hash(const int32_t* characters, intptr_t len) { | 10720 intptr_t String::Hash(const int32_t* characters, intptr_t len) { |
10721 return HashImpl(characters, len); | 10721 return HashImpl(characters, len); |
10722 } | 10722 } |
10723 | 10723 |
10724 | 10724 |
10725 int32_t String::CharAt(intptr_t index) const { | 10725 int32_t String::CharAt(intptr_t index) const { |
10726 intptr_t class_id = raw()->GetClassId(); | 10726 intptr_t class_id = raw()->GetClassId(); |
10727 ASSERT(RawObject::IsStringClassId(class_id)); | 10727 ASSERT(RawObject::IsStringClassId(class_id)); |
10728 NoGCScope no_gc; | 10728 NoGCScope no_gc; |
10729 if (class_id == kOneByteStringCid) { | 10729 if (class_id == kOneByteStringCid) { |
10730 return *OneByteString::CharAddr(*this, index); | 10730 return *OneByteString::CharAddr(*this, index); |
10731 } | 10731 } |
10732 if (class_id == kTwoByteStringCid) { | 10732 if (class_id == kTwoByteStringCid) { |
10733 return *TwoByteString::CharAddr(*this, index); | 10733 return *TwoByteString::CharAddr(*this, index); |
10734 } | 10734 } |
10735 if (class_id == kExternalOneByteStringCid) { | 10735 if (class_id == kExternalOneByteStringCid) { |
10736 return *ExternalOneByteString::CharAddr(*this, index); | 10736 return *ExternalOneByteString::CharAddr(*this, index); |
10737 } | 10737 } |
10738 ASSERT(class_id == kExternalTwoByteStringCid); | 10738 ASSERT(class_id == kExternalTwoByteStringCid); |
10739 return *ExternalTwoByteString::CharAddr(*this, index); | 10739 return *ExternalTwoByteString::CharAddr(*this, index); |
10740 } | 10740 } |
10741 | 10741 |
10742 | 10742 |
10743 intptr_t String::CharSize() const { | 10743 intptr_t String::CharSize() const { |
10744 intptr_t class_id = raw()->GetClassId(); | 10744 intptr_t class_id = raw()->GetClassId(); |
10745 if (class_id == kOneByteStringCid || class_id == kExternalOneByteStringCid) { | 10745 if (class_id == kOneByteStringCid || class_id == kExternalOneByteStringCid) { |
10746 return kOneByteChar; | 10746 return kOneByteChar; |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11199 result = OneByteString::New(length, space); | 11199 result = OneByteString::New(length, space); |
11200 } else { | 11200 } else { |
11201 result = TwoByteString::New(length, space); | 11201 result = TwoByteString::New(length, space); |
11202 } | 11202 } |
11203 String::Copy(result, 0, str, begin_index, length); | 11203 String::Copy(result, 0, str, begin_index, length); |
11204 return result.raw(); | 11204 return result.raw(); |
11205 } | 11205 } |
11206 | 11206 |
11207 | 11207 |
11208 const char* String::ToCString() const { | 11208 const char* String::ToCString() const { |
11209 intptr_t len = Utf8::Length(*this); | 11209 if (IsOneByteString()) { |
| 11210 // Quick conversion if OneByteString contains only ASCII characters. |
| 11211 intptr_t len = Length(); |
| 11212 if (len == 0) { |
| 11213 return ""; |
| 11214 } |
| 11215 Zone* zone = Isolate::Current()->current_zone(); |
| 11216 uint8_t* result = zone->Alloc<uint8_t>(len + 1); |
| 11217 NoGCScope no_gc; |
| 11218 const uint8_t* original_str = OneByteString::CharAddr(*this, 0); |
| 11219 for (intptr_t i = 0; i < len; i++) { |
| 11220 if (original_str[i] <= Utf8::kMaxOneByteChar) { |
| 11221 result[i] = original_str[i]; |
| 11222 } else { |
| 11223 len = -1; |
| 11224 break; |
| 11225 } |
| 11226 } |
| 11227 if (len > 0) { |
| 11228 result[len] = 0; |
| 11229 return reinterpret_cast<const char*>(result); |
| 11230 } |
| 11231 } |
| 11232 const intptr_t len = Utf8::Length(*this); |
11210 Zone* zone = Isolate::Current()->current_zone(); | 11233 Zone* zone = Isolate::Current()->current_zone(); |
11211 uint8_t* result = zone->Alloc<uint8_t>(len + 1); | 11234 uint8_t* result = zone->Alloc<uint8_t>(len + 1); |
11212 ToUTF8(result, len); | 11235 ToUTF8(result, len); |
11213 result[len] = 0; | 11236 result[len] = 0; |
11214 return reinterpret_cast<const char*>(result); | 11237 return reinterpret_cast<const char*>(result); |
11215 } | 11238 } |
11216 | 11239 |
11217 | 11240 |
11218 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const { | 11241 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const { |
11219 ASSERT(array_len >= Utf8::Length(*this)); | 11242 ASSERT(array_len >= Utf8::Length(*this)); |
(...skipping 1772 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12992 } | 13015 } |
12993 return result.raw(); | 13016 return result.raw(); |
12994 } | 13017 } |
12995 | 13018 |
12996 | 13019 |
12997 const char* WeakProperty::ToCString() const { | 13020 const char* WeakProperty::ToCString() const { |
12998 return "_WeakProperty"; | 13021 return "_WeakProperty"; |
12999 } | 13022 } |
13000 | 13023 |
13001 } // namespace dart | 13024 } // namespace dart |
OLD | NEW |