Skip to content
Snippets Groups Projects
Commit 4108c43f authored by Daniel Dunbar's avatar Daniel Dunbar
Browse files

Add raw_ostream::write_escaped, for writing escaped strings.

llvm-svn: 84355
parent 9b24e8ab
No related branches found
No related tags found
No related merge requests found
...@@ -216,6 +216,10 @@ public: ...@@ -216,6 +216,10 @@ public:
/// write_hex - Output \arg N in hexadecimal, without any prefix or padding. /// write_hex - Output \arg N in hexadecimal, without any prefix or padding.
raw_ostream &write_hex(unsigned long long N); raw_ostream &write_hex(unsigned long long N);
/// write_escaped - Output \arg Str, turning '\\', '\t', '\n', '"', and
/// anything that doesn't satisfy std::isprint into an escape sequence.
raw_ostream &write_escaped(StringRef Str);
raw_ostream &write(unsigned char C); raw_ostream &write(unsigned char C);
raw_ostream &write(const char *Ptr, size_t Size); raw_ostream &write(const char *Ptr, size_t Size);
......
...@@ -168,6 +168,40 @@ raw_ostream &raw_ostream::write_hex(unsigned long long N) { ...@@ -168,6 +168,40 @@ raw_ostream &raw_ostream::write_hex(unsigned long long N) {
return write(CurPtr, EndPtr-CurPtr); return write(CurPtr, EndPtr-CurPtr);
} }
raw_ostream &raw_ostream::write_escaped(StringRef Str) {
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
unsigned char c = Str[i];
switch (c) {
case '\\':
*this << '\\' << '\\';
break;
case '\t':
*this << '\\' << 't';
break;
case '\n':
*this << '\\' << 'n';
break;
case '"':
*this << '\\' << '"';
break;
default:
if (std::isprint(c)) {
*this << c;
break;
}
// Always expand to a 3-character octal escape.
*this << '\\';
*this << char('0' + ((c >> 6) & 7));
*this << char('0' + ((c >> 3) & 7));
*this << char('0' + ((c >> 0) & 7));
}
}
return *this;
}
raw_ostream &raw_ostream::operator<<(const void *P) { raw_ostream &raw_ostream::operator<<(const void *P) {
*this << '0' << 'x'; *this << '0' << 'x';
......
...@@ -127,4 +127,20 @@ TEST(raw_ostreamTest, TinyBuffer) { ...@@ -127,4 +127,20 @@ TEST(raw_ostreamTest, TinyBuffer) {
EXPECT_EQ("hello1world", OS.str()); EXPECT_EQ("hello1world", OS.str());
} }
TEST(raw_ostreamTest, WriteEscaped) {
std::string Str;
Str = "";
raw_string_ostream(Str).write_escaped("hi");
EXPECT_EQ("hi", Str);
Str = "";
raw_string_ostream(Str).write_escaped("\\\t\n\"");
EXPECT_EQ("\\\\\\t\\n\\\"", Str);
Str = "";
raw_string_ostream(Str).write_escaped("\1\10\200");
EXPECT_EQ("\\001\\010\\200", Str);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment