aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Support/FormattedStream.cpp
blob: d471d53df8397f4e772130843b7154dfddfad226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//===-- llvm/CodeGen/AsmStream.cpp - AsmStream Framework --------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains instantiations of "standard" AsmOStreams.
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/FormattedStream.h"

namespace llvm {
  /// ComputeColumn - Examine the current output and figure out which
  /// column we end up in after output.
  ///
  void formatted_raw_ostream::ComputeColumn(const char *Ptr, unsigned Size)
  {
    // Keep track of the current column by scanning the string for
    // special characters

    // Find the last newline.  This is our column start.  If there
    // is no newline, start with the current column.
    const char *nlpos = NULL;
    for (const char *pos = Ptr + Size, *epos = Ptr; pos > epos; --pos) {
      if (*(pos-1) == '\n') {
        nlpos = pos-1;
        // The newline will be counted, setting this to zero.  We
        // need to do it this way in case nlpos is Ptr.
        Column = -1;
        break;
      }
    }

    if (nlpos == NULL) {
      nlpos = Ptr;
    }

    // Walk through looking for tabs and advance column as appropriate
    for (const char *pos = nlpos, *epos = Ptr + Size; pos != epos; ++pos) {
      ++Column;
      if (*pos == '\t') {
        // Advance to next tab stop (every eight characters)
        Column += ((8 - (Column & 0x7)) & 0x7);
        assert(!(Column & 0x3) && "Column out of alignment");
      }
    }
  }

  /// PadToColumn - Align the output to some column number
  ///
  /// \param NewCol - The column to move to
  /// \param MinPad - The minimum space to give after the most recent
  /// I/O, even if the current column + minpad > newcol
  ///
  void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) 
  {
    flush();

    // Output spaces until we reach the desired column
    unsigned num = NewCol - Column;
    if (NewCol < Column || num < MinPad) {
      num = MinPad;
    }

    // TODO: Write a whole string at a time
    while (num-- > 0) {
      write(' ');
    }
  }
}