| 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 30 matching lines...) Expand all Loading... |
| 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 (size_t i = 0; i < str.size(); i++) |
| 48 NinjaEscapeChar(str[i], dest); | 48 NinjaEscapeChar(str[i], dest); |
| 49 } | 49 } |
| 50 | 50 |
| 51 template<typename DestString> |
| 52 void EscapeStringToString_NinjaPreformatted(const base::StringPiece& str, |
| 53 DestString* dest) { |
| 54 // Only Ninja-escape $. |
| 55 for (size_t i = 0; i < str.size(); i++) { |
| 56 if (str[i] == '$') |
| 57 dest->push_back('$'); |
| 58 dest->push_back(str[i]); |
| 59 } |
| 60 } |
| 61 |
| 51 // Escape for CommandLineToArgvW and additionally escape Ninja characters. | 62 // Escape for CommandLineToArgvW and additionally escape Ninja characters. |
| 52 // | 63 // |
| 53 // 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 |
| 54 // 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, |
| 55 // quote the string, and backslash-escape all quotes and backslashes. | 66 // quote the string, and backslash-escape all quotes and backslashes. |
| 56 // See: | 67 // See: |
| 57 // 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 |
| 58 // http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx | 69 // http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx |
| 59 template<typename DestString> | 70 template<typename DestString> |
| 60 void EscapeStringToString_WindowsNinjaFork(const base::StringPiece& str, | 71 void EscapeStringToString_WindowsNinjaFork(const base::StringPiece& str, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 needed_quoting); | 170 needed_quoting); |
| 160 break; | 171 break; |
| 161 case ESCAPE_PLATFORM_POSIX: | 172 case ESCAPE_PLATFORM_POSIX: |
| 162 EscapeStringToString_PosixNinjaFork(str, options, dest, | 173 EscapeStringToString_PosixNinjaFork(str, options, dest, |
| 163 needed_quoting); | 174 needed_quoting); |
| 164 break; | 175 break; |
| 165 default: | 176 default: |
| 166 NOTREACHED(); | 177 NOTREACHED(); |
| 167 } | 178 } |
| 168 break; | 179 break; |
| 180 case ESCAPE_NINJA_PREFORMATTED_COMMAND: |
| 181 EscapeStringToString_NinjaPreformatted(str, dest); |
| 182 break; |
| 169 default: | 183 default: |
| 170 NOTREACHED(); | 184 NOTREACHED(); |
| 171 } | 185 } |
| 172 } | 186 } |
| 173 | 187 |
| 174 } // namespace | 188 } // namespace |
| 175 | 189 |
| 176 std::string EscapeString(const base::StringPiece& str, | 190 std::string EscapeString(const base::StringPiece& str, |
| 177 const EscapeOptions& options, | 191 const EscapeOptions& options, |
| 178 bool* needed_quoting) { | 192 bool* needed_quoting) { |
| 179 std::string result; | 193 std::string result; |
| 180 result.reserve(str.size() + 4); // Guess we'll add a couple of extra chars. | 194 result.reserve(str.size() + 4); // Guess we'll add a couple of extra chars. |
| 181 EscapeStringToString(str, options, &result, needed_quoting); | 195 EscapeStringToString(str, options, &result, needed_quoting); |
| 182 return result; | 196 return result; |
| 183 } | 197 } |
| 184 | 198 |
| 185 void EscapeStringToStream(std::ostream& out, | 199 void EscapeStringToStream(std::ostream& out, |
| 186 const base::StringPiece& str, | 200 const base::StringPiece& str, |
| 187 const EscapeOptions& options) { | 201 const EscapeOptions& options) { |
| 188 base::StackString<256> escaped; | 202 base::StackString<256> escaped; |
| 189 EscapeStringToString(str, options, &escaped.container(), NULL); | 203 EscapeStringToString(str, options, &escaped.container(), NULL); |
| 190 if (!escaped->empty()) | 204 if (!escaped->empty()) |
| 191 out.write(escaped->data(), escaped->size()); | 205 out.write(escaped->data(), escaped->size()); |
| 192 } | 206 } |
| OLD | NEW |