| 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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 if (str1.Length() == 0) { | 386 if (str1.Length() == 0) { |
| 387 return New(str2); | 387 return New(str2); |
| 388 } else if (str2.Length() == 0) { | 388 } else if (str2.Length() == 0) { |
| 389 return New(str1); | 389 return New(str1); |
| 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 |
| 397 // up symbol from pieces instead of concatenating them first into a big string. |
| 398 RawString* Symbols::FromConcatAll(const GrowableArray<const String*>& strs) { |
| 399 GrowableArray<const char*> cchars(strs.length()); |
| 400 GrowableArray<intptr_t> lengths(strs.length()); |
| 401 intptr_t len_sum = 0; |
| 402 for (intptr_t i = 0; i < strs.length(); i++) { |
| 403 const char* to_cstr = strs[i]->ToCString(); |
| 404 intptr_t len = strlen(to_cstr); |
| 405 cchars.Add(to_cstr); |
| 406 lengths.Add(len); |
| 407 len_sum += len; |
| 408 } |
| 409 |
| 410 Zone* zone = Thread::Current()->zone(); |
| 411 char* buffer = zone->Alloc<char>(len_sum); |
| 412 const char* const orig_buffer = buffer; |
| 413 for (intptr_t i = 0; i < cchars.length(); i++) { |
| 414 intptr_t len = lengths[i]; |
| 415 strncpy(buffer, cchars[i], len); |
| 416 buffer += len; |
| 417 } |
| 418 ASSERT(len_sum == buffer - orig_buffer); |
| 419 return Symbols::New(orig_buffer, len_sum); |
| 420 } |
| 421 |
| 422 |
| 396 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array. | 423 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array. |
| 397 template<typename StringType> | 424 template<typename StringType> |
| 398 RawString* Symbols::NewSymbol(const StringType& str) { | 425 RawString* Symbols::NewSymbol(const StringType& str) { |
| 399 Thread* thread = Thread::Current(); | 426 Thread* thread = Thread::Current(); |
| 400 Isolate* isolate = thread->isolate(); | 427 Isolate* isolate = thread->isolate(); |
| 401 Zone* zone = thread->zone(); | 428 Zone* zone = thread->zone(); |
| 402 String& symbol = String::Handle(zone); | 429 String& symbol = String::Handle(zone); |
| 403 { | 430 { |
| 404 Isolate* vm_isolate = Dart::vm_isolate(); | 431 Isolate* vm_isolate = Dart::vm_isolate(); |
| 405 SymbolTable table(zone, vm_isolate->object_store()->symbol_table()); | 432 SymbolTable table(zone, vm_isolate->object_store()->symbol_table()); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 496 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 470 ASSERT(IsVMSymbolId(object_id)); | 497 ASSERT(IsVMSymbolId(object_id)); |
| 471 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 498 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 472 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { | 499 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { |
| 473 return symbol_handles_[i]->raw(); | 500 return symbol_handles_[i]->raw(); |
| 474 } | 501 } |
| 475 return Object::null(); | 502 return Object::null(); |
| 476 } | 503 } |
| 477 | 504 |
| 478 } // namespace dart | 505 } // namespace dart |
| OLD | NEW |