This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformat.go
109 lines (98 loc) · 1.91 KB
/
format.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
package uuid
type formatDetails struct {
peakCount uint
usedCount uint
padCount uint
needPrePad bool
needPostPad bool
hasSecond bool
first bits
second bits
}
func formatStudy(verb rune, hasPlus, hasSharp, hasMinus, hasWidth bool, width uint, x bits) formatDetails {
var d formatDetails
y := x.just(bitValid | bitTextIsDense | bitTextIsModeY | bitTextIsModeX)
switch verb {
case 'd':
d.first = bitValid | bitTextIsDense
case 's', 'q':
if hasPlus {
d.first = bitValid | bitTextIsDense
} else if hasSharp {
d.first = bitValid | textModeCanonical
} else {
d.first = y
}
if verb == 'q' {
d.first |= bitReserved
}
case 'v':
if hasPlus {
d.first = bitValid | bitTextIsDense | bitReserved
d.second = bitValid | textModeBracketed
d.hasSecond = true
} else if hasSharp {
d.first = bitValid | textModeCanonical
} else {
d.first = y
}
default:
d.first = y
}
need := func(n uint) {
d.peakCount = d.usedCount + n
}
use := func(n uint) {
d.usedCount += n
if d.peakCount < d.usedCount {
d.peakCount = d.usedCount
}
}
add := func(x bits) {
used, peak := x.textLength()
need(peak)
use(used)
}
if d.first.has(bitReserved) {
use(1)
}
add(d.first)
if d.first.has(bitReserved) {
use(1)
}
if d.hasSecond {
use(1)
add(d.second)
}
if hasWidth && width > d.usedCount {
d.padCount = width - d.usedCount
if hasMinus {
d.needPostPad = true
use(d.padCount)
} else {
d.needPrePad = true
d.peakCount += d.padCount
d.usedCount += d.padCount
}
}
return d
}
func formatApply(w *sliceWriter, in []byte, d formatDetails) {
if d.needPrePad {
w.Fill(' ', d.padCount)
}
if d.first.has(bitReserved) {
w.WriteByte('"')
}
marshalText(w, in, d.first)
if d.first.has(bitReserved) {
w.WriteByte('"')
}
if d.hasSecond {
w.WriteByte(' ')
marshalText(w, in, d.second)
}
if d.needPostPad {
w.Fill(' ', d.padCount)
}
}