Chromium Code Reviews| Index: runtime/vm/object.cc |
| =================================================================== |
| --- runtime/vm/object.cc (revision 18861) |
| +++ runtime/vm/object.cc (working copy) |
| @@ -10636,13 +10636,13 @@ |
| ASSERT(RawObject::IsStringClassId(class_id)); |
| NoGCScope no_gc; |
| if (class_id == kOneByteStringCid) { |
| - return *OneByteString::CharAddr(*this, index); |
| + return *OneByteString::CharAddr(*this, index); |
| } |
| if (class_id == kTwoByteStringCid) { |
| - return *TwoByteString::CharAddr(*this, index); |
| + return *TwoByteString::CharAddr(*this, index); |
| } |
| if (class_id == kExternalOneByteStringCid) { |
| - return *ExternalOneByteString::CharAddr(*this, index); |
| + return *ExternalOneByteString::CharAddr(*this, index); |
| } |
| ASSERT(class_id == kExternalTwoByteStringCid); |
| return *ExternalTwoByteString::CharAddr(*this, index); |
| @@ -11115,7 +11115,29 @@ |
| const char* String::ToCString() const { |
| - intptr_t len = Utf8::Length(*this); |
| + if (IsOneByteString()) { |
| + // 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.
|
| + intptr_t len = Length(); |
| + if (len == 0) { |
| + return ""; |
| + } |
| + Zone* zone = Isolate::Current()->current_zone(); |
| + uint8_t* result = zone->Alloc<uint8_t>(len + 1); |
| + 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.
|
| + for (intptr_t i = 0; i < len; i++) { |
| + if (original_str[i] <= Utf8::kMaxOneByteChar) { |
| + result[i] = original_str[i]; |
| + } else { |
| + len = -1; |
| + break; |
| + } |
| + } |
| + if (len > 0) { |
| + result[len] = 0; |
| + return reinterpret_cast<const char*>(result); |
| + } |
| + } |
| + const intptr_t len = Utf8::Length(*this); |
| Zone* zone = Isolate::Current()->current_zone(); |
| uint8_t* result = zone->Alloc<uint8_t>(len + 1); |
| ToUTF8(result, len); |