StringRef'ize escapeAndPrintString

Swift SVN r13911
This commit is contained in:
Dmitri Hrybenko
2014-02-14 16:00:44 +00:00
parent f8fa982c84
commit f73dd27e83

View File

@@ -56,40 +56,38 @@ CommandOutput::getAnyOutputForType(types::ID type) const {
return getAdditionalOutputForType(type);
}
static void escapeAndPrintString(llvm::raw_ostream &os, const char *str) {
StringRef String(str);
if (String.empty()) {
static void escapeAndPrintString(llvm::raw_ostream &os, StringRef Str) {
if (Str.empty()) {
// Special-case the empty string.
os << "\"\"";
return;
}
bool NeedsEscape = String.find_first_of(" \"\\$") != StringRef::npos;
bool NeedsEscape = Str.find_first_of(" \"\\$") != StringRef::npos;
if (!NeedsEscape) {
// This string doesn't have anything we need to escape, so print it directly
os << str;
os << Str;
return;
}
// Quote and escape. This isn't really complete, but is good enough, and
// matches how Clang's Command handles escaping arguments.
os << '"';
for (const char c : String) {
for (const char c : Str) {
switch (c) {
case '"':
case '\\':
case '$':
// These characters need to be escaped.
os << '\\';
// Fall-through to the default case, since we still need to print the
// character.
default:
os << c;
case '"':
case '\\':
case '$':
// These characters need to be escaped.
os << '\\';
// Fall-through to the default case, since we still need to print the
// character.
default:
os << c;
}
}
os << '"';
}
void Command::printCommandLine(llvm::raw_ostream &os) const {