OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/values.h" | 5 #include "base/values.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 } | 383 } |
384 | 384 |
385 void DictionaryValue::Clear() { | 385 void DictionaryValue::Clear() { |
386 dictionary_.clear(); | 386 dictionary_.clear(); |
387 } | 387 } |
388 | 388 |
389 void DictionaryValue::Set(StringPiece path, std::unique_ptr<Value> in_value) { | 389 void DictionaryValue::Set(StringPiece path, std::unique_ptr<Value> in_value) { |
390 DCHECK(IsStringUTF8(path)); | 390 DCHECK(IsStringUTF8(path)); |
391 DCHECK(in_value); | 391 DCHECK(in_value); |
392 | 392 |
393 std::string current_path(path.as_string()); | 393 StringPiece current_path(path); |
394 DictionaryValue* current_dictionary = this; | 394 DictionaryValue* current_dictionary = this; |
395 for (size_t delimiter_position = current_path.find('.'); | 395 for (size_t delimiter_position = current_path.find('.'); |
396 delimiter_position != std::string::npos; | 396 delimiter_position != StringPiece::npos; |
397 delimiter_position = current_path.find('.')) { | 397 delimiter_position = current_path.find('.')) { |
398 // Assume that we're indexing into a dictionary. | 398 // Assume that we're indexing into a dictionary. |
399 std::string key(current_path, 0, delimiter_position); | 399 StringPiece key = current_path.substr(0, delimiter_position); |
400 DictionaryValue* child_dictionary = NULL; | 400 DictionaryValue* child_dictionary = NULL; |
401 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { | 401 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { |
402 child_dictionary = new DictionaryValue; | 402 child_dictionary = new DictionaryValue; |
403 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); | 403 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); |
404 } | 404 } |
405 | 405 |
406 current_dictionary = child_dictionary; | 406 current_dictionary = child_dictionary; |
407 current_path.erase(0, delimiter_position + 1); | 407 current_path = current_path.substr(delimiter_position + 1); |
408 } | 408 } |
409 | 409 |
410 current_dictionary->SetWithoutPathExpansion(current_path, | 410 current_dictionary->SetWithoutPathExpansion(current_path, |
411 std::move(in_value)); | 411 std::move(in_value)); |
412 } | 412 } |
413 | 413 |
414 void DictionaryValue::Set(StringPiece path, Value* in_value) { | 414 void DictionaryValue::Set(StringPiece path, Value* in_value) { |
415 Set(path, WrapUnique(in_value)); | 415 Set(path, WrapUnique(in_value)); |
416 } | 416 } |
417 | 417 |
418 void DictionaryValue::SetBoolean(StringPiece path, bool in_value) { | 418 void DictionaryValue::SetBoolean(StringPiece path, bool in_value) { |
419 Set(path, new FundamentalValue(in_value)); | 419 Set(path, new FundamentalValue(in_value)); |
420 } | 420 } |
421 | 421 |
422 void DictionaryValue::SetInteger(StringPiece path, int in_value) { | 422 void DictionaryValue::SetInteger(StringPiece path, int in_value) { |
423 Set(path, new FundamentalValue(in_value)); | 423 Set(path, new FundamentalValue(in_value)); |
424 } | 424 } |
425 | 425 |
426 void DictionaryValue::SetDouble(StringPiece path, double in_value) { | 426 void DictionaryValue::SetDouble(StringPiece path, double in_value) { |
427 Set(path, new FundamentalValue(in_value)); | 427 Set(path, new FundamentalValue(in_value)); |
428 } | 428 } |
429 | 429 |
430 void DictionaryValue::SetString(StringPiece path, StringPiece in_value) { | 430 void DictionaryValue::SetString(StringPiece path, StringPiece in_value) { |
431 Set(path, new StringValue(in_value.as_string())); | 431 Set(path, new StringValue(in_value)); |
432 } | 432 } |
433 | 433 |
434 void DictionaryValue::SetString(StringPiece path, const string16& in_value) { | 434 void DictionaryValue::SetString(StringPiece path, const string16& in_value) { |
435 Set(path, new StringValue(in_value)); | 435 Set(path, new StringValue(in_value)); |
436 } | 436 } |
437 | 437 |
438 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, | 438 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, |
439 std::unique_ptr<Value> in_value) { | 439 std::unique_ptr<Value> in_value) { |
440 dictionary_[key.as_string()] = std::move(in_value); | 440 dictionary_[key.as_string()] = std::move(in_value); |
441 } | 441 } |
(...skipping 13 matching lines...) Expand all Loading... |
455 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 455 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
456 } | 456 } |
457 | 457 |
458 void DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, | 458 void DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, |
459 double in_value) { | 459 double in_value) { |
460 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 460 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
461 } | 461 } |
462 | 462 |
463 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, | 463 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, |
464 StringPiece in_value) { | 464 StringPiece in_value) { |
465 SetWithoutPathExpansion(path, new StringValue(in_value.as_string())); | 465 SetWithoutPathExpansion(path, new StringValue(in_value)); |
466 } | 466 } |
467 | 467 |
468 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, | 468 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, |
469 const string16& in_value) { | 469 const string16& in_value) { |
470 SetWithoutPathExpansion(path, new StringValue(in_value)); | 470 SetWithoutPathExpansion(path, new StringValue(in_value)); |
471 } | 471 } |
472 | 472 |
473 bool DictionaryValue::Get(StringPiece path, | 473 bool DictionaryValue::Get(StringPiece path, |
474 const Value** out_value) const { | 474 const Value** out_value) const { |
475 DCHECK(IsStringUTF8(path)); | 475 DCHECK(IsStringUTF8(path)); |
476 StringPiece current_path(path); | 476 StringPiece current_path(path); |
477 const DictionaryValue* current_dictionary = this; | 477 const DictionaryValue* current_dictionary = this; |
478 for (size_t delimiter_position = current_path.find('.'); | 478 for (size_t delimiter_position = current_path.find('.'); |
479 delimiter_position != std::string::npos; | 479 delimiter_position != std::string::npos; |
480 delimiter_position = current_path.find('.')) { | 480 delimiter_position = current_path.find('.')) { |
481 const DictionaryValue* child_dictionary = NULL; | 481 const DictionaryValue* child_dictionary = NULL; |
482 if (!current_dictionary->GetDictionaryWithoutPathExpansion( | 482 if (!current_dictionary->GetDictionaryWithoutPathExpansion( |
483 current_path.substr(0, delimiter_position).as_string(), | 483 current_path.substr(0, delimiter_position), &child_dictionary)) { |
484 &child_dictionary)) { | |
485 return false; | 484 return false; |
486 } | 485 } |
487 | 486 |
488 current_dictionary = child_dictionary; | 487 current_dictionary = child_dictionary; |
489 current_path = current_path.substr(delimiter_position + 1); | 488 current_path = current_path.substr(delimiter_position + 1); |
490 } | 489 } |
491 | 490 |
492 return current_dictionary->GetWithoutPathExpansion(current_path.as_string(), | 491 return current_dictionary->GetWithoutPathExpansion(current_path, out_value); |
493 out_value); | |
494 } | 492 } |
495 | 493 |
496 bool DictionaryValue::Get(StringPiece path, Value** out_value) { | 494 bool DictionaryValue::Get(StringPiece path, Value** out_value) { |
497 return static_cast<const DictionaryValue&>(*this).Get( | 495 return static_cast<const DictionaryValue&>(*this).Get( |
498 path, | 496 path, |
499 const_cast<const Value**>(out_value)); | 497 const_cast<const Value**>(out_value)); |
500 } | 498 } |
501 | 499 |
502 bool DictionaryValue::GetBoolean(StringPiece path, bool* bool_value) const { | 500 bool DictionaryValue::GetBoolean(StringPiece path, bool* bool_value) const { |
503 const Value* value; | 501 const Value* value; |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
720 ListValue** out_value) { | 718 ListValue** out_value) { |
721 return | 719 return |
722 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( | 720 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( |
723 key, | 721 key, |
724 const_cast<const ListValue**>(out_value)); | 722 const_cast<const ListValue**>(out_value)); |
725 } | 723 } |
726 | 724 |
727 bool DictionaryValue::Remove(StringPiece path, | 725 bool DictionaryValue::Remove(StringPiece path, |
728 std::unique_ptr<Value>* out_value) { | 726 std::unique_ptr<Value>* out_value) { |
729 DCHECK(IsStringUTF8(path)); | 727 DCHECK(IsStringUTF8(path)); |
730 std::string current_path(path.as_string()); | 728 StringPiece current_path(path); |
731 DictionaryValue* current_dictionary = this; | 729 DictionaryValue* current_dictionary = this; |
732 size_t delimiter_position = current_path.rfind('.'); | 730 size_t delimiter_position = current_path.rfind('.'); |
733 if (delimiter_position != std::string::npos) { | 731 if (delimiter_position != StringPiece::npos) { |
734 if (!GetDictionary(current_path.substr(0, delimiter_position), | 732 if (!GetDictionary(current_path.substr(0, delimiter_position), |
735 ¤t_dictionary)) | 733 ¤t_dictionary)) |
736 return false; | 734 return false; |
737 current_path.erase(0, delimiter_position + 1); | 735 current_path = current_path.substr(delimiter_position + 1); |
738 } | 736 } |
739 | 737 |
740 return current_dictionary->RemoveWithoutPathExpansion(current_path, | 738 return current_dictionary->RemoveWithoutPathExpansion(current_path, |
741 out_value); | 739 out_value); |
742 } | 740 } |
743 | 741 |
744 bool DictionaryValue::RemoveWithoutPathExpansion( | 742 bool DictionaryValue::RemoveWithoutPathExpansion( |
745 StringPiece key, | 743 StringPiece key, |
746 std::unique_ptr<Value>* out_value) { | 744 std::unique_ptr<Value>* out_value) { |
747 DCHECK(IsStringUTF8(key)); | 745 DCHECK(IsStringUTF8(key)); |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1055 | 1053 |
1056 void ListValue::AppendInteger(int in_value) { | 1054 void ListValue::AppendInteger(int in_value) { |
1057 Append(MakeUnique<FundamentalValue>(in_value)); | 1055 Append(MakeUnique<FundamentalValue>(in_value)); |
1058 } | 1056 } |
1059 | 1057 |
1060 void ListValue::AppendDouble(double in_value) { | 1058 void ListValue::AppendDouble(double in_value) { |
1061 Append(MakeUnique<FundamentalValue>(in_value)); | 1059 Append(MakeUnique<FundamentalValue>(in_value)); |
1062 } | 1060 } |
1063 | 1061 |
1064 void ListValue::AppendString(StringPiece in_value) { | 1062 void ListValue::AppendString(StringPiece in_value) { |
1065 Append(MakeUnique<StringValue>(in_value.as_string())); | 1063 Append(MakeUnique<StringValue>(in_value)); |
1066 } | 1064 } |
1067 | 1065 |
1068 void ListValue::AppendString(const string16& in_value) { | 1066 void ListValue::AppendString(const string16& in_value) { |
1069 Append(MakeUnique<StringValue>(in_value)); | 1067 Append(MakeUnique<StringValue>(in_value)); |
1070 } | 1068 } |
1071 | 1069 |
1072 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { | 1070 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { |
1073 for (std::vector<std::string>::const_iterator it = in_values.begin(); | 1071 for (std::vector<std::string>::const_iterator it = in_values.begin(); |
1074 it != in_values.end(); ++it) { | 1072 it != in_values.end(); ++it) { |
1075 AppendString(*it); | 1073 AppendString(*it); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1164 ValueDeserializer::~ValueDeserializer() { | 1162 ValueDeserializer::~ValueDeserializer() { |
1165 } | 1163 } |
1166 | 1164 |
1167 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1165 std::ostream& operator<<(std::ostream& out, const Value& value) { |
1168 std::string json; | 1166 std::string json; |
1169 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); | 1167 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
1170 return out << json; | 1168 return out << json; |
1171 } | 1169 } |
1172 | 1170 |
1173 } // namespace base | 1171 } // namespace base |
OLD | NEW |