raw_ostream: Put all exceptional conditions in raw_ostream::write

under a single branch.

Also, add a FIXME for formatted output.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@67069 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2009-03-17 01:36:56 +00:00
parent e47b0089d9
commit 262541b074

View File

@ -123,6 +123,8 @@ void raw_ostream::flush_nonempty() {
} }
raw_ostream &raw_ostream::write(unsigned char C) { raw_ostream &raw_ostream::write(unsigned char C) {
// Group exceptional cases into a single branch.
if (OutBufCur >= OutBufEnd) {
if (Unbuffered) { if (Unbuffered) {
write_impl(reinterpret_cast<char*>(&C), 1); write_impl(reinterpret_cast<char*>(&C), 1);
return *this; return *this;
@ -130,14 +132,17 @@ raw_ostream &raw_ostream::write(unsigned char C) {
if (!OutBufStart) if (!OutBufStart)
SetBufferSize(); SetBufferSize();
else if (OutBufCur >= OutBufEnd) else
flush_nonempty(); flush_nonempty();
}
*OutBufCur++ = C; *OutBufCur++ = C;
return *this; return *this;
} }
raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) { raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
// Group exceptional cases into a single branch.
if (OutBufCur+Size > OutBufEnd) {
if (Unbuffered) { if (Unbuffered) {
write_impl(Ptr, Size); write_impl(Ptr, Size);
return *this; return *this;
@ -145,8 +150,9 @@ raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
if (!OutBufStart) if (!OutBufStart)
SetBufferSize(); SetBufferSize();
else if (OutBufCur+Size > OutBufEnd) else
flush_nonempty(); flush_nonempty();
}
// Handle short strings specially, memcpy isn't very good at very short // Handle short strings specially, memcpy isn't very good at very short
// strings. // strings.
@ -176,8 +182,14 @@ raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
// Formatted output. // Formatted output.
raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { 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 // If we have more than a few bytes left in our output buffer, try
// directly onto its end. // formatting directly onto its end.
//
// FIXME: This test is a bit silly, since if we don't have enough
// space in the buffer we will have to flush the formatted output
// anyway. We should just flush upfront in such cases, and use the
// whole buffer as our scratch pad. Note, however, that this case is
// also necessary for correctness on unbuffered streams.
unsigned NextBufferSize = 127; unsigned NextBufferSize = 127;
if (OutBufEnd-OutBufCur > 3) { if (OutBufEnd-OutBufCur > 3) {
unsigned BufferBytesLeft = OutBufEnd-OutBufCur; unsigned BufferBytesLeft = OutBufEnd-OutBufCur;