llvm.org GIT mirror llvm / eb85728
Re-apply previous changes and improve column padding performance some more. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77461 91177308-0d34-0410-b5e6-96231b3b80d8 David Greene 11 years ago
2 changed file(s) with 40 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
4848 ///
4949 bool DeleteStream;
5050
51 /// Column - The current output column of the stream. The column
52 /// scheme is zero-based.
51 /// ColumnScanned - The current output column of the data that's
52 /// been flushed and the portion of the buffer that's been
53 /// scanned. The column scheme is zero-based.
5354 ///
54 unsigned Column;
55 unsigned ColumnScanned;
56
57 /// Scanned - This points to one past the last character in the
58 /// buffer we've scanned.
59 ///
60 iterator Scanned;
5561
5662 virtual void write_impl(const char *Ptr, size_t Size) {
57 ComputeColumn(Ptr, Size);
63 ComputeColumn();
5864 TheStream->write(Ptr, Size);
65 Scanned = begin();
5966 }
6067
6168 /// current_pos - Return the current position within the stream,
6976 /// ComputeColumn - Examine the current output and figure out
7077 /// which column we end up in after output.
7178 ///
72 void ComputeColumn(const char *Ptr, size_t Size);
79 void ComputeColumn();
7380
7481 public:
7582 /// formatted_raw_ostream - Open the specified file for
8390 /// underneath it.
8491 ///
8592 formatted_raw_ostream(raw_ostream &Stream, bool Delete = false)
86 : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {
93 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
8794 setStream(Stream, Delete);
8895 }
8996 explicit formatted_raw_ostream()
90 : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {}
97 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
98 Scanned = begin();
99 }
91100
92101 ~formatted_raw_ostream() {
102 flush();
93103 if (DeleteStream)
94104 delete TheStream;
95105 }
109119 if (size_t BufferSize = TheStream->GetNumBytesInBuffer())
110120 SetBufferSize(BufferSize);
111121 TheStream->SetUnbuffered();
122
123 Scanned = begin();
112124 }
113125
114126 /// PadToColumn - Align the output to some column number.
1818 /// ComputeColumn - Examine the current output and figure out which
1919 /// column we end up in after output.
2020 ///
21 void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) {
21 void formatted_raw_ostream::ComputeColumn() {
2222 // Keep track of the current column by scanning the string for
2323 // special characters
2424
25 for (const char *epos = Ptr + Size; Ptr != epos; ++Ptr) {
26 ++Column;
27 if (*Ptr == '\n' || *Ptr == '\r')
28 Column = 0;
29 else if (*Ptr == '\t')
30 Column += (8 - (Column & 0x7)) & 0x7;
25 // The buffer may have been allocated underneath us.
26 if (Scanned == 0 && GetNumBytesInBuffer() != 0) {
27 Scanned = begin();
28 }
29
30 while (Scanned != end()) {
31 ++ColumnScanned;
32 if (*Scanned == '\n' || *Scanned == '\r')
33 ColumnScanned = 0;
34 else if (*Scanned == '\t')
35 // Assumes tab stop = 8 characters.
36 ColumnScanned += (8 - (ColumnScanned & 0x7)) & 0x7;
37 ++Scanned;
3138 }
3239 }
3340
3744 /// \param MinPad - The minimum space to give after the most recent
3845 /// I/O, even if the current column + minpad > newcol.
3946 ///
40 void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) {
41 flush();
47 void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) {
48 // Figure out what's in the buffer and add it to the column count.
49 ComputeColumn();
4250
4351 // Output spaces until we reach the desired column.
44 unsigned num = NewCol - Column;
45 if (NewCol < Column || num < MinPad)
52 unsigned num = NewCol - ColumnScanned;
53 if (NewCol < ColumnScanned || num < MinPad)
4654 num = MinPad;
4755
4856 // Keep a buffer of spaces handy to speed up processing.
49 static char Spaces[MAX_COLUMN_PAD];
50 static bool Initialized = false;
51 if (!Initialized) {
52 std::fill_n(Spaces, MAX_COLUMN_PAD, ' '),
53 Initialized = true;
54 }
57 const char *Spaces = " "
58 " ";
5559
5660 assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding");
5761