Source File
params.go
Belonging Package
go.pact.im/x/phcformat
package phcformat
import (
)
// ParamsIterator iterates over comma-separated key=value parameter pairs. Note
// that iterator does not validate characters in parameter’s key and value.
type ParamsIterator struct {
// Name is the name of the current parameter.
Name string
// Value is the value of the current parameter.
Value string
// After is the string with remaining parameters.
After string
// Valid indicates that the iterator is valid. On parse error, After
// will contain unparsed bytes.
Valid bool
}
// IterParams returns a ParamsIterator for the given string.
func ( string) ParamsIterator {
if == "" {
return ParamsIterator{}
}
:= ParamsIterator{After: }
return .Next()
}
// Iter returns an iterator over the remaining key=value parameter pairs.
//
// It yields (name, value) pairs parsed from the input string, stopping when all
// parameters have been consumed or a parsing error occurs (e.g. missing '=' or
// trailing comma).
//
// When iteration ends, the ParamsIterator will contain unparsed trailing data in
// the After field. This allows the caller to inspect whether a parsing error
// occurred and what data remains.
func ( *ParamsIterator) () iter.Seq2[string, string] {
return func( func(string, string) bool) {
for ; .Valid; * = .Next() {
if !(.Name, .Value) {
return
}
}
}
}
// Next advances to the next parameter in the sequence.
func ( ParamsIterator) () ParamsIterator {
.Name, .Value, .After, .Valid = nextParam(.After)
return
}
// nextParam returns the next parameter in s and the remaining string.
func ( string) (, , string, bool) {
:= strings.IndexByte(, '=')
if < 0 {
return "", "", , false
}
, = [:], [+1:]
:= strings.IndexByte(, ',')
if < 0 {
return , , "", true
}
// Consume parameter but make the next iteration invalid if we have a
// trailing comma.
:= 1
if +1 == len() {
= 0
}
, = [:], [+:]
return , , , true
}
The pages are generated with Golds v0.7.6. (GOOS=linux GOARCH=amd64)