llvm.org GIT mirror llvm / ab81027
raw_ostream: Reduce FormattedStream's reliance on raw_ostream's implementation. - Kill off begin(), end(), and iterator. It isn't clear what these mean. Instead provide getBufferStart(), which can be used with GetNumBytesInBuffer to the same effect. - Update ComputeColumn to take arguments for the buffer to scan, this simplifies the implementation of write_impl substantially. - This should also fix possible problems with the scanning pointer pointing outside of the current raw_ostream buffer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79379 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 10 years ago
3 changed file(s) with 27 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
5757 /// Scanned - This points to one past the last character in the
5858 /// buffer we've scanned.
5959 ///
60 iterator Scanned;
60 const char *Scanned;
6161
6262 virtual void write_impl(const char *Ptr, size_t Size);
6363
6969 return TheStream->tell() - TheStream->GetNumBytesInBuffer();
7070 }
7171
72 /// ComputeColumn - Examine the current output and figure out
73 /// which column we end up in after output.
72 /// ComputeColumn - Examine the given output buffer and figure out which
73 /// column we end up in after output.
7474 ///
75 void ComputeColumn();
75 void ComputeColumn(const char *Ptr, size_t size);
7676
7777 public:
7878 /// formatted_raw_ostream - Open the specified file for
9191 }
9292 explicit formatted_raw_ostream()
9393 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
94 Scanned = begin();
94 Scanned = 0;
9595 }
9696
9797 ~formatted_raw_ostream() {
115115 SetUnbuffered();
116116 TheStream->SetUnbuffered();
117117
118 Scanned = begin();
118 Scanned = 0;
119119 }
120120
121121 /// PadToColumn - Align the output to some column number. If the current
250250 /// been encountered.
251251 void error_detected() { Error = true; }
252252
253 typedef char * iterator;
254 iterator begin() { return OutBufStart; }
255 iterator end() { return OutBufCur; }
253 /// getBufferStart - Return the beginning of the current stream buffer, or 0
254 /// if the stream is unbuffered.
255 const char *getBufferStart() const { return OutBufStart; }
256256
257257 //===--------------------------------------------------------------------===//
258258 // Private Interface
3535
3636 /// ComputeColumn - Examine the current output and figure out which
3737 /// 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();
38 void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) {
39 // If our previous scan pointer is inside the buffer, assume we already
40 // scanned those bytes. This depends on raw_ostream to not change our buffer
41 // in unexpected ways.
42 if (Ptr <= Scanned && Scanned <= Ptr + Size) {
43 // Scan all characters added since our last scan to determine the new
44 // column.
45 ColumnScanned = CountColumns(ColumnScanned, Scanned,
46 Size - (Scanned - Ptr));
47 } else
48 ColumnScanned = CountColumns(ColumnScanned, Ptr, Size);
49
50 // Update the scanning pointer.
51 Scanned = Ptr + Size;
4552 }
4653
4754 /// PadToColumn - Align the output to some column number.
5259 ///
5360 void formatted_raw_ostream::PadToColumn(unsigned NewCol) {
5461 // Figure out what's in the buffer and add it to the column count.
55 ComputeColumn();
62 ComputeColumn(getBufferStart(), GetNumBytesInBuffer());
5663
5764 // Output spaces until we reach the desired column.
5865 unsigned num = NewCol - ColumnScanned;
6976
7077 void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
7178 // Figure out what's in the buffer and add it to the column count.
72 ComputeColumn();
79 ComputeColumn(Ptr, Size);
7380
7481 // Write the data to the underlying stream (which is unbuffered, so
7582 // the data will be immediately written out).
7683 TheStream->write(Ptr, Size);
7784
78 // If this FormattedStream is unbuffered, scan the string that
79 // was just written to determine the new column.
80 if (Ptr == begin()) {
81 // Buffered mode. The buffer is being flushed; reset the scanning
82 // position to the beginning of the buffer.
83 assert(Ptr + Size == end() && "Buffer is not being fully flushed!");
84 Scanned = begin();
85 } else {
86 // Unbuffered mode. Immediately scan the string that was just
87 // written to determine the new column.
88 ColumnScanned = CountColumns(ColumnScanned, Ptr, Size);
89 }
85 // Reset the scanning pointer.
86 Scanned = 0;
9087 }
9188
9289 /// fouts() - This returns a reference to a formatted_raw_ostream for