Add ICU message format support
[chromium-blink-merge.git] / tools / gn / value.cc
blob1a4215ce29e19569b344a5adf1507a603f8416b6
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/gn/value.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_util.h"
9 #include "tools/gn/scope.h"
11 Value::Value()
12 : type_(NONE),
13 boolean_value_(false),
14 int_value_(0),
15 origin_(nullptr) {
18 Value::Value(const ParseNode* origin, Type t)
19 : type_(t),
20 boolean_value_(false),
21 int_value_(0),
22 origin_(origin) {
25 Value::Value(const ParseNode* origin, bool bool_val)
26 : type_(BOOLEAN),
27 boolean_value_(bool_val),
28 int_value_(0),
29 origin_(origin) {
32 Value::Value(const ParseNode* origin, int64 int_val)
33 : type_(INTEGER),
34 boolean_value_(false),
35 int_value_(int_val),
36 origin_(origin) {
39 Value::Value(const ParseNode* origin, std::string str_val)
40 : type_(STRING),
41 string_value_(),
42 boolean_value_(false),
43 int_value_(0),
44 origin_(origin) {
45 string_value_.swap(str_val);
48 Value::Value(const ParseNode* origin, const char* str_val)
49 : type_(STRING),
50 string_value_(str_val),
51 boolean_value_(false),
52 int_value_(0),
53 origin_(origin) {
56 Value::Value(const ParseNode* origin, scoped_ptr<Scope> scope)
57 : type_(SCOPE),
58 string_value_(),
59 boolean_value_(false),
60 int_value_(0),
61 scope_value_(scope.Pass()),
62 origin_(origin) {
65 Value::Value(const Value& other)
66 : type_(other.type_),
67 string_value_(other.string_value_),
68 boolean_value_(other.boolean_value_),
69 int_value_(other.int_value_),
70 list_value_(other.list_value_),
71 origin_(other.origin_) {
72 if (type() == SCOPE && other.scope_value_.get())
73 scope_value_ = other.scope_value_->MakeClosure();
76 Value::~Value() {
79 Value& Value::operator=(const Value& other) {
80 type_ = other.type_;
81 string_value_ = other.string_value_;
82 boolean_value_ = other.boolean_value_;
83 int_value_ = other.int_value_;
84 list_value_ = other.list_value_;
85 if (type() == SCOPE && other.scope_value_.get())
86 scope_value_ = other.scope_value_->MakeClosure();
87 origin_ = other.origin_;
88 return *this;
91 // static
92 const char* Value::DescribeType(Type t) {
93 switch (t) {
94 case NONE:
95 return "none";
96 case BOOLEAN:
97 return "boolean";
98 case INTEGER:
99 return "integer";
100 case STRING:
101 return "string";
102 case LIST:
103 return "list";
104 case SCOPE:
105 return "scope";
106 default:
107 NOTREACHED();
108 return "UNKNOWN";
112 void Value::SetScopeValue(scoped_ptr<Scope> scope) {
113 DCHECK(type_ == SCOPE);
114 scope_value_ = scope.Pass();
117 std::string Value::ToString(bool quote_string) const {
118 switch (type_) {
119 case NONE:
120 return "<void>";
121 case BOOLEAN:
122 return boolean_value_ ? "true" : "false";
123 case INTEGER:
124 return base::Int64ToString(int_value_);
125 case STRING:
126 if (quote_string) {
127 std::string result = "\"";
128 bool hanging_backslash = false;
129 for (char ch : string_value_) {
130 // If the last character was a literal backslash and the next
131 // character could form a valid escape sequence, we need to insert
132 // an extra backslash to prevent that.
133 if (hanging_backslash && (ch == '$' || ch == '"' || ch == '\\'))
134 result += '\\';
135 // If the next character is a dollar sign or double quote, it needs
136 // to be escaped; otherwise it can be printed as is.
137 if (ch == '$' || ch == '"')
138 result += '\\';
139 result += ch;
140 hanging_backslash = (ch == '\\');
142 // Again, we need to prevent the closing double quotes from becoming
143 // an escape sequence.
144 if (hanging_backslash)
145 result += '\\';
146 result += '"';
147 return result;
149 return string_value_;
150 case LIST: {
151 std::string result = "[";
152 for (size_t i = 0; i < list_value_.size(); i++) {
153 if (i > 0)
154 result += ", ";
155 result += list_value_[i].ToString(true);
157 result.push_back(']');
158 return result;
160 case SCOPE: {
161 Scope::KeyValueMap scope_values;
162 scope_value_->GetCurrentScopeValues(&scope_values);
163 if (scope_values.empty())
164 return std::string("{ }");
166 std::string result = "{\n";
167 for (const auto& pair : scope_values) {
168 result += " " + pair.first.as_string() + " = " +
169 pair.second.ToString(true) + "\n";
171 result += "}";
173 return result;
176 return std::string();
179 bool Value::VerifyTypeIs(Type t, Err* err) const {
180 if (type_ == t)
181 return true;
183 *err = Err(origin(),
184 std::string("This is not a ") + DescribeType(t) + ".",
185 std::string("Instead I see a ") + DescribeType(type_) + " = " +
186 ToString(true));
187 return false;
190 bool Value::operator==(const Value& other) const {
191 if (type_ != other.type_)
192 return false;
194 switch (type_) {
195 case Value::BOOLEAN:
196 return boolean_value() == other.boolean_value();
197 case Value::INTEGER:
198 return int_value() == other.int_value();
199 case Value::STRING:
200 return string_value() == other.string_value();
201 case Value::LIST:
202 if (list_value().size() != other.list_value().size())
203 return false;
204 for (size_t i = 0; i < list_value().size(); i++) {
205 if (list_value()[i] != other.list_value()[i])
206 return false;
208 return true;
209 case Value::SCOPE:
210 // Scopes are always considered not equal because there's currently
211 // no use case for comparing them, and it requires a bunch of complex
212 // iteration code.
213 return false;
214 default:
215 return false;
219 bool Value::operator!=(const Value& other) const {
220 return !operator==(other);