| 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/symbols.h" | 5 #include "vm/symbols.h" |
| 6 | 6 |
| 7 #include "vm/handles.h" | 7 #include "vm/handles.h" |
| 8 #include "vm/handles_impl.h" | 8 #include "vm/handles_impl.h" |
| 9 #include "vm/hash_table.h" | 9 #include "vm/hash_table.h" |
| 10 #include "vm/isolate.h" | 10 #include "vm/isolate.h" |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 } else { | 390 } else { |
| 391 return NewSymbol(ConcatString(str1, str2)); | 391 return NewSymbol(ConcatString(str1, str2)); |
| 392 } | 392 } |
| 393 } | 393 } |
| 394 | 394 |
| 395 | 395 |
| 396 // TODO(srdjan): If this becomes performance critical code, consider looking | 396 // TODO(srdjan): If this becomes performance critical code, consider looking |
| 397 // up symbol from pieces instead of concatenating them first into a big string. | 397 // up symbol from pieces instead of concatenating them first into a big string. |
| 398 RawString* Symbols::FromConcatAll( | 398 RawString* Symbols::FromConcatAll( |
| 399 const GrowableHandlePtrArray<const String>& strs) { | 399 const GrowableHandlePtrArray<const String>& strs) { |
| 400 GrowableArray<const char*> cchars(strs.length()); | 400 const intptr_t strs_length = strs.length(); |
| 401 GrowableArray<intptr_t> lengths(strs.length()); | 401 GrowableArray<intptr_t> lengths(strs_length); |
| 402 |
| 402 intptr_t len_sum = 0; | 403 intptr_t len_sum = 0; |
| 403 for (intptr_t i = 0; i < strs.length(); i++) { | 404 bool is_one_byte_string = true; |
| 404 const char* to_cstr = strs[i].ToCString(); | 405 for (intptr_t i = 0; i < strs_length; i++) { |
| 405 intptr_t len = strlen(to_cstr); | 406 const String& str = strs[i]; |
| 406 cchars.Add(to_cstr); | 407 const intptr_t str_len = str.Length(); |
| 407 lengths.Add(len); | 408 if ((String::kMaxElements - len_sum) < str_len) { |
| 408 len_sum += len; | 409 Exceptions::ThrowOOM(); |
| 410 UNREACHABLE(); |
| 411 } |
| 412 len_sum += str_len; |
| 413 lengths.Add(str_len); |
| 414 if (!str.IsOneByteString()) { |
| 415 is_one_byte_string = false; |
| 416 } |
| 409 } | 417 } |
| 410 | 418 |
| 411 Zone* zone = Thread::Current()->zone(); | 419 Zone* zone = Thread::Current()->zone(); |
| 412 char* buffer = zone->Alloc<char>(len_sum); | 420 |
| 413 const char* const orig_buffer = buffer; | 421 if (is_one_byte_string) { |
| 414 for (intptr_t i = 0; i < cchars.length(); i++) { | 422 uint8_t* buffer = zone->Alloc<uint8_t>(len_sum); |
| 415 intptr_t len = lengths[i]; | 423 const uint8_t* const orig_buffer = buffer; |
| 416 strncpy(buffer, cchars[i], len); | 424 for (intptr_t i = 0; i < strs_length; i++) { |
| 417 buffer += len; | 425 NoSafepointScope no_safepoint; |
| 426 intptr_t str_len = lengths[i]; |
| 427 const String& str = strs[i]; |
| 428 memmove(buffer, OneByteString::CharAddr(str, 0), str_len); |
| 429 buffer += str_len; |
| 430 } |
| 431 ASSERT(len_sum == buffer - orig_buffer); |
| 432 return Symbols::FromLatin1(orig_buffer, len_sum); |
| 433 } else { |
| 434 uint16_t* buffer = zone->Alloc<uint16_t>(len_sum); |
| 435 const uint16_t* const orig_buffer = buffer; |
| 436 for (intptr_t i = 0; i < strs_length; i++) { |
| 437 NoSafepointScope no_safepoint; |
| 438 intptr_t str_len = lengths[i]; |
| 439 const String& str = strs[i]; |
| 440 if (str.IsTwoByteString()) { |
| 441 memmove(buffer, TwoByteString::CharAddr(str, 0), str_len * 2); |
| 442 } else { |
| 443 uint8_t* src_p = OneByteString::CharAddr(str, 0); |
| 444 for (int n = 0; n < str_len; n++) { |
| 445 buffer[n] = src_p[n]; |
| 446 } |
| 447 } |
| 448 buffer += str_len; |
| 449 } |
| 450 ASSERT(len_sum == buffer - orig_buffer); |
| 451 return Symbols::FromUTF16(orig_buffer, len_sum); |
| 418 } | 452 } |
| 419 ASSERT(len_sum == buffer - orig_buffer); | |
| 420 return Symbols::New(orig_buffer, len_sum); | |
| 421 } | 453 } |
| 422 | 454 |
| 423 | 455 |
| 424 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array. | 456 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array. |
| 425 template<typename StringType> | 457 template<typename StringType> |
| 426 RawString* Symbols::NewSymbol(const StringType& str) { | 458 RawString* Symbols::NewSymbol(const StringType& str) { |
| 427 Thread* thread = Thread::Current(); | 459 Thread* thread = Thread::Current(); |
| 428 Isolate* isolate = thread->isolate(); | 460 Isolate* isolate = thread->isolate(); |
| 429 Zone* zone = thread->zone(); | 461 Zone* zone = thread->zone(); |
| 430 String& symbol = String::Handle(zone); | 462 String& symbol = String::Handle(zone); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 529 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 498 ASSERT(IsVMSymbolId(object_id)); | 530 ASSERT(IsVMSymbolId(object_id)); |
| 499 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 531 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 500 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { | 532 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { |
| 501 return symbol_handles_[i]->raw(); | 533 return symbol_handles_[i]->raw(); |
| 502 } | 534 } |
| 503 return Object::null(); | 535 return Object::null(); |
| 504 } | 536 } |
| 505 | 537 |
| 506 } // namespace dart | 538 } // namespace dart |
| OLD | NEW |