diff options
author | Luis Hector Chavez <lhchavez@google.com> | 2016-05-31 23:50:26 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-05-31 23:50:26 +0000 |
commit | 50a175dc8c72fd300b59510dfe308e0ebb2d26ad (patch) | |
tree | dd9041de1b74c6133fb3a07fcaa321de989b9668 /third_party/chromium/base/json/json_parser.cc | |
parent | fa44197670399e2f5fd61ff7baa15fa13a793bcc (diff) | |
parent | 24b97cc306d4834f865f9599b379e73e1b326ab8 (diff) | |
download | libweave-50a175dc8c72fd300b59510dfe308e0ebb2d26ad.tar.gz |
Revert "Merge remote-tracking branch \'weave/master\' into \'weave/aosp-master\'" am: abfe7a063b am: d77dfa0b5a
am: 24b97cc306
* commit '24b97cc306d4834f865f9599b379e73e1b326ab8':
Revert "Merge remote-tracking branch 'weave/master' into 'weave/aosp-master'"
Change-Id: I9aad4853ea9f419c4234a0b7028608ef87aec82e
Diffstat (limited to 'third_party/chromium/base/json/json_parser.cc')
-rw-r--r-- | third_party/chromium/base/json/json_parser.cc | 82 |
1 files changed, 38 insertions, 44 deletions
diff --git a/third_party/chromium/base/json/json_parser.cc b/third_party/chromium/base/json/json_parser.cc index 708965a..304a7bd 100644 --- a/third_party/chromium/base/json/json_parser.cc +++ b/third_party/chromium/base/json/json_parser.cc @@ -5,11 +5,10 @@ #include "base/json/json_parser.h" #include <cmath> -#include <utility> #include "base/logging.h" #include "base/macros.h" -#include "base/memory/ptr_util.h" +#include "base/memory/scoped_ptr.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_piece.h" #include "base/strings/string_util.h" @@ -27,19 +26,16 @@ const int kStackMaxDepth = 100; const int32_t kExtendedASCIIStart = 0x80; -// DictionaryHiddenRootValue and ListHiddenRootValue are used in conjunction -// with JSONStringValue as an optimization for reducing the number of string -// copies. When this optimization is active, the parser uses a hidden root to -// keep the original JSON input string live and creates JSONStringValue children -// holding StringPiece references to the input string, avoiding about 2/3rds of -// string memory copies. The real root value is Swap()ed into the new instance. +// This and the class below are used to own the JSON input string for when +// string tokens are stored as StringPiece instead of std::string. This +// optimization avoids about 2/3rds of string memory copies. The constructor +// takes ownership of the input string. The real root value is Swap()ed into +// the new instance. class DictionaryHiddenRootValue : public DictionaryValue { public: - DictionaryHiddenRootValue(std::unique_ptr<std::string> json, - std::unique_ptr<Value> root) - : json_(std::move(json)) { + DictionaryHiddenRootValue(std::string* json, Value* root) : json_(json) { DCHECK(root->IsType(Value::TYPE_DICTIONARY)); - DictionaryValue::Swap(static_cast<DictionaryValue*>(root.get())); + DictionaryValue::Swap(static_cast<DictionaryValue*>(root)); } void Swap(DictionaryValue* other) override { @@ -47,7 +43,7 @@ class DictionaryHiddenRootValue : public DictionaryValue { // First deep copy to convert JSONStringValue to std::string and swap that // copy with |other|, which contains the new contents of |this|. - std::unique_ptr<DictionaryValue> copy(CreateDeepCopy()); + scoped_ptr<DictionaryValue> copy(DeepCopy()); copy->Swap(other); // Then erase the contents of the current dictionary and swap in the @@ -61,7 +57,7 @@ class DictionaryHiddenRootValue : public DictionaryValue { // the method below. bool RemoveWithoutPathExpansion(const std::string& key, - std::unique_ptr<Value>* out) override { + scoped_ptr<Value>* out) override { // If the caller won't take ownership of the removed value, just call up. if (!out) return DictionaryValue::RemoveWithoutPathExpansion(key, out); @@ -70,28 +66,26 @@ class DictionaryHiddenRootValue : public DictionaryValue { // Otherwise, remove the value while its still "owned" by this and copy it // to convert any JSONStringValues to std::string. - std::unique_ptr<Value> out_owned; + scoped_ptr<Value> out_owned; if (!DictionaryValue::RemoveWithoutPathExpansion(key, &out_owned)) return false; - *out = out_owned->CreateDeepCopy(); + out->reset(out_owned->DeepCopy()); return true; } private: - std::unique_ptr<std::string> json_; + scoped_ptr<std::string> json_; DISALLOW_COPY_AND_ASSIGN(DictionaryHiddenRootValue); }; class ListHiddenRootValue : public ListValue { public: - ListHiddenRootValue(std::unique_ptr<std::string> json, - std::unique_ptr<Value> root) - : json_(std::move(json)) { + ListHiddenRootValue(std::string* json, Value* root) : json_(json) { DCHECK(root->IsType(Value::TYPE_LIST)); - ListValue::Swap(static_cast<ListValue*>(root.get())); + ListValue::Swap(static_cast<ListValue*>(root)); } void Swap(ListValue* other) override { @@ -99,7 +93,7 @@ class ListHiddenRootValue : public ListValue { // First deep copy to convert JSONStringValue to std::string and swap that // copy with |other|, which contains the new contents of |this|. - std::unique_ptr<ListValue> copy(CreateDeepCopy()); + scoped_ptr<ListValue> copy(DeepCopy()); copy->Swap(other); // Then erase the contents of the current list and swap in the new contents, @@ -109,7 +103,7 @@ class ListHiddenRootValue : public ListValue { ListValue::Swap(copy.get()); } - bool Remove(size_t index, std::unique_ptr<Value>* out) override { + bool Remove(size_t index, scoped_ptr<Value>* out) override { // If the caller won't take ownership of the removed value, just call up. if (!out) return ListValue::Remove(index, out); @@ -118,17 +112,17 @@ class ListHiddenRootValue : public ListValue { // Otherwise, remove the value while its still "owned" by this and copy it // to convert any JSONStringValues to std::string. - std::unique_ptr<Value> out_owned; + scoped_ptr<Value> out_owned; if (!ListValue::Remove(index, &out_owned)) return false; - *out = out_owned->CreateDeepCopy(); + out->reset(out_owned->DeepCopy()); return true; } private: - std::unique_ptr<std::string> json_; + scoped_ptr<std::string> json_; DISALLOW_COPY_AND_ASSIGN(ListHiddenRootValue); }; @@ -138,8 +132,10 @@ class ListHiddenRootValue : public ListValue { // otherwise the referenced string will not be guaranteed to outlive it. class JSONStringValue : public Value { public: - explicit JSONStringValue(StringPiece piece) - : Value(TYPE_STRING), string_piece_(piece) {} + explicit JSONStringValue(const StringPiece& piece) + : Value(TYPE_STRING), + string_piece_(piece) { + } // Overridden from Value: bool GetAsString(std::string* out_value) const override { @@ -202,13 +198,13 @@ JSONParser::JSONParser(int options) JSONParser::~JSONParser() { } -std::unique_ptr<Value> JSONParser::Parse(StringPiece input) { - std::unique_ptr<std::string> input_copy; +Value* JSONParser::Parse(const StringPiece& input) { + scoped_ptr<std::string> input_copy; // If the children of a JSON root can be detached, then hidden roots cannot // be used, so do not bother copying the input because StringPiece will not // be used anywhere. if (!(options_ & JSON_DETACHABLE_CHILDREN)) { - input_copy = WrapUnique(new std::string(input.as_string())); + input_copy.reset(new std::string(input.as_string())); start_pos_ = input_copy->data(); } else { start_pos_ = input.data(); @@ -234,15 +230,15 @@ std::unique_ptr<Value> JSONParser::Parse(StringPiece input) { } // Parse the first and any nested tokens. - std::unique_ptr<Value> root(ParseNextToken()); - if (!root) - return nullptr; + scoped_ptr<Value> root(ParseNextToken()); + if (!root.get()) + return NULL; // Make sure the input stream is at an end. if (GetNextToken() != T_END_OF_INPUT) { if (!CanConsume(1) || (NextChar() && GetNextToken() != T_END_OF_INPUT)) { ReportError(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, 1); - return nullptr; + return NULL; } } @@ -250,21 +246,19 @@ std::unique_ptr<Value> JSONParser::Parse(StringPiece input) { // hidden root. if (!(options_ & JSON_DETACHABLE_CHILDREN)) { if (root->IsType(Value::TYPE_DICTIONARY)) { - return WrapUnique(new DictionaryHiddenRootValue(std::move(input_copy), - std::move(root))); + return new DictionaryHiddenRootValue(input_copy.release(), root.get()); } else if (root->IsType(Value::TYPE_LIST)) { - return WrapUnique( - new ListHiddenRootValue(std::move(input_copy), std::move(root))); + return new ListHiddenRootValue(input_copy.release(), root.get()); } else if (root->IsType(Value::TYPE_STRING)) { // A string type could be a JSONStringValue, but because there's no // corresponding HiddenRootValue, the memory will be lost. Deep copy to // preserve it. - return root->CreateDeepCopy(); + return root->DeepCopy(); } } // All other values can be returned directly. - return root; + return root.release(); } JSONReader::JsonParseError JSONParser::error_code() const { @@ -310,7 +304,7 @@ JSONParser::StringBuilder::~StringBuilder() { void JSONParser::StringBuilder::Append(const char& c) { DCHECK_GE(c, 0); - DCHECK_LT(static_cast<unsigned char>(c), 128); + DCHECK_LT(c, 128); if (string_) string_->push_back(c); @@ -500,7 +494,7 @@ Value* JSONParser::ConsumeDictionary() { return NULL; } - std::unique_ptr<DictionaryValue> dict(new DictionaryValue); + scoped_ptr<DictionaryValue> dict(new DictionaryValue); NextChar(); Token token = GetNextToken(); @@ -564,7 +558,7 @@ Value* JSONParser::ConsumeList() { return NULL; } - std::unique_ptr<ListValue> list(new ListValue); + scoped_ptr<ListValue> list(new ListValue); NextChar(); Token token = GetNextToken(); |