1 //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the implementation of formatted_raw_ostream.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/FormattedStream.h"
19 /// ComputeColumn - Examine the current output and figure out which
20 /// column we end up in after output.
22 void formatted_raw_ostream::ComputeColumn(unsigned &Column) {
23 // Keep track of the current column by scanning the string for
26 for (const char *Ptr = begin(); Ptr != end(); ++Ptr) {
28 if (*Ptr == '\n' || *Ptr == '\r')
30 else if (*Ptr == '\t')
31 Column += (8 - (Column & 0x7)) & 0x7;
35 /// PadToColumn - Align the output to some column number.
37 /// \param NewCol - The column to move to.
38 /// \param MinPad - The minimum space to give after the most recent
39 /// I/O, even if the current column + minpad > newcol.
41 void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) {
42 // Start out from the last flush position.
43 unsigned Column = ColumnFlushed;
45 // Now figure out what's in the buffer and add it to the column
47 ComputeColumn(Column);
49 // Output spaces until we reach the desired column.
50 unsigned num = NewCol - Column;
51 if (NewCol < Column || num < MinPad)
54 // Keep a buffer of spaces handy to speed up processing.
55 static char Spaces[MAX_COLUMN_PAD];
56 static bool Initialized = false;
58 std::fill_n(Spaces, MAX_COLUMN_PAD, ' '),
62 assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding");
67 /// fouts() - This returns a reference to a formatted_raw_ostream for
68 /// standard output. Use it like: fouts() << "foo" << "bar";
69 formatted_raw_ostream &llvm::fouts() {
70 static formatted_raw_ostream S(outs());
74 /// ferrs() - This returns a reference to a formatted_raw_ostream for
75 /// standard error. Use it like: ferrs() << "foo" << "bar";
76 formatted_raw_ostream &llvm::ferrs() {
77 static formatted_raw_ostream S(errs());