Debian Codegraph @

internal/fmtstr/parse.go

raw symbols SCIP
1 // Copyright 2024 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package fmtstr defines a parser for format strings as used by [fmt.Printf].
6 package fmtstrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/
7
8 import (
9 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
10 "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/"
11 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
12 "unicode/utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/"
13 )
14
15 // Operation holds the parsed representation of a printf operation such as "%3.*[4]d".
16 // It is constructed by [Parse].
17 type Operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation# struct {
18 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text. string // full text of the operation, e.g. "%[2]*.3d"
19 Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Verb. Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb# // verb specifier, guaranteed to exist, e.g., 'd' in '%[1]d'
20 Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Range. Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range# // the range of Text within the overall format string
21 Flagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Flags. string // formatting flags, e.g. "-0"
22 Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Width. Sizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size# // width specifier, e.g., '3' in '%3d'
23 Precscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Prec. Sizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size# // precision specifier, e.g., '.4' in '%.4f'
24 }
25
26 // Size describes an optional width or precision in a format operation.
27 // It may represent no value, a literal number, an asterisk, or an indexed asterisk.
28 type Sizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size# struct {
29 // At most one of these two fields is non-negative.
30 Fixedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Fixed. int // e.g. 4 from "%4d", otherwise -1
31 Dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Dynamic. int // index of argument providing dynamic size (e.g. %*d or %[3]*d), otherwise -1
32
33 Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Index. int // If the width or precision uses an indexed argument (e.g. 2 in %[2]*d), this is the index, otherwise -1
34 Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range. Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range# // position of the size specifier within the operation
35 }
36
37 // Verb represents the verb character of a format operation (e.g., 'd', 's', 'f').
38 // It also includes positional information and any explicit argument indexing.
39 type Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb# struct {
40 Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Verb. rune
41 Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Range. Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range# // positional range of the verb in the format string
42 Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Index. int // index of an indexed argument, (e.g. 2 in %[2]d), otherwise -1
43 ArgIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#ArgIndex. int // argument index (0-based) associated with this verb, relative to CallExpr
44 }
45
46 // byte offsets of format string
47 type Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range# struct {
48 Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start., Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End. int
49 }
50
51 // Parse takes a format string and its index in the printf-like call,
52 // parses out all format operations, returns a slice of parsed
53 // [Operation] which describes flags, width, precision, verb, and argument indexing,
54 // or an error if parsing fails.
55 //
56 // All error messages are in predicate form ("call has a problem")
57 // so that they may be affixed into a subject ("log.Printf ").
58 //
59 // The flags will only be a subset of ['#', '0', '+', '-', ' '].
60 // It does not perform any validation of verbs, nor the
61 // existence of corresponding arguments (obviously it can't). The provided format string may differ
62 // from the one in CallExpr, such as a concatenated string or a string
63 // referred to by the argument in the CallExpr.
64 func Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Parse().(format string, idx int) ([]*Operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#, error) {
65 if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(format, "%") {
66 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("call has arguments but no formatting directives")
67 }
68
69 firstArg := idx + 1 // Arguments are immediately after format string.
70 argNum := firstArg
71 var operations []*Operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#
72 for i, w := 0, 0; i < len(format); i += w {
73 w = 1
74 if format[i] != '%' {
75 continue
76 }
77 state, err := parseOperationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/parseOperation().(format[i:], firstArg, argNum)
78 if err != nil {
79 return nil, err
80 }
81
82 state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..addOffsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#addOffset().(i)
83 operations = append(operations, state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation.)
84
85 w = len(state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.)
86 // Do not waste an argument for '%'.
87 if state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Verb..Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Verb. != '%' {
88 argNum = state.argNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#argNum. + 1
89 }
90 }
91 return operations, nil
92 }
93
94 // Internal parsing state to operation.
95 type statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state# struct {
96 operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation. *Operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#
97 firstArgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#firstArg. int // index of the first argument after the format string
98 argNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#argNum. int // which argument we're expecting to format now
99 hasIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#hasIndex. bool // whether the argument is indexed
100 indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#index. int // the encountered index
101 indexPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPos. int // the encountered index's offset
102 indexPendingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPending. bool // whether we have an indexed argument that has not resolved
103 nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. int // number of bytes of the format string consumed
104 }
105
106 // parseOperation parses one format operation starting at the given substring `format`,
107 // which should begin with '%'. It returns a fully populated state or an error
108 // if the operation is malformed. The firstArg and argNum parameters help determine how
109 // arguments map to this operation.
110 //
111 // Parse sequence: '%' -> flags -> {[N]* or width} -> .{[N]* or precision} -> [N] -> verb.
112 func parseOperationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/parseOperation().(format string, firstArg, argNum int) (*statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#, error) {
113 state := &statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#{
114 operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation.: &Operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#{
115 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.: format,
116 Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Width.: Sizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#{
117 Fixedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Fixed.: -1,
118 Dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Dynamic.: -1,
119 Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Index.: -1,
120 },
121 Precscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Prec.: Sizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#{
122 Fixedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Fixed.: -1,
123 Dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Dynamic.: -1,
124 Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Index.: -1,
125 },
126 },
127 firstArgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#firstArg.: firstArg,
128 argNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#argNum.: argNum,
129 hasIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#hasIndex.: false,
130 indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#index.: 0,
131 indexPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPos.: 0,
132 indexPendingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPending.: false,
133 nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.: len("%"), // There's guaranteed to be a percent sign.
134 }
135 // There may be flags.
136 state.parseFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parseFlags().()
137 // There may be an index.
138 if err := state.parseIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parseIndex().(); err != nil {
139 return nil, err
140 }
141 // There may be a width.
142 state.parseSizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parseSize().(Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Width.)
143 // There may be a precision.
144 if err := state.parsePrecisionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parsePrecision().(); err != nil {
145 return nil, err
146 }
147 // Now a verb, possibly prefixed by an index (which we may already have).
148 if !state.indexPendingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPending. {
149 if err := state.parseIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parseIndex().(); err != nil {
150 return nil, err
151 }
152 }
153 if state.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. == len(state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.) {
154 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("format %s is missing verb at end of string", state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.)
155 }
156 verb, w := utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.DecodeRuneInStringscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/DecodeRuneInString().(state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[state.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.:])
157
158 // Ensure there must be a verb.
159 if state.indexPendingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPending. {
160 state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Verb. = Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#{
161 Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Verb.: verb,
162 Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Range.: Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#{
163 Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start.: state.indexPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPos.,
164 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End.: state.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. + w,
165 },
166 Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Index.: state.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#index.,
167 ArgIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#ArgIndex.: state.argNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#argNum.,
168 }
169 } else {
170 state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Verb. = Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#{
171 Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Verb.: verb,
172 Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Range.: Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#{
173 Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start.: state.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.,
174 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End.: state.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. + w,
175 },
176 Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Index.: -1,
177 ArgIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#ArgIndex.: state.argNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#argNum.,
178 }
179 }
180
181 state.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. += w
182 state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text. = state.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[:state.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.]
183 return state, nil
184 }
185
186 // addOffset adjusts the recorded positions in Verb, Width, Prec, and the
187 // operation's overall Range to be relative to the position in the full format string.
188 func (s *Operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#) addOffsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#addOffset().(parsedLen int) {
189 s.Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Verb..Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Range..Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start. += parsedLen
190 s.Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Verb..Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Range..Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End. += parsedLen
191
192 s.Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Range..Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start. = parsedLen
193 s.Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Range..Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End. = s.Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Verb..Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Verb#Range..Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End.
194
195 // one of Fixed or Dynamic is non-negative means existence.
196 if s.Precscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Prec..Fixedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Fixed. != -1 || s.Precscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Prec..Dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Dynamic. != -1 {
197 s.Precscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Prec..Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range..Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start. += parsedLen
198 s.Precscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Prec..Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range..Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End. += parsedLen
199 }
200 if s.Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Width..Fixedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Fixed. != -1 || s.Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Width..Dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Dynamic. != -1 {
201 s.Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Width..Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range..Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start. += parsedLen
202 s.Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Width..Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range..Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End. += parsedLen
203 }
204 }
205
206 // parseFlags accepts any printf flags.
207 func (s *statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#) parseFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parseFlags().() {
208 s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Flagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Flags. = prefixOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/prefixOf().(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.:], "#0+- ")
209 s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. += len(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Flagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Flags.)
210 }
211
212 // prefixOf returns the prefix of s composed only of runes from the specified set.
213 func prefixOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/prefixOf().(s, set string) string {
214 rest := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimLeftscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimLeft().(s, set)
215 return s[:len(s)-len(rest)]
216 }
217
218 // parseIndex parses an argument index of the form "[n]" that can appear
219 // in a printf operation (e.g., "%[2]d"). Returns an error if syntax is
220 // malformed or index is invalid.
221 func (s *statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#) parseIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parseIndex().() error {
222 if s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. == len(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.) || s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.] != '[' {
223 return nil
224 }
225 // Argument index present.
226 s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.++ // skip '['
227 start := s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.
228 if num, ok := s.scanNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#scanNum().(); ok {
229 // Later consumed/stored by a '*' or verb.
230 s.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#index. = num
231 s.indexPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPos. = start - 1
232 }
233
234 ok := true
235 if s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. == len(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.) || s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. == start || s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.] != ']' {
236 ok = false // syntax error is either missing "]" or invalid index.
237 s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[start:], "]")
238 if s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. < 0 {
239 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("format %s is missing closing ]", s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.)
240 }
241 s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. = s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. + start
242 }
243 arg32, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseInt().(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[start:s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.], 10, 32)
244 if err != nil || !ok || arg32 <= 0 {
245 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("format has invalid argument index [%s]", s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[start:s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.])
246 }
247
248 s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.++ // skip ']'
249 arg := int(arg32)
250 arg += s.firstArgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#firstArg. - 1 // We want to zero-index the actual arguments.
251 s.argNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#argNum. = arg
252 s.hasIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#hasIndex. = true
253 s.indexPendingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPending. = true
254 return nil
255 }
256
257 // scanNum advances through a decimal number if present, which represents a [Size] or [Index].
258 func (s *statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#) scanNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#scanNum().() (int, bool) {
259 start := s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.
260 for ; s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. < len(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.); s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.++ {
261 c := s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.]
262 if c < '0' || '9' < c {
263 if start < s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. {
264 num, _ := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseInt().(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[start:s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.], 10, 32)
265 return int(num), true
266 } else {
267 return 0, false
268 }
269 }
270 }
271 return 0, false
272 }
273
274 type sizeTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/sizeType# int
275
276 const (
277 Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Width. sizeTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/sizeType# = iota
278 Precisionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Precision.
279 )
280
281 // parseSize parses a width or precision specifier. It handles literal numeric
282 // values (e.g., "%3d"), asterisk values (e.g., "%*d"), or indexed asterisk values (e.g., "%[2]*d").
283 func (s *statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#) parseSizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parseSize().(kind sizeTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/sizeType#) {
284 if s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. < len(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.) && s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.] == '*' {
285 s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.++
286 if s.indexPendingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPending. {
287 // Absorb it.
288 s.indexPendingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPending. = false
289 size := Sizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#{
290 Fixedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Fixed.: -1,
291 Dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Dynamic.: s.argNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#argNum.,
292 Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Index.: s.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#index.,
293 Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range.: Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#{
294 Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start.: s.indexPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#indexPos.,
295 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End.: s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.,
296 },
297 }
298 switch kind {
299 case Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Width.:
300 s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Width. = size
301 case Precisionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Precision.:
302 // Include the leading '.'.
303 size.Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range..Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start. -= len(".")
304 s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Precscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Prec. = size
305 default:
306 panic(kind)
307 }
308 } else {
309 // Non-indexed asterisk: "%*d".
310 size := Sizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#{
311 Dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Dynamic.: s.argNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#argNum.,
312 Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Index.: -1,
313 Fixedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Fixed.: -1,
314 Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range.: Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#{
315 Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start.: s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. - 1,
316 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End.: s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.,
317 },
318 }
319 switch kind {
320 case Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Width.:
321 s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Width. = size
322 case Precisionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Precision.:
323 // For precision, include the '.' in the range.
324 size.Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range..Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start. -= 1
325 s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Precscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Prec. = size
326 default:
327 panic(kind)
328 }
329 }
330 s.argNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#argNum.++
331 } else { // Literal number, e.g. "%10d"
332 start := s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.
333 if num, ok := s.scanNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#scanNum().(); ok {
334 size := Sizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#{
335 Fixedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Fixed.: num,
336 Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Index.: -1,
337 Dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Dynamic.: -1,
338 Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range.: Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#{
339 Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start.: start,
340 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#End.: s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.,
341 },
342 }
343 switch kind {
344 case Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Width.:
345 s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Widthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Width. = size
346 case Precisionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Precision.:
347 // Include the leading '.'.
348 size.Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Size#Range..Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Range#Start. -= 1
349 s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Precscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Prec. = size
350 default:
351 panic(kind)
352 }
353 }
354 }
355 }
356
357 // parsePrecision checks if there's a precision specified after a '.' character.
358 // If found, it may also parse an index or an asterisk. Returns an error if any index
359 // parsing fails.
360 func (s *statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#) parsePrecisionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parsePrecision().() error {
361 // If there's a period, there may be a precision.
362 if s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes. < len(s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.) && s.operationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#operation..Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Operation#Text.[s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.] == '.' {
363 s.nbytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#nbytes.++
364 if err := s.parseIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parseIndex().(); err != nil {
365 return err
366 }
367 s.parseSizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/state#parseSize().(Precisionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/fmtstr`/Precision.)
368 }
369 return nil
370 }