diff options
author | Chris Lattner <sabre@nondot.org> | 2008-08-23 19:23:10 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2008-08-23 19:23:10 +0000 |
commit | 22b52c984a4005dfd27b230a434f9e9cfef13aa3 (patch) | |
tree | 9698581a616c291562149cd7a2df4f5e081df893 /llvm/lib | |
parent | 0fbd44699a8cf46d720b821a2f74cf5b4fa4bbc6 (diff) | |
download | bcm5719-llvm-22b52c984a4005dfd27b230a434f9e9cfef13aa3.tar.gz bcm5719-llvm-22b52c984a4005dfd27b230a434f9e9cfef13aa3.zip |
add a simple mechanism for formatted output. This gives raw_ostream's
all the power and risk of fprintf format strings. Use them like this:
OS << format("%10.4f", 42.0) << "\n" << format("%x", 42) << '\n';
llvm-svn: 55246
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Support/raw_ostream.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp index d776fa583f1..104dc771498 100644 --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/lib/Support/raw_ostream.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/raw_ostream.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/Config/config.h" #include <ostream> @@ -134,6 +135,55 @@ raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) { return *this; } +// Formatted output. +raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { + // If we have more than a few bytes left in our output buffer, try formatting + // directly onto its end. + unsigned NextBufferSize = 127; + if (OutBufEnd-OutBufCur > 3) { + unsigned BufferBytesLeft = OutBufEnd-OutBufCur; + unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); + + // Common case is that we have plenty of space. + if (BytesUsed < BufferBytesLeft) { + OutBufCur += BytesUsed; + return *this; + } + + // Otherwise, we overflowed and the return value tells us the size to try + // again with. + NextBufferSize = BytesUsed; + } + + // If we got here, we didn't have enough space in the output buffer for the + // string. Try printing into a SmallVector that is resized to have enough + // space. Iterate until we win. + SmallVector<char, 128> V; + + while (1) { + V.resize(NextBufferSize); + + // Try formatting into the SmallVector. + unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize); + + // If BytesUsed fit into the vector, we win. + if (BytesUsed < NextBufferSize) + return write(&V[0], BytesUsed); + + // Otherwise, try again with a new size. + assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); + NextBufferSize = BytesUsed; + } +} + +//===----------------------------------------------------------------------===// +// Formatted Output +//===----------------------------------------------------------------------===// + +// Out of line virtual method. +void format_object_base::home() { +} + //===----------------------------------------------------------------------===// // raw_fd_ostream //===----------------------------------------------------------------------===// |