llvm.org GIT mirror llvm / fbcb5b6
Add support for column computation on unbuffered streams. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79065 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
1 changed file(s) with 38 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
1414
1515 using namespace llvm;
1616
17 /// ComputeColumn - Examine the current output and figure out which
17 /// CountColumns - Examine the given char sequence and figure out which
1818 /// column we end up in after output.
1919 ///
20 void formatted_raw_ostream::ComputeColumn() {
20 static unsigned CountColumns(unsigned Column, const char *Ptr, size_t Size) {
2121 // Keep track of the current column by scanning the string for
2222 // special characters
2323
24 // The buffer may have been allocated underneath us.
25 if (Scanned == 0 && GetNumBytesInBuffer() != 0) {
26 Scanned = begin();
24 for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
25 ++Column;
26 if (*Ptr == '\n' || *Ptr == '\r')
27 Column = 0;
28 else if (*Ptr == '\t')
29 // Assumes tab stop = 8 characters.
30 Column += (8 - (Column & 0x7)) & 0x7;
2731 }
2832
29 while (Scanned != end()) {
30 ++ColumnScanned;
31 if (*Scanned == '\n' || *Scanned == '\r')
32 ColumnScanned = 0;
33 else if (*Scanned == '\t')
34 // Assumes tab stop = 8 characters.
35 ColumnScanned += (8 - (ColumnScanned & 0x7)) & 0x7;
36 ++Scanned;
37 }
33 return Column;
34 }
35
36 /// ComputeColumn - Examine the current output and figure out which
37 /// column we end up in after output.
38 void formatted_raw_ostream::ComputeColumn() {
39 // The buffer may have been allocated underneath us.
40 if (Scanned == 0) Scanned = begin();
41 // Scan all characters added since our last scan to determine the new column.
42 ColumnScanned = CountColumns(ColumnScanned, Scanned, end() - Scanned);
43 // We're now current with everything in the buffer.
44 Scanned = end();
3845 }
3946
4047 /// PadToColumn - Align the output to some column number.
6269 }
6370
6471 void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
72 // Figure out what's in the buffer and add it to the column count.
6573 ComputeColumn();
74
75 // Write the data to the underlying stream (which is unbuffered, so
76 // the data will be immediately written out).
6677 TheStream->write(Ptr, Size);
67 Scanned = begin();
78
79 // If this FormattedStream is unbuffered, scan the string that
80 // was just written to determine the new column.
81 if (Ptr == begin()) {
82 // Buffered mode. The buffer is being flushed; reset the scanning
83 // position to the beginning of the buffer.
84 assert(Ptr + Size == end() && "Buffer is not being fully flushed!");
85 Scanned = begin();
86 } else {
87 // Unbuffered mode. Immediately scan the string that was just
88 // written to determine the new column.
89 ColumnScanned = CountColumns(ColumnScanned, Ptr, Size);
90 }
6891 }
6992
7093 /// fouts() - This returns a reference to a formatted_raw_ostream for