-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfmt.go
41 lines (37 loc) · 836 Bytes
/
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
package slog
import (
"regexp"
"strconv"
)
var formatterRe = regexp.MustCompile(`%` +
`[\+\-# 0]*` + // Flags
`(?:\d*\.|\[(\d+)\]\*\.)?(?:\d+|\[(\d+)\]\*)?` + // Width and precision
`(?:\[(\d+)\])?` + // Argument index
`[vTtbcdoOqxXUeEfFgGsp%]`, // Verb
)
func countFmtOperands(input string) int {
count, point := 0, 0
for _, match := range formatterRe.FindAllStringSubmatch(input, -1) {
if match[0] == "%%" {
// Deliberately match the regexp on %% (to prevent overlapping matches), but stop them here
continue
}
for _, flag := range match[1:] {
if flag == "" {
continue
} else if i, err := strconv.Atoi(flag); err == nil && i > 0 {
point = i
if point > count {
count = point
}
}
}
if match[3] == "" {
point++
}
if point > count {
count = point
}
}
return count
}