Skip to content

Commit 1170fdd

Browse files
committed
simplify state machine for parsing format string for format()
1 parent 9f0b113 commit 1170fdd

File tree

1 file changed

+10
-36
lines changed

1 file changed

+10
-36
lines changed

expr_sema.go

+10-36
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package actionlint
22

33
import (
44
"fmt"
5-
"regexp"
65
"strconv"
76
"strings"
87
)
98

10-
var reFormatPlaceholder = regexp.MustCompile(`{\d+}`)
11-
129
func ordinal(i int) string {
1310
suffix := "th"
1411
switch i % 10 {
@@ -32,48 +29,25 @@ func ordinal(i int) string {
3229
// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions#format
3330
func parseFormatFuncSpecifiers(f string, n int) map[int]struct{} {
3431
ret := make(map[int]struct{}, n)
35-
36-
type state int
37-
const (
38-
init state = iota // Initial state
39-
brace // {
40-
digit // 0..9
41-
)
42-
43-
var cur state
44-
var start int
32+
start := -1
4533
for i, r := range f {
46-
switch cur {
47-
case init:
48-
switch r {
49-
case '{':
50-
cur = brace
34+
if r == '{' {
35+
if start == i {
36+
start = -1 // When the '{' is escaped like '{{'
37+
} else {
5138
start = i + 1 // `+ 1` because `i` points char '{'
5239
}
53-
case brace:
54-
switch {
55-
case '0' <= r && r <= '9':
56-
cur = digit
57-
default:
58-
cur = init
40+
} else if start >= 0 {
41+
if '0' <= r && r <= '9' {
42+
continue
5943
}
60-
case digit:
61-
switch {
62-
case '0' <= r && r <= '9':
63-
// Do nothing
64-
case r == '{':
65-
cur = brace
66-
start = i + 1
67-
case r == '}':
44+
if r == '}' && start < i {
6845
i, _ := strconv.Atoi(f[start:i])
6946
ret[i] = struct{}{}
70-
cur = init
71-
default:
72-
cur = init
7347
}
48+
start = -1 // Done
7449
}
7550
}
76-
7751
return ret
7852
}
7953

0 commit comments

Comments
 (0)