llvm.org GIT mirror llvm / dbe77cf
Support writing a StringRef to a raw_ostream directly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76754 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 10 years ago
2 changed file(s) with 21 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
1414 #define LLVM_SUPPORT_RAW_OSTREAM_H
1515
1616 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringRef.h"
1718 #include
1819 #include
1920 #include
150151 return *this;
151152 }
152153
153 raw_ostream &operator<<(const char *Str) {
154 // Inline fast path, particulary for constant strings where a
155 // sufficiently smart compiler will simplify strlen.
156
157 size_t Size = strlen(Str);
154 raw_ostream &operator<<(const StringRef &Str) {
155 // Inline fast path, particularly for strings with a known length.
156 size_t Size = Str.size();
158157
159158 // Make sure we can use the fast path.
160159 if (OutBufCur+Size > OutBufEnd)
161 return write(Str, Size);
162
163 memcpy(OutBufCur, Str, Size);
160 return write(Str.data(), Size);
161
162 memcpy(OutBufCur, Str.data(), Size);
164163 OutBufCur += Size;
164 return *this;
165 }
166
167 raw_ostream &operator<<(const char *Str) {
168 // Inline fast path, particulary for constant strings where a sufficiently
169 // smart compiler will simplify strlen.
170
171 this->operator<<(StringRef(Str));
165172 return *this;
166173 }
167174
88
99 #include "gtest/gtest.h"
1010 #include "llvm/ADT/StringRef.h"
11 #include "llvm/Support/raw_ostream.h"
1112 using namespace llvm;
1213
1314 namespace {
6566 EXPECT_TRUE(Str.startswith("he"));
6667 EXPECT_FALSE(Str.startswith("helloworld"));
6768 EXPECT_FALSE(Str.startswith("hi"));
69
70 std::string Storage;
71 raw_string_ostream OS(Storage);
72 OS << StringRef("hello");
73 EXPECT_EQ("hello", OS.str());
6874 }
6975
7076 } // end anonymous namespace