blob: df91641962ff65c14f9223b2576c3dff7af78168 [file] [log] [blame]
David Greene191cf282009-07-13 16:49:27 +00001//===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
David Greene62fe47a2009-07-10 21:14:44 +00002//
3// The LLVM Compiler Infrastructure
4//
David Greene191cf282009-07-13 16:49:27 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
David Greene62fe47a2009-07-10 21:14:44 +00007//
8//===----------------------------------------------------------------------===//
9//
David Greene191cf282009-07-13 16:49:27 +000010// This file contains the implementation of formatted_raw_ostream and
11// friends.
David Greene62fe47a2009-07-10 21:14:44 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Support/FormattedStream.h"
16
David Greene191cf282009-07-13 16:49:27 +000017using namespace llvm;
David Greene62fe47a2009-07-10 21:14:44 +000018
David Greene191cf282009-07-13 16:49:27 +000019/// ComputeColumn - Examine the current output and figure out which
20/// column we end up in after output.
21///
22void formatted_raw_ostream::ComputeColumn(const char *Ptr, unsigned Size)
23{
24 // Keep track of the current column by scanning the string for
25 // special characters
David Greene62fe47a2009-07-10 21:14:44 +000026
David Greene191cf282009-07-13 16:49:27 +000027 for (const char *epos = Ptr + Size; Ptr != epos; ++Ptr) {
28 ++Column;
29 if (*Ptr == '\n' || *Ptr == '\r')
30 Column = 0;
31 else if (*Ptr == '\t')
32 Column += (8 - (Column & 0x7)) & 0x7;
David Greene62fe47a2009-07-10 21:14:44 +000033 }
34}
David Greene191cf282009-07-13 16:49:27 +000035
36/// PadToColumn - Align the output to some column number.
37///
38/// \param NewCol - The column to move to.
39/// \param MinPad - The minimum space to give after the most recent
40/// I/O, even if the current column + minpad > newcol.
41///
42void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad)
43{
44 flush();
45
46 // Output spaces until we reach the desired column.
47 unsigned num = NewCol - Column;
48 if (NewCol < Column || num < MinPad) {
49 num = MinPad;
50 }
51
52 // TODO: Write a whole string at a time.
53 while (num-- > 0) {
54 write(' ');
55 }
56}
57