-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatters.go
268 lines (239 loc) · 5.38 KB
/
formatters.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package sqroot
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
// Digit represents a digit and a zero based position.
type Digit struct {
// The 0 based position of the digit.
Position int
// The value of the digit. Always between 0 and 9.
Value int
}
type printer struct {
rawPrinter
missingDigit rune
}
func newPrinter(
writer io.Writer, maxDigits int, settings *printerSettings) *printer {
var result printer
result.Init(writer, maxDigits, settings)
result.missingDigit = settings.missingDigit
return &result
}
func (p *printer) Consume(d Digit) {
if p.index < d.Position {
if p.digitsPerRow > 0 && p.digitCountSpec != "" {
p.skipRowsFor(d.Position)
}
for p.index < d.Position {
p.rawPrinter.Consume(p.missingDigit)
}
}
p.rawPrinter.Consume('0' + rune(d.Value))
}
func (p *printer) skipRowsFor(nextPosit int) {
currentRow := p.index / p.digitsPerRow
nextRow := nextPosit / p.digitsPerRow
if p.index%p.digitsPerRow == 0 {
p.skipRows(nextRow - currentRow)
} else if nextRow > currentRow {
p.skipRows(nextRow - currentRow - 1)
}
}
type rawPrinter struct {
cWriter *countingWriter
writer *bufio.Writer
indentation string
digitCountSpec string
digitsPerRow int
digitsPerColumn int
index int
indexInRow int
err error
}
func (p *rawPrinter) Init(
writer io.Writer, maxDigits int, settings *printerSettings) {
cWriter := &countingWriter{delegate: writer}
var bWriter *bufio.Writer
if settings.bufferSize <= 0 {
bWriter = bufio.NewWriter(cWriter)
} else {
bWriter = bufio.NewWriterSize(cWriter, settings.bufferSize)
}
indentation, digitCountSpec := computeIndentation(
settings.digitCountWidth(maxDigits))
*p = rawPrinter{
cWriter: cWriter,
writer: bWriter,
indentation: indentation,
digitCountSpec: digitCountSpec,
digitsPerRow: settings.digitsPerRow,
digitsPerColumn: settings.digitsPerColumn,
}
}
func (p *rawPrinter) CanConsume() bool {
return p.err == nil
}
func (p *rawPrinter) Consume(digit rune) {
if !p.CanConsume() {
return
}
if p.index == 0 {
_, p.err = fmt.Fprintf(p.writer, "%s0.", p.indentation)
if p.err != nil {
return
}
} else if p.digitsPerRow > 0 && p.index%p.digitsPerRow == 0 {
if p.BytesWritten()+p.bytesBuffered() > 0 {
_, p.err = fmt.Fprintln(p.writer)
if p.err != nil {
return
}
}
if p.digitCountSpec != "" {
_, p.err = fmt.Fprintf(p.writer, p.digitCountSpec, p.index)
if p.err != nil {
return
}
}
_, p.err = p.writer.WriteString(" ")
if p.err != nil {
return
}
p.indexInRow = 0
} else if p.digitsPerColumn > 0 && p.indexInRow%p.digitsPerColumn == 0 {
p.err = p.writer.WriteByte(' ')
if p.err != nil {
return
}
}
_, p.err = p.writer.WriteRune(digit)
if p.err != nil {
return
}
p.index++
p.indexInRow++
}
func (p *rawPrinter) Finish() {
err := p.writer.Flush()
if p.err == nil {
p.err = err
}
}
func (p *rawPrinter) BytesWritten() int {
return p.cWriter.bytesWritten
}
func (p *rawPrinter) Err() error {
return p.err
}
func (p *rawPrinter) bytesBuffered() int {
return p.writer.Buffered()
}
func (p *rawPrinter) skipRows(rowsToSkip int) {
p.index += rowsToSkip * p.digitsPerRow
}
type printerSettings struct {
digitsPerRow int
digitsPerColumn int
showCount bool
missingDigit rune
bufferSize int
}
func (p *printerSettings) digitCountWidth(maxDigits int) int {
if !p.showCount || p.digitsPerRow <= 0 {
return 0
}
if maxDigits <= p.digitsPerRow {
return 0
}
maxCounter := ((maxDigits - 1) / p.digitsPerRow) * p.digitsPerRow
return len(strconv.Itoa(maxCounter))
}
func computeIndentation(width int) (
indentation string, digitCountSpec string) {
if width <= 0 {
return
}
indentation = strings.Repeat(" ", width)
digitCountSpec = fmt.Sprintf("%%%dd", width)
return
}
type formatter struct {
writer *bufio.Writer
sigDigits int // invariant sigDigits >= exponent
exponent int
exactDigitCount bool
index int
}
func newFormatter(
w io.Writer, sigDigits, exponent int, exactDigitCount bool) *formatter {
if sigDigits < exponent {
panic("sigDigits must be >= exponent")
}
return &formatter{
writer: bufio.NewWriter(w),
sigDigits: sigDigits,
exponent: exponent,
exactDigitCount: exactDigitCount,
}
}
func (f *formatter) CanConsume() bool {
return f.index < f.sigDigits
}
func (f *formatter) Consume(digit int) {
if !f.CanConsume() {
return
}
f.add(digit)
}
func (f *formatter) Finish() {
maxDigits := f.sigDigits
if !f.exactDigitCount {
maxDigits = f.exponent
}
for f.index < maxDigits {
f.add(0)
}
// If we haven't written anything yet
if f.index == 0 {
count := -f.exponent
if f.exactDigitCount {
count = f.sigDigits - f.exponent
}
f.addLeadingZeros(count)
}
f.writer.Flush()
}
func (f *formatter) add(digit int) {
if f.index == 0 && f.exponent <= 0 {
f.addLeadingZeros(-f.exponent)
}
if f.index == f.exponent {
f.writer.WriteByte('.')
}
f.writer.WriteByte('0' + byte(digit))
f.index++
}
func (f *formatter) addLeadingZeros(count int) {
f.writer.WriteByte('0')
if count <= 0 {
return
}
f.writer.WriteByte('.')
for i := 0; i < count; i++ {
f.writer.WriteByte('0')
}
}
type countingWriter struct {
delegate io.Writer
bytesWritten int
}
func (c *countingWriter) Write(p []byte) (n int, err error) {
n, err = c.delegate.Write(p)
c.bytesWritten += n
return
}