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

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: typo Created 5 years, 8 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') | no next file with comments »
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 hanging_backslash = false;
129 ReplaceSubstringsAfterOffset(&escaped, 0, "\\$", "\\\\$"); 129 for (char ch : string_value_) {
130 ReplaceSubstringsAfterOffset(&escaped, 0, "\\\"", "\\\\\""); 130 // If the last character was a literal backslash and the next
131 131 // character could form a valid escape sequence, we need to insert
132 // Now escape special chars. 132 // an extra backslash to prevent that.
133 ReplaceSubstringsAfterOffset(&escaped, 0, "$", "\\$"); 133 if (hanging_backslash && (ch == '$' || ch == '"' || ch == '\\'))
134 ReplaceSubstringsAfterOffset(&escaped, 0, "\"", "\\\""); 134 result += '\\';
135 return "\"" + escaped + "\""; 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 == '\\');
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;
136 } 148 }
137 return string_value_; 149 return string_value_;
138 case LIST: { 150 case LIST: {
139 std::string result = "["; 151 std::string result = "[";
140 for (size_t i = 0; i < list_value_.size(); i++) { 152 for (size_t i = 0; i < list_value_.size(); i++) {
141 if (i > 0) 153 if (i > 0)
142 result += ", "; 154 result += ", ";
143 result += list_value_[i].ToString(true); 155 result += list_value_[i].ToString(true);
144 } 156 }
145 result.push_back(']'); 157 result.push_back(']');
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 // iteration code. 212 // iteration code.
201 return false; 213 return false;
202 default: 214 default:
203 return false; 215 return false;
204 } 216 }
205 } 217 }
206 218
207 bool Value::operator!=(const Value& other) const { 219 bool Value::operator!=(const Value& other) const {
208 return !operator==(other); 220 return !operator==(other);
209 } 221 }
OLDNEW
« no previous file with comments | « no previous file | tools/gn/value_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698