This source file includes following definitions.
- EscapeStringToString
- EscapeString
- EscapeStringToStream
#include "tools/gn/escape.h"
#include "base/containers/stack_container.h"
namespace {
template<typename DestString>
void EscapeStringToString(const base::StringPiece& str,
const EscapeOptions& options,
DestString* dest,
bool* needed_quoting) {
bool used_quotes = false;
for (size_t i = 0; i < str.size(); i++) {
if (str[i] == '$' && (options.mode & ESCAPE_NINJA)) {
dest->push_back('$');
dest->push_back('$');
} else if (str[i] == '"' && (options.mode & ESCAPE_SHELL)) {
dest->push_back('\\');
dest->push_back('"');
} else if (str[i] == ' ') {
if (options.mode & ESCAPE_NINJA) {
dest->push_back('$');
}
if (options.mode & ESCAPE_SHELL) {
if (needed_quoting)
*needed_quoting = true;
if (!options.inhibit_quoting) {
if (!used_quotes) {
used_quotes = true;
dest->insert(dest->begin(), '"');
}
}
}
dest->push_back(' ');
} else if (str[i] == '\'' && (options.mode & ESCAPE_JSON)) {
dest->push_back('\\');
dest->push_back('\'');
#if defined(OS_WIN)
} else if (str[i] == '/' && options.convert_slashes) {
dest->push_back('\\');
#else
} else if (str[i] == '\\' && (options.mode & ESCAPE_SHELL)) {
dest->push_back('\\');
dest->push_back('\\');
#endif
} else if (str[i] == '\\' && (options.mode & ESCAPE_JSON)) {
dest->push_back('\\');
dest->push_back('\\');
} else {
dest->push_back(str[i]);
}
}
if (used_quotes)
dest->push_back('"');
}
}
std::string EscapeString(const base::StringPiece& str,
const EscapeOptions& options,
bool* needed_quoting) {
std::string result;
result.reserve(str.size() + 4);
EscapeStringToString(str, options, &result, needed_quoting);
return result;
}
void EscapeStringToStream(std::ostream& out,
const base::StringPiece& str,
const EscapeOptions& options) {
base::StackVector<char, 256> result;
result->reserve(str.size() + 4);
EscapeStringToString(str, options, &result.container(), NULL);
if (!result->empty())
out.write(result->data(), result->size());
}