-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
format.go
76 lines (73 loc) · 1.47 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
package stdio
import "strings"
type formatWord struct {
Str string
Verb bool
}
func parseFormat(format string) []formatWord {
var (
words []formatWord
placeholder = false
cur []rune
)
reset := func() {
cur = cur[:0]
placeholder = false
}
pushWord := func(verb bool) {
if len(cur) != 0 {
words = append(words, formatWord{Str: string(cur), Verb: verb})
}
reset()
}
for _, b := range format {
if b == '%' {
if placeholder {
if len(cur) == 1 { // %%
cur = append(cur, '%')
pushWord(true) // fake verb
continue
}
// end the current placeholder, start a new one
pushWord(true)
}
// start a new placeholder
pushWord(false)
placeholder = true
cur = append(cur, '%')
continue
}
if !placeholder {
// continue the string
cur = append(cur, b)
continue
}
// in placeholder
switch b {
default:
if strings.IndexRune("1234567890#+-*. l", b) >= 0 {
// consider a part of the placeholder
cur = append(cur, b)
continue
}
// other rune: stop the placeholder
pushWord(true)
cur = append(cur, b)
continue
case 'i': // signed, allows octal and hex
case 'd': // signed, only decimal
case 'u': // unsigned, only decimal
case 'o': // octal
case 'x': // hex
case 'f', 'e', 'g', 'F', 'E', 'G': // float
case 'c': // char
case 'p': // ptr
case 's': // string
case 'S': // wstring
}
cur = append(cur, b)
pushWord(true)
}
pushWord(placeholder)
return words
}