package httpsfv
import (
"errors"
"fmt"
"io"
)
var ErrInvalidKeyFormat = errors .New ("invalid key format" )
func isKeyChar(c byte ) bool {
if isLowerCaseAlpha (c ) || isDigit (c ) {
return true
}
switch c {
case '_' , '-' , '.' , '*' :
return true
}
return false
}
func checkKey(k string ) error {
if len (k ) == 0 {
return fmt .Errorf ("a key cannot be empty: %w" , ErrInvalidKeyFormat )
}
if !isLowerCaseAlpha (k [0 ]) && k [0 ] != '*' {
return fmt .Errorf ("a key must start with a lower case alpha character or *: %w" , ErrInvalidKeyFormat )
}
for i := 1 ; i < len (k ); i ++ {
if !isKeyChar (k [i ]) {
return fmt .Errorf ("the character %c isn't allowed in a key: %w" , k [i ], ErrInvalidKeyFormat )
}
}
return nil
}
func marshalKey(b io .StringWriter , k string ) error {
if err := checkKey (k ); err != nil {
return err
}
_ , err := b .WriteString (k )
return err
}
func parseKey(s *scanner ) (string , error ) {
if s .eof () {
return "" , &UnmarshalError {s .off , ErrInvalidKeyFormat }
}
c := s .data [s .off ]
if !isLowerCaseAlpha (c ) && c != '*' {
return "" , &UnmarshalError {s .off , ErrInvalidKeyFormat }
}
start := s .off
s .off ++
for !s .eof () {
if !isKeyChar (s .data [s .off ]) {
break
}
s .off ++
}
return s .data [start :s .off ], nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .