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()
}

// 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:]

	 := bytes.IndexByte([]byte(), ',')
	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
}