Skip to content

Commit

Permalink
simplify state machine for parsing format string for format()
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 12, 2024
1 parent 9f0b113 commit 554c6a6
Showing 1 changed file with 10 additions and 33 deletions.
43 changes: 10 additions & 33 deletions expr_sema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,25 @@ func ordinal(i int) string {
// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions#format
func parseFormatFuncSpecifiers(f string, n int) map[int]struct{} {
ret := make(map[int]struct{}, n)

type state int
const (
init state = iota // Initial state
brace // {
digit // 0..9
)

var cur state
var start int
start := -1
for i, r := range f {
switch cur {
case init:
switch r {
case '{':
cur = brace
if r == '{' {
if start == i {
start = -1 // When the '{' is escaped like '{{'
} else {
start = i + 1 // `+ 1` because `i` points char '{'
}
case brace:
switch {
case '0' <= r && r <= '9':
cur = digit
default:
cur = init
} else if start >= 0 {
if '0' <= r && r <= '9' {
continue
}
case digit:
switch {
case '0' <= r && r <= '9':
// Do nothing
case r == '{':
cur = brace
start = i + 1
case r == '}':
if r == '}' && start < i {
i, _ := strconv.Atoi(f[start:i])
ret[i] = struct{}{}
cur = init
default:
cur = init
}
start = -1 // Done
}
}

return ret
}

Expand Down

0 comments on commit 554c6a6

Please sign in to comment.