package httpsfv

import (
	
	
	
)

// isExtendedTchar checks if c is a valid token character as defined in the spec.
func isExtendedTchar( byte) bool {
	if isAlpha() || isDigit() {
		return true
	}

	switch  {
	case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`', '|', '~', ':', '/':
		return true
	}

	return false
}

// ErrInvalidTokenFormat is returned when a token format is invalid.
var ErrInvalidTokenFormat = errors.New("invalid token format")

// Token represents a token as defined in
// https://httpwg.org/specs/rfc9651.html#token.
// A specific type is used to distinguish tokens from strings.
type Token string

// marshalSFV serializes as defined in
// https://httpwg.org/specs/rfc9651.html#ser-token.
func ( Token) ( io.StringWriter) error {
	if len() == 0 {
		return fmt.Errorf("a token cannot be empty: %w", ErrInvalidTokenFormat)
	}

	if !isAlpha([0]) && [0] != '*' {
		return fmt.Errorf("a token must start with an alpha character or *: %w", ErrInvalidTokenFormat)
	}

	for  := 1;  < len(); ++ {
		if !isExtendedTchar([]) {
			return fmt.Errorf("the character %c isn't allowed in a token: %w", [], ErrInvalidTokenFormat)
		}
	}

	,  := .WriteString(string())

	return 
}

// parseToken parses as defined in
// https://httpwg.org/specs/rfc9651.html#parse-token.
func parseToken( *scanner) (Token, error) {
	if .eof() || (!isAlpha(.data[.off]) && .data[.off] != '*') {
		return "", &UnmarshalError{.off, ErrInvalidTokenFormat}
	}

	 := .off
	.off++

	for !.eof() {
		if !isExtendedTchar(.data[.off]) {
			break
		}
		.off++
	}

	return Token(.data[:.off]), nil
}