package httpsfv

import (
	
	
	
)

// ErrInvalidStringFormat is returned when a string format is invalid.
var ErrInvalidStringFormat = errors.New("invalid string format")

// marshalString serializes as defined in
// https://httpwg.org/specs/rfc9651.html#ser-string.
func marshalString( *strings.Builder,  string) error {
	if  := .WriteByte('"');  != nil {
		return 
	}

	for  := 0;  < len(); ++ {
		if [] <= '\u001F' || [] >= unicode.MaxASCII {
			return ErrInvalidStringFormat
		}

		switch [] {
		case '"', '\\':
			if  := .WriteByte('\\');  != nil {
				return 
			}
		}

		if  := .WriteByte([]);  != nil {
			return 
		}
	}

	if  := .WriteByte('"');  != nil {
		return 
	}

	return nil
}

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

	var  strings.Builder

	for !.eof() {
		 := .data[.off]
		.off++

		switch  {
		case '\\':
			if .eof() {
				return "", &UnmarshalError{.off, ErrInvalidStringFormat}
			}

			 := .data[.off]
			if  != '"' &&  != '\\' {
				return "", &UnmarshalError{.off, ErrInvalidStringFormat}
			}
			.off++

			if  := .WriteByte();  != nil {
				return "", 
			}

			continue
		case '"':
			return .String(), nil
		default:
			if  <= '\u001F' ||  >= unicode.MaxASCII {
				return "", &UnmarshalError{.off, ErrInvalidStringFormat}
			}

			if  := .WriteByte();  != nil {
				return "", 
			}
		}
	}

	return "", &UnmarshalError{.off, ErrInvalidStringFormat}
}