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 1170fdd
Showing 1 changed file with 10 additions and 36 deletions.
46 changes: 10 additions & 36 deletions expr_sema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package actionlint

import (
"fmt"
"regexp"
"strconv"
"strings"
)

var reFormatPlaceholder = regexp.MustCompile(`{\d+}`)

func ordinal(i int) string {
suffix := "th"
switch i % 10 {
Expand All @@ -32,48 +29,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 1170fdd

Please sign in to comment.