-
Notifications
You must be signed in to change notification settings - Fork 24
/
fmt.go
239 lines (212 loc) · 4.94 KB
/
fmt.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
package gothic
import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// A special type which can be passed to Interpreter.Eval method family as the
// only argument and in that case you can use named abbreviations within format
// tags.
type ArgMap map[string]interface{}
func split_tag(tag string) (abbrev, format string) {
abbrev = tag
format = ""
if i := strings.Index(tag, "%"); i != -1 {
abbrev = tag[:i]
format = tag[i:]
}
return
}
func write_arg_quoted(buf *bytes.Buffer, arg interface{}) {
switch a := arg.(type) {
case string:
quote(buf, a)
case error:
quote(buf, a.Error())
case fmt.Stringer:
quote(buf, a.String())
default:
// TODO: it doesn't work in all cases, we still need to escape
// various $ { } [ ] symbols
fmt.Fprintf(buf, "%q", arg)
}
}
func write_arg(buf *bytes.Buffer, arg interface{}, format string) {
if format != "" {
if format == "%q" {
write_arg_quoted(buf, arg)
return
} else {
fmt.Fprintf(buf, format, arg)
}
} else {
fmt.Fprint(buf, arg)
}
}
func write_tag(buf *bytes.Buffer, tag string, counter *int, args []interface{}) error {
argnum := 0
if tag == "" || strings.HasPrefix(tag, "%") {
// no abbrev, means use counter
}
abbrev, format := split_tag(tag)
if abbrev == "" {
// no abbrev, means use the counter
argnum = *counter
(*counter)++
} else {
// non-empty abbrev, let's convert it to the integer
i, err := strconv.ParseInt(abbrev, 10, 16)
if err != nil {
return errors.New("gothic.sprintf: not-a-number tag abbrev")
}
argnum = int(i)
}
if argnum < 0 || argnum >= len(args) {
return fmt.Errorf("gothic.sprintf: there is no argument with index %d", argnum)
}
write_arg(buf, args[argnum], format)
return nil
}
func write_tag_argmap(buf *bytes.Buffer, tag string, argmap ArgMap) error {
if tag == "" || strings.HasPrefix(tag, "%") {
return errors.New("gothic.sprintf: empty format tag abbrev on gothic.ArgMap call form")
}
key, format := split_tag(tag)
arg, found := argmap[key]
if !found {
return fmt.Errorf("gothic.sprintf: no argument %q in the ArgMap", key)
}
write_arg(buf, arg, format)
return nil
}
func sprintf(buf *bytes.Buffer, format string, args ...interface{}) error {
if len(args) == 0 {
// quick path
buf.WriteString(format)
return nil
}
counter := 0
argmap := false
argmapvar := ArgMap(nil)
if len(args) == 1 {
argmapvar, argmap = args[0].(ArgMap)
}
offset := 0
for {
i := strings.Index(format[offset:], "%{")
if i == -1 {
// no more format tags, write the rest and return
buf.WriteString(format[offset:])
break
}
// write everything before the formatter
buf.WriteString(format[offset : offset+i])
// now let's fine the ending "}"
b := offset + i + 2
j := strings.Index(format[b:], "}")
if j == -1 {
return errors.New("gothic.sprintf: missing enclosing bracket in a formatter tag")
}
e := b + j
var err error
tag := format[b:e]
if argmap {
err = write_tag_argmap(buf, tag, argmapvar)
} else {
err = write_tag(buf, tag, &counter, args)
}
if err != nil {
return err
}
offset = e + 1
}
return nil
}
func quote_rune(buf *bytes.Buffer, r rune, size int) {
const lowerhex = "0123456789abcdef"
if size == 1 && r == utf8.RuneError {
// invalid rune, write the byte as is
buf.WriteString(`\x`)
buf.WriteByte(lowerhex[r>>4])
buf.WriteByte(lowerhex[r&0xF])
return
}
// first check for special TCL escaping cases
switch r {
case '{', '}', '[', ']', '"', '$', '\\':
buf.WriteString("\\")
buf.WriteRune(r)
return
}
// other printable characters
if unicode.IsPrint(r) {
buf.WriteRune(r)
return
}
// non-printable characters
switch r {
case '\a':
buf.WriteString(`\a`)
case '\b':
buf.WriteString(`\b`)
case '\f':
buf.WriteString(`\f`)
case '\n':
buf.WriteString(`\n`)
case '\r':
buf.WriteString(`\r`)
case '\t':
buf.WriteString(`\t`)
case '\v':
buf.WriteString(`\v`)
default:
switch {
case r < ' ':
buf.WriteString(`\x`)
buf.WriteByte(lowerhex[r>>4])
buf.WriteByte(lowerhex[r&0xF])
case r >= 0x10000:
r = 0xFFFD
fallthrough
case r < 0x10000:
buf.WriteString(`\u`)
for s := 12; s >= 0; s -= 4 {
buf.WriteByte(lowerhex[r>>uint(s)&0xF])
}
}
}
}
func quote(buf *bytes.Buffer, s string) {
buf.WriteString(`"`)
size := 0
for offset := 0; offset < len(s); offset += size {
r := rune(s[offset])
size = 1
if r >= utf8.RuneSelf {
r, size = utf8.DecodeRuneInString(s[offset:])
}
quote_rune(buf, r, size)
}
buf.WriteString(`"`)
}
// Works exactly like Eval("%{%q}"), but instead of evaluating returns a quoted
// string.
func Quote(s string) string {
var tmp bytes.Buffer
quote(&tmp, s)
return tmp.String()
}
// Quotes the rune just like if it was passed through Quote, the result is the
// same as: Quote(string(r)).
func QuoteRune(r rune) string {
var tmp bytes.Buffer
size := utf8.RuneLen(r)
tmp.WriteString(`"`)
quote_rune(&tmp, r, size)
tmp.WriteString(`"`)
return tmp.String()
}