| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "tools/gn/value.h" | 5 #include "tools/gn/value.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "tools/gn/scope.h" | 9 #include "tools/gn/scope.h" |
| 10 | 10 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 std::string Value::ToString(bool quote_string) const { | 117 std::string Value::ToString(bool quote_string) const { |
| 118 switch (type_) { | 118 switch (type_) { |
| 119 case NONE: | 119 case NONE: |
| 120 return "<void>"; | 120 return "<void>"; |
| 121 case BOOLEAN: | 121 case BOOLEAN: |
| 122 return boolean_value_ ? "true" : "false"; | 122 return boolean_value_ ? "true" : "false"; |
| 123 case INTEGER: | 123 case INTEGER: |
| 124 return base::Int64ToString(int_value_); | 124 return base::Int64ToString(int_value_); |
| 125 case STRING: | 125 case STRING: |
| 126 if (quote_string) { | 126 if (quote_string) { |
| 127 std::string result = "\""; | 127 std::string escaped = string_value_; |
| 128 bool hanging_backslash = false; | 128 // First escape all special uses of a backslash. |
| 129 for (char ch : string_value_) { | 129 ReplaceSubstringsAfterOffset(&escaped, 0, "\\$", "\\\\$"); |
| 130 // If the last character was a literal backslash and the next | 130 ReplaceSubstringsAfterOffset(&escaped, 0, "\\\"", "\\\\\""); |
| 131 // character could form a valid escape sequence, we need to insert | 131 |
| 132 // an extra backslash to prevent that. | 132 // Now escape special chars. |
| 133 if (hanging_backslash && (ch == '$' || ch == '"' || ch == '\\')) | 133 ReplaceSubstringsAfterOffset(&escaped, 0, "$", "\\$"); |
| 134 result += '\\'; | 134 ReplaceSubstringsAfterOffset(&escaped, 0, "\"", "\\\""); |
| 135 // If the next character is a dollar sign or double quote, it needs | 135 return "\"" + escaped + "\""; |
| 136 // to be escaped; otherwise it can be printed as is. | |
| 137 if (ch == '$' || ch == '"') | |
| 138 result += '\\'; | |
| 139 result += ch; | |
| 140 hanging_backslash = (ch == '\\'); | |
| 141 } | |
| 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; | |
| 148 } | 136 } |
| 149 return string_value_; | 137 return string_value_; |
| 150 case LIST: { | 138 case LIST: { |
| 151 std::string result = "["; | 139 std::string result = "["; |
| 152 for (size_t i = 0; i < list_value_.size(); i++) { | 140 for (size_t i = 0; i < list_value_.size(); i++) { |
| 153 if (i > 0) | 141 if (i > 0) |
| 154 result += ", "; | 142 result += ", "; |
| 155 result += list_value_[i].ToString(true); | 143 result += list_value_[i].ToString(true); |
| 156 } | 144 } |
| 157 result.push_back(']'); | 145 result.push_back(']'); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // iteration code. | 200 // iteration code. |
| 213 return false; | 201 return false; |
| 214 default: | 202 default: |
| 215 return false; | 203 return false; |
| 216 } | 204 } |
| 217 } | 205 } |
| 218 | 206 |
| 219 bool Value::operator!=(const Value& other) const { | 207 bool Value::operator!=(const Value& other) const { |
| 220 return !operator==(other); | 208 return !operator==(other); |
| 221 } | 209 } |
| OLD | NEW |