[llvm-commits] [llvm] r120600 - in /llvm/trunk: include/llvm/ADT/Twine.h lib/Support/Twine.cpp unittests/ADT/TwineTest.cpp
Michael J. Spencer
bigcheesegs at gmail.com
Wed Dec 1 12:37:30 PST 2010
Author: mspencer
Date: Wed Dec 1 14:37:30 2010
New Revision: 120600
URL: http://llvm.org/viewvc/llvm-project?rev=120600&view=rev
Log:
Support/ADT/Twine: Add toNullTerminatedStringRef.
Modified:
llvm/trunk/include/llvm/ADT/Twine.h
llvm/trunk/lib/Support/Twine.cpp
llvm/trunk/unittests/ADT/TwineTest.cpp
Modified: llvm/trunk/include/llvm/ADT/Twine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Twine.h?rev=120600&r1=120599&r2=120600&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Twine.h (original)
+++ llvm/trunk/include/llvm/ADT/Twine.h Wed Dec 1 14:37:30 2010
@@ -382,6 +382,14 @@
/// SmallVector and a StringRef to the SmallVector's data is returned.
StringRef toStringRef(SmallVectorImpl<char> &Out) const;
+ /// toNullTerminatedStringRef - This returns the twine as a single null
+ /// terminated StringRef if it can be represented as such. Otherwise the
+ /// twine is written into the given SmallVector and a StringRef to the
+ /// SmallVector's data is returned.
+ ///
+ /// The returned StringRef's size does not include the null terminator.
+ StringRef toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const;
+
/// print - Write the concatenated string represented by this twine to the
/// stream \arg OS.
void print(raw_ostream &OS) const;
Modified: llvm/trunk/lib/Support/Twine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Twine.cpp?rev=120600&r1=120599&r2=120600&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Twine.cpp (original)
+++ llvm/trunk/lib/Support/Twine.cpp Wed Dec 1 14:37:30 2010
@@ -30,6 +30,18 @@
return StringRef(Out.data(), Out.size());
}
+StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
+ if (isSingleStringRef()) {
+ StringRef sr = getSingleStringRef();
+ if (*(sr.begin() + sr.size()) == 0)
+ return sr;
+ }
+ toVector(Out);
+ Out.push_back(0);
+ Out.pop_back();
+ return StringRef(Out.data(), Out.size());
+}
+
void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
NodeKind Kind) const {
switch (Kind) {
Modified: llvm/trunk/unittests/ADT/TwineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/TwineTest.cpp?rev=120600&r1=120599&r2=120600&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/TwineTest.cpp (original)
+++ llvm/trunk/unittests/ADT/TwineTest.cpp Wed Dec 1 14:37:30 2010
@@ -9,6 +9,7 @@
#include "gtest/gtest.h"
#include "llvm/ADT/Twine.h"
+#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
@@ -69,6 +70,13 @@
repr(Twine("a").concat(Twine("b").concat(Twine("c")))));
}
+TEST(TwineTest, toNullTerminatedStringRef) {
+ SmallString<8> storage;
+ EXPECT_EQ(0, *Twine("hello").toNullTerminatedStringRef(storage).end());
+ EXPECT_EQ(0,
+ *Twine(StringRef("hello")).toNullTerminatedStringRef(storage).end());
+}
+
// I suppose linking in the entire code generator to add a unit test to check
// the code size of the concat operation is overkill... :)
More information about the llvm-commits
mailing list