[llvm-commits] [llvm] r84355 - in /llvm/trunk: include/llvm/Support/raw_ostream.h lib/Support/raw_ostream.cpp unittests/Support/raw_ostream_test.cpp

Daniel Dunbar daniel at zuster.org
Mon Oct 19 14:08:02 PDT 2009


On Mon, Oct 19, 2009 at 10:27 AM, Jim Grosbach <grosbach at apple.com> wrote:
> Hi Daniel,
>
> This looks very nice. Definitely an improvement over EscapeString.
>
> I assume the choice to restrict the list handled explicitly and not include
> things like '\v' and '\b' is due to those being uncommon enough to just
> leave to the generic octal expansion?

I was just stealing the logic from EscapeString. But I do tend to
think its better to just go to octal, it is more likely to be helpful
to users of this function I expect (unless they have the \v and \b
values memorized), and the clients of this function don't need to
worry as much about whether whatever is parsing the output understands
\v and \b.

 - Daniel

> -Jim
>
> On Oct 17, 2009, at 1:43 PM, Daniel Dunbar wrote:
>
>> Author: ddunbar
>> Date: Sat Oct 17 15:43:08 2009
>> New Revision: 84355
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=84355&view=rev
>> Log:
>> Add raw_ostream::write_escaped, for writing escaped strings.
>>
>> Modified:
>>   llvm/trunk/include/llvm/Support/raw_ostream.h
>>   llvm/trunk/lib/Support/raw_ostream.cpp
>>   llvm/trunk/unittests/Support/raw_ostream_test.cpp
>>
>> Modified: llvm/trunk/include/llvm/Support/raw_ostream.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/raw_ostream.h?rev=84355&r1=84354&r2=84355&view=diff
>>
>>
>> ==============================================================================
>> --- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
>> +++ llvm/trunk/include/llvm/Support/raw_ostream.h Sat Oct 17 15:43:08 2009
>> @@ -216,6 +216,10 @@
>>  /// write_hex - Output \arg N in hexadecimal, without any prefix or
>> padding.
>>  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(const char *Ptr, size_t Size);
>>
>>
>> Modified: llvm/trunk/lib/Support/raw_ostream.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=84355&r1=84354&r2=84355&view=diff
>>
>>
>> ==============================================================================
>> --- llvm/trunk/lib/Support/raw_ostream.cpp (original)
>> +++ llvm/trunk/lib/Support/raw_ostream.cpp Sat Oct 17 15:43:08 2009
>> @@ -168,6 +168,40 @@
>>  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) {
>>  *this << '0' << 'x';
>>
>>
>> Modified: llvm/trunk/unittests/Support/raw_ostream_test.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/raw_ostream_test.cpp?rev=84355&r1=84354&r2=84355&view=diff
>>
>>
>> ==============================================================================
>> --- llvm/trunk/unittests/Support/raw_ostream_test.cpp (original)
>> +++ llvm/trunk/unittests/Support/raw_ostream_test.cpp Sat Oct 17 15:43:08
>> 2009
>> @@ -127,4 +127,20 @@
>>  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);
>> +}
>> +
>> }
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
>




More information about the llvm-commits mailing list