Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: tools/gn/value.cc

Issue 995393005: tools/gn: fix escaping of backslashes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tools/gn/value_unittest.cc » ('j') | tools/gn/value_unittest.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 escaped = string_value_; 127 std::string result = "\"";
128 // First escape all special uses of a backslash. 128 bool escaped = false;
brettw 2015/03/31 23:01:51 The goal of tracking this should be documented, it
mdempsky 2015/03/31 23:14:47 Done, PTAL.
129 ReplaceSubstringsAfterOffset(&escaped, 0, "\\$", "\\\\$"); 129 for (char ch : string_value_) {
130 ReplaceSubstringsAfterOffset(&escaped, 0, "\\\"", "\\\\\""); 130 if (escaped && (ch == '$' || ch == '"' || ch == '\\'))
131 131 result += '\\';
132 // Now escape special chars. 132 if (ch == '$' || ch == '"')
133 ReplaceSubstringsAfterOffset(&escaped, 0, "$", "\\$"); 133 result += '\\';
134 ReplaceSubstringsAfterOffset(&escaped, 0, "\"", "\\\""); 134 result += ch;
135 return "\"" + escaped + "\""; 135 escaped = (ch == '\\');
136 }
137 if (escaped)
138 result += '\\';
139 result += '"';
140 return result;
136 } 141 }
137 return string_value_; 142 return string_value_;
138 case LIST: { 143 case LIST: {
139 std::string result = "["; 144 std::string result = "[";
140 for (size_t i = 0; i < list_value_.size(); i++) { 145 for (size_t i = 0; i < list_value_.size(); i++) {
141 if (i > 0) 146 if (i > 0)
142 result += ", "; 147 result += ", ";
143 result += list_value_[i].ToString(true); 148 result += list_value_[i].ToString(true);
144 } 149 }
145 result.push_back(']'); 150 result.push_back(']');
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 // iteration code. 205 // iteration code.
201 return false; 206 return false;
202 default: 207 default:
203 return false; 208 return false;
204 } 209 }
205 } 210 }
206 211
207 bool Value::operator!=(const Value& other) const { 212 bool Value::operator!=(const Value& other) const {
208 return !operator==(other); 213 return !operator==(other);
209 } 214 }
OLDNEW
« no previous file with comments | « no previous file | tools/gn/value_unittest.cc » ('j') | tools/gn/value_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698