Chromium Code Reviews| 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/escape.h" | 5 #include "tools/gn/escape.h" |
| 6 | 6 |
| 7 #include "base/containers/stack_container.h" | 7 #include "base/containers/stack_container.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 if (ch == '$' || ch == ' ' || ch == ':') | 37 if (ch == '$' || ch == ' ' || ch == ':') |
| 38 dest->push_back('$'); | 38 dest->push_back('$'); |
| 39 dest->push_back(ch); | 39 dest->push_back(ch); |
| 40 } | 40 } |
| 41 | 41 |
| 42 template<typename DestString> | 42 template<typename DestString> |
| 43 void EscapeStringToString_Ninja(const base::StringPiece& str, | 43 void EscapeStringToString_Ninja(const base::StringPiece& str, |
| 44 const EscapeOptions& options, | 44 const EscapeOptions& options, |
| 45 DestString* dest, | 45 DestString* dest, |
| 46 bool* needed_quoting) { | 46 bool* needed_quoting) { |
| 47 for (size_t i = 0; i < str.size(); i++) | 47 for (auto& elem : str) |
|
scottmg
2015/03/13 19:58:30
const?
tfarina
2015/03/13 23:27:49
Done.
| |
| 48 NinjaEscapeChar(str[i], dest); | 48 NinjaEscapeChar(elem, dest); |
| 49 } | 49 } |
| 50 | 50 |
| 51 template<typename DestString> | 51 template<typename DestString> |
| 52 void EscapeStringToString_NinjaPreformatted(const base::StringPiece& str, | 52 void EscapeStringToString_NinjaPreformatted(const base::StringPiece& str, |
| 53 DestString* dest) { | 53 DestString* dest) { |
| 54 // Only Ninja-escape $. | 54 // Only Ninja-escape $. |
| 55 for (size_t i = 0; i < str.size(); i++) { | 55 for (auto& elem : str) { |
|
scottmg
2015/03/13 19:58:30
const?
tfarina
2015/03/13 23:27:49
Done.
| |
| 56 if (str[i] == '$') | 56 if (elem == '$') |
| 57 dest->push_back('$'); | 57 dest->push_back('$'); |
| 58 dest->push_back(str[i]); | 58 dest->push_back(elem); |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Escape for CommandLineToArgvW and additionally escape Ninja characters. | 62 // Escape for CommandLineToArgvW and additionally escape Ninja characters. |
| 63 // | 63 // |
| 64 // The basic algorithm is if the string doesn't contain any parse-affecting | 64 // The basic algorithm is if the string doesn't contain any parse-affecting |
| 65 // characters, don't do anything (other than the Ninja processing). If it does, | 65 // characters, don't do anything (other than the Ninja processing). If it does, |
| 66 // quote the string, and backslash-escape all quotes and backslashes. | 66 // quote the string, and backslash-escape all quotes and backslashes. |
| 67 // See: | 67 // See: |
| 68 // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/eve ryone-quotes-arguments-the-wrong-way.aspx | 68 // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/eve ryone-quotes-arguments-the-wrong-way.aspx |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 if (needed_quoting) | 111 if (needed_quoting) |
| 112 *needed_quoting = true; | 112 *needed_quoting = true; |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 | 115 |
| 116 template<typename DestString> | 116 template<typename DestString> |
| 117 void EscapeStringToString_PosixNinjaFork(const base::StringPiece& str, | 117 void EscapeStringToString_PosixNinjaFork(const base::StringPiece& str, |
| 118 const EscapeOptions& options, | 118 const EscapeOptions& options, |
| 119 DestString* dest, | 119 DestString* dest, |
| 120 bool* needed_quoting) { | 120 bool* needed_quoting) { |
| 121 for (size_t i = 0; i < str.size(); i++) { | 121 for (auto& elem : str) { |
|
scottmg
2015/03/13 19:58:30
const?
tfarina
2015/03/13 23:27:49
Done.
| |
| 122 if (str[i] == '$' || str[i] == ' ') { | 122 if (elem == '$' || elem == ' ') { |
| 123 // Space and $ are special to both Ninja and the shell. '$' escape for | 123 // Space and $ are special to both Ninja and the shell. '$' escape for |
| 124 // Ninja, then backslash-escape for the shell. | 124 // Ninja, then backslash-escape for the shell. |
| 125 dest->push_back('\\'); | 125 dest->push_back('\\'); |
| 126 dest->push_back('$'); | 126 dest->push_back('$'); |
| 127 dest->push_back(str[i]); | 127 dest->push_back(elem); |
| 128 } else if (str[i] == ':') { | 128 } else if (elem == ':') { |
| 129 // Colon is the only other Ninja special char, which is not special to | 129 // Colon is the only other Ninja special char, which is not special to |
| 130 // the shell. | 130 // the shell. |
| 131 dest->push_back('$'); | 131 dest->push_back('$'); |
| 132 dest->push_back(':'); | 132 dest->push_back(':'); |
| 133 } else if (static_cast<unsigned>(str[i]) >= 0x80 || | 133 } else if (static_cast<unsigned>(elem) >= 0x80 || |
| 134 !kShellValid[static_cast<int>(str[i])]) { | 134 !kShellValid[static_cast<int>(elem)]) { |
| 135 // All other invalid shell chars get backslash-escaped. | 135 // All other invalid shell chars get backslash-escaped. |
| 136 dest->push_back('\\'); | 136 dest->push_back('\\'); |
| 137 dest->push_back(str[i]); | 137 dest->push_back(elem); |
| 138 } else { | 138 } else { |
| 139 // Everything else is a literal. | 139 // Everything else is a literal. |
| 140 dest->push_back(str[i]); | 140 dest->push_back(elem); |
| 141 } | 141 } |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 | 144 |
| 145 template<typename DestString> | 145 template<typename DestString> |
| 146 void EscapeStringToString(const base::StringPiece& str, | 146 void EscapeStringToString(const base::StringPiece& str, |
| 147 const EscapeOptions& options, | 147 const EscapeOptions& options, |
| 148 DestString* dest, | 148 DestString* dest, |
| 149 bool* needed_quoting) { | 149 bool* needed_quoting) { |
| 150 switch (options.mode) { | 150 switch (options.mode) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 } | 197 } |
| 198 | 198 |
| 199 void EscapeStringToStream(std::ostream& out, | 199 void EscapeStringToStream(std::ostream& out, |
| 200 const base::StringPiece& str, | 200 const base::StringPiece& str, |
| 201 const EscapeOptions& options) { | 201 const EscapeOptions& options) { |
| 202 base::StackString<256> escaped; | 202 base::StackString<256> escaped; |
| 203 EscapeStringToString(str, options, &escaped.container(), nullptr); | 203 EscapeStringToString(str, options, &escaped.container(), nullptr); |
| 204 if (!escaped->empty()) | 204 if (!escaped->empty()) |
| 205 out.write(escaped->data(), escaped->size()); | 205 out.write(escaped->data(), escaped->size()); |
| 206 } | 206 } |
| OLD | NEW |