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 10618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
10629 intptr_t String::Hash(const int32_t* characters, intptr_t len) { | 10629 intptr_t String::Hash(const int32_t* characters, intptr_t len) { |
10630 return HashImpl(characters, len); | 10630 return HashImpl(characters, len); |
10631 } | 10631 } |
10632 | 10632 |
10633 | 10633 |
10634 int32_t String::CharAt(intptr_t index) const { | 10634 int32_t String::CharAt(intptr_t index) const { |
10635 intptr_t class_id = raw()->GetClassId(); | 10635 intptr_t class_id = raw()->GetClassId(); |
10636 ASSERT(RawObject::IsStringClassId(class_id)); | 10636 ASSERT(RawObject::IsStringClassId(class_id)); |
10637 NoGCScope no_gc; | 10637 NoGCScope no_gc; |
10638 if (class_id == kOneByteStringCid) { | 10638 if (class_id == kOneByteStringCid) { |
10639 return *OneByteString::CharAddr(*this, index); | 10639 return *OneByteString::CharAddr(*this, index); |
10640 } | 10640 } |
10641 if (class_id == kTwoByteStringCid) { | 10641 if (class_id == kTwoByteStringCid) { |
10642 return *TwoByteString::CharAddr(*this, index); | 10642 return *TwoByteString::CharAddr(*this, index); |
10643 } | 10643 } |
10644 if (class_id == kExternalOneByteStringCid) { | 10644 if (class_id == kExternalOneByteStringCid) { |
10645 return *ExternalOneByteString::CharAddr(*this, index); | 10645 return *ExternalOneByteString::CharAddr(*this, index); |
10646 } | 10646 } |
10647 ASSERT(class_id == kExternalTwoByteStringCid); | 10647 ASSERT(class_id == kExternalTwoByteStringCid); |
10648 return *ExternalTwoByteString::CharAddr(*this, index); | 10648 return *ExternalTwoByteString::CharAddr(*this, index); |
10649 } | 10649 } |
10650 | 10650 |
10651 | 10651 |
10652 intptr_t String::CharSize() const { | 10652 intptr_t String::CharSize() const { |
10653 intptr_t class_id = raw()->GetClassId(); | 10653 intptr_t class_id = raw()->GetClassId(); |
10654 if (class_id == kOneByteStringCid || class_id == kExternalOneByteStringCid) { | 10654 if (class_id == kOneByteStringCid || class_id == kExternalOneByteStringCid) { |
10655 return kOneByteChar; | 10655 return kOneByteChar; |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
11108 result = OneByteString::New(length, space); | 11108 result = OneByteString::New(length, space); |
11109 } else { | 11109 } else { |
11110 result = TwoByteString::New(length, space); | 11110 result = TwoByteString::New(length, space); |
11111 } | 11111 } |
11112 String::Copy(result, 0, str, begin_index, length); | 11112 String::Copy(result, 0, str, begin_index, length); |
11113 return result.raw(); | 11113 return result.raw(); |
11114 } | 11114 } |
11115 | 11115 |
11116 | 11116 |
11117 const char* String::ToCString() const { | 11117 const char* String::ToCString() const { |
11118 intptr_t len = Utf8::Length(*this); | 11118 if (IsOneByteString()) { |
11119 // Quick conversion if OneByteString contains one byte characters. | |
siva
2013/02/22 21:47:09
... contains only ASCII characters.
srdjan
2013/02/22 22:27:37
Done.
| |
11120 intptr_t len = Length(); | |
11121 if (len == 0) { | |
11122 return ""; | |
11123 } | |
11124 Zone* zone = Isolate::Current()->current_zone(); | |
11125 uint8_t* result = zone->Alloc<uint8_t>(len + 1); | |
11126 const uint8_t* original_str = OneByteString::CharAddr(*this, 0); | |
siva
2013/02/22 21:47:09
Since you are using the raw pointer here in a loop
srdjan
2013/02/22 22:27:37
Done.
| |
11127 for (intptr_t i = 0; i < len; i++) { | |
11128 if (original_str[i] <= Utf8::kMaxOneByteChar) { | |
11129 result[i] = original_str[i]; | |
11130 } else { | |
11131 len = -1; | |
11132 break; | |
11133 } | |
11134 } | |
11135 if (len > 0) { | |
11136 result[len] = 0; | |
11137 return reinterpret_cast<const char*>(result); | |
11138 } | |
11139 } | |
11140 const intptr_t len = Utf8::Length(*this); | |
11119 Zone* zone = Isolate::Current()->current_zone(); | 11141 Zone* zone = Isolate::Current()->current_zone(); |
11120 uint8_t* result = zone->Alloc<uint8_t>(len + 1); | 11142 uint8_t* result = zone->Alloc<uint8_t>(len + 1); |
11121 ToUTF8(result, len); | 11143 ToUTF8(result, len); |
11122 result[len] = 0; | 11144 result[len] = 0; |
11123 return reinterpret_cast<const char*>(result); | 11145 return reinterpret_cast<const char*>(result); |
11124 } | 11146 } |
11125 | 11147 |
11126 | 11148 |
11127 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const { | 11149 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const { |
11128 ASSERT(array_len >= Utf8::Length(*this)); | 11150 ASSERT(array_len >= Utf8::Length(*this)); |
(...skipping 1772 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
12901 } | 12923 } |
12902 return result.raw(); | 12924 return result.raw(); |
12903 } | 12925 } |
12904 | 12926 |
12905 | 12927 |
12906 const char* WeakProperty::ToCString() const { | 12928 const char* WeakProperty::ToCString() const { |
12907 return "_WeakProperty"; | 12929 return "_WeakProperty"; |
12908 } | 12930 } |
12909 | 12931 |
12910 } // namespace dart | 12932 } // namespace dart |
OLD | NEW |