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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/gn/value_unittest.cc » ('j') | tools/gn/value_unittest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/value.cc
diff --git a/tools/gn/value.cc b/tools/gn/value.cc
index 6dfe23417a4a4d6ee3183e613561bc7f434b1c18..687c0396db325e551edb43781efe9ae1ff5cf185 100644
--- a/tools/gn/value.cc
+++ b/tools/gn/value.cc
@@ -124,15 +124,20 @@ std::string Value::ToString(bool quote_string) const {
return base::Int64ToString(int_value_);
case STRING:
if (quote_string) {
- std::string escaped = string_value_;
- // First escape all special uses of a backslash.
- ReplaceSubstringsAfterOffset(&escaped, 0, "\\$", "\\\\$");
- ReplaceSubstringsAfterOffset(&escaped, 0, "\\\"", "\\\\\"");
-
- // Now escape special chars.
- ReplaceSubstringsAfterOffset(&escaped, 0, "$", "\\$");
- ReplaceSubstringsAfterOffset(&escaped, 0, "\"", "\\\"");
- return "\"" + escaped + "\"";
+ std::string result = "\"";
+ 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.
+ for (char ch : string_value_) {
+ if (escaped && (ch == '$' || ch == '"' || ch == '\\'))
+ result += '\\';
+ if (ch == '$' || ch == '"')
+ result += '\\';
+ result += ch;
+ escaped = (ch == '\\');
+ }
+ if (escaped)
+ result += '\\';
+ result += '"';
+ return result;
}
return string_value_;
case LIST: {
« 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