-
Notifications
You must be signed in to change notification settings - Fork 9
/
squares.go
356 lines (288 loc) · 6.79 KB
/
squares.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package prettyslice
import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// drawing pretty draws a slice
type drawing struct {
slice, backer reflect.Value
buf *strings.Builder
// draw multiple items or just one?
multiple bool
}
// Show pretty prints slices
func Show(msg string, slices ...interface{}) {
buf := new(strings.Builder)
for i, slice := range slices {
d := create(slice, buf)
// only draw the message for the first item (grouping)
if i > 0 {
msg = ""
}
d.header(msg)
d.pushNewline()
if s := d.slice; s.IsNil() {
d.push("<nil slice>\n")
continue
} else if s.Len() == 0 {
d.push("<empty slice>\n")
// keep processing: slice can have elements in the backing array
}
// draw the slice elements
l := d.backer.Len()
if !PrintBacking {
l = d.slice.Len()
}
step := MaxPerLine
if step <= 0 {
step = l
}
for f := 0; f < l; f += step {
if enough(f) {
d.push(ColorBacker.Sprintf("...%d more...", l-f))
d.pushNewline()
break
}
t := f + step
d.wrap("╔", "╗", f, t)
d.pushNewline()
d.middle(f, t)
d.pushNewline()
d.wrap("╚", "╝", f, t)
d.pushNewline()
d.indexes(f, t)
d.pushNewline()
if PrintElementAddr {
d.addresses(f, t)
d.pushNewline()
}
}
}
// WriteString already checks for WriteString method
io.WriteString(Writer, buf.String())
}
// create initializes a new drawing struct.
func create(slice interface{}, buf *strings.Builder) drawing {
s := reflect.ValueOf(slice)
multiple := true
if s.Kind() != reflect.Slice {
s = makeSlice(s)
// don't draw slice details for one item
multiple = false
}
return drawing{
slice: s,
// this contains the backing array's data, after the slice's pointer.
backer: s.Slice(0, s.Cap()),
multiple: multiple,
buf: buf,
}
}
// header draws the header information about the slice with a message
func (d drawing) header(msg string) {
var info string
if d.multiple {
f := " (len:%-2d cap:%-2d ptr:%-4d)"
if PrintHex {
f = " (len:%-2d cap:%-2d ptr:%-10x)"
}
info = fmt.Sprintf(
f,
d.slice.Len(), d.slice.Cap(), d.pointer(0),
)
}
msg = " " + msg
w, l := Width, len(msg)+len(info)
w -= l
if l > Width {
w = 1
}
d.push(ColorHeader.Sprintf("%s%*s%s", msg, w, "", info))
}
// indexes draws the index numbers on top of the slice elements
func (d drawing) indexes(from, to int) {
for i, v := range over(d.backer, from, to) {
if !PrintBacking && d.backing(from+i) {
break
}
// current index
ci := i + from
lp, rp := paddings(len(strconv.Itoa(ci)), slen(v))
lps := strings.Repeat(" ", lp)
d.push(ColorIndex.Sprintf("%s%-*d", lps, rp, ci))
}
}
// addresses draw element addresses
func (d drawing) addresses(from, to int) {
for i, v := range over(d.backer, from, to) {
if !PrintBacking && d.backing(from+i) {
break
}
// current index
ci := i + from
p := d.pointer(ci)
lp, rp := paddings(len(strconv.FormatInt(p, 10)), slen(v))
lps := strings.Repeat(" ", lp)
d.push(ColorAddr.Sprintf("%s%-*d", lps, rp, p))
}
}
// wrap draws the header and the footer depending on the left and right values
func (d drawing) wrap(left, right string, from, to int) {
for i, v := range over(d.backer, from, to) {
c, l, r, m := ColorSlice, left, right, "═"
if d.backing(from + i) {
if !PrintBacking {
break
}
c, l, r, m = ColorBacker, "+", "+", "-"
}
// draw the horizontal line
// +2 is for the left and right vertical bars
w := strings.Repeat(m, slen(v)+2)
d.push(c.Sprintf("%s%s%s", l, w, r))
}
}
// middle draws the item's value wrapped between pipes
func (d drawing) middle(from, to int) {
for i, v := range over(d.backer, from, to) {
p, c := "║", ColorSlice
if d.backing(from + i) {
if !PrintBacking {
break
}
p, c = "|", ColorBacker
}
// Left Vertical : %-2[3]s
// Item Value : %-[1]*v
// (its width is dynamically adjusted: slen(v))
// Right Vertical: %2[3]s
d.push(
c.Sprintf("%-2[3]s%-[1]*v%2[3]s",
slen(v), v, p),
)
}
}
// pointer simplifies the pointer data for easy viewing
func (d drawing) pointer(index int) int64 {
var s int64 = 1
if NormalizePointers && d.slice.Len() > 0 {
s = int64(d.backer.Index(index).Type().Size())
}
p := int64(d.slice.Pointer())
if index != 0 && d.slice.Len() > 0 {
p = int64(d.backer.Index(index).Addr().Pointer())
}
trim := int64(10000) // get rid of the leading digits
if PrintHex {
// do not trim the digits: p % p + 1 = p
trim = p + 1
}
return (p / s) % trim
}
// backing is true if the index belongs to the backing array
func (d drawing) backing(index int) bool {
return index >= d.slice.Len()
}
// push appends a new string into the drawing's buffer
func (d drawing) push(s string) {
d.buf.WriteString(s)
}
// pushNewline appends a newline into the drawing's buffer
func (d drawing) pushNewline() {
d.push("\n")
}
// paddings finds out the left and right paddings from two values' lengths
func paddings(a, b int) (lp int, rp int) {
// middle length
mli := a / 2
// total width
w := b + 4
// left and right paddings
lp = w/2 - mli
rp = w - lp
if lp < 0 {
lp = 0
}
if rp < 0 {
rp = 0
}
return
}
// slen gets the length of a utf-8 string.
// this is a func because it doesn't use the struct's data. it's stateless.
func slen(s string) int {
return utf8.RuneCountInString(s)
}
// enough is true if the current is > MaxElements
func enough(index int) bool {
return MaxElements != 0 && index >= MaxElements
}
// over range overs a reflect.Value as []string
// TODO (@inanc): Fix the unnecessary allocation
func over(slice reflect.Value, from, to int) []string {
size := to - from
if MaxElements != 0 && size >= MaxElements {
size = MaxElements
}
values := make([]string, 0, size)
if l := slice.Len(); to > l {
to = l
}
for i := from; i < to; i++ {
if enough(i) {
break
}
v := slice.Index(i)
s := fmt.Sprintf("%v", v)
// this will be overwritten if PrettyByteRune
if PrintBytesHex {
if b, ok := v.Interface().(byte); ok {
s = fmt.Sprintf("%02x", b)
}
}
if PrettyByteRune {
var (
r rune
isRune bool
)
switch v.Interface().(type) {
case byte:
r = rune(v.Uint())
isRune = !PrintBytesHex && true
case rune:
r = rune(v.Int())
isRune = true
case string:
str := v.String()
var buf strings.Builder
for _, r := range str {
buf.WriteRune(toSpace(r))
}
s = buf.String()
}
if isRune {
s = string(toSpace(r))
}
}
values = append(values, s)
}
return values
}
func toSpace(r rune) (out rune) {
out = r
switch {
case unicode.IsSpace(r), unicode.IsControl(r):
out = SpaceCharacter
}
return
}
func makeSlice(v reflect.Value) reflect.Value {
slice := reflect.MakeSlice(reflect.SliceOf(v.Type()), 0, 1)
slice = reflect.Append(slice, v)
return slice
}