diff options
author | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-10 22:18:47 +0000 |
---|---|---|
committer | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-10 22:18:47 +0000 |
commit | 3e9fe53e8e10628ba4432e50ea114200757ffe40 (patch) | |
tree | 4d7f8df513db02c3a4557c52d2f55568344c50b6 /base/values.cc | |
parent | fc6ff4229344bf1a799a13c8aa651c657c2e9bbd (diff) | |
download | chromium_src-3e9fe53e8e10628ba4432e50ea114200757ffe40.zip chromium_src-3e9fe53e8e10628ba4432e50ea114200757ffe40.tar.gz chromium_src-3e9fe53e8e10628ba4432e50ea114200757ffe40.tar.bz2 |
Added std::string to Value via Set/GetString overloading.
All input is converted to UTF-8. Output is converted back to wide during std::wstring version of GetString().
This is part one of some more patches to come where I switch clients over to UTF-8 strings.
Review URL: http://codereview.chromium.org/13230
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6740 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/values.cc')
-rw-r--r-- | base/values.cc | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/base/values.cc b/base/values.cc index dd8062b..b0160e1 100644 --- a/base/values.cc +++ b/base/values.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "base/logging.h" +#include "base/string_util.h" #include "base/values.h" ///////////////////// Value //////////////////// @@ -31,11 +32,16 @@ Value* Value::CreateRealValue(double in_value) { } // static -Value* Value::CreateStringValue(const std::wstring& in_value) { +Value* Value::CreateStringValue(const std::string& in_value) { return new StringValue(in_value); } // static +Value* Value::CreateStringValue(const std::wstring& in_value) { + return new StringValue(WideToUTF8(in_value)); +} + +// static BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) { return BinaryValue::Create(buffer, size); } @@ -52,6 +58,10 @@ bool Value::GetAsReal(double* in_value) const { return false; } +bool Value::GetAsString(std::string* in_value) const { + return false; +} + bool Value::GetAsString(std::wstring* in_value) const { return false; } @@ -135,15 +145,27 @@ bool FundamentalValue::Equals(const Value* other) const { ///////////////////// StringValue //////////////////// +StringValue::StringValue(const std::string& in_value) + : Value(TYPE_STRING), + value_(in_value) { + DCHECK(IsStringUTF8(in_value)); +} + StringValue::~StringValue() { } -bool StringValue::GetAsString(std::wstring* out_value) const { +bool StringValue::GetAsString(std::string* out_value) const { if (out_value) *out_value = value_; return true; } +bool StringValue::GetAsString(std::wstring* out_value) const { + if (out_value) + *out_value = UTF8ToWide(value_); + return true; +} + Value* StringValue::DeepCopy() const { return CreateStringValue(value_); } @@ -151,7 +173,7 @@ Value* StringValue::DeepCopy() const { bool StringValue::Equals(const Value* other) const { if (other->GetType() != GetType()) return false; - std::wstring lhs, rhs; + std::string lhs, rhs; return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; } @@ -276,6 +298,11 @@ bool DictionaryValue::SetReal(const std::wstring& path, double in_value) { } bool DictionaryValue::SetString(const std::wstring& path, + const std::string& in_value) { + return Set(path, CreateStringValue(in_value)); +} + +bool DictionaryValue::SetString(const std::wstring& path, const std::wstring& in_value) { return Set(path, CreateStringValue(in_value)); } @@ -335,6 +362,15 @@ bool DictionaryValue::GetReal(const std::wstring& path, } bool DictionaryValue::GetString(const std::wstring& path, + std::string* out_value) const { + Value* value; + if (!Get(path, &value)) + return false; + + return value->GetAsString(out_value); +} + +bool DictionaryValue::GetString(const std::wstring& path, std::wstring* out_value) const { Value* value; if (!Get(path, &value)) |