go-rfc2396/domainlabel/bytes.go

44 lines
580 B
Go
Raw Permalink Normal View History

2023-12-08 14:18:34 +00:00
package domainlabel
import (
"sourcecode.social/reiver/go-rfc2396/alphanum"
)
func Bytes(p []byte) (result []byte, rest []byte, ok bool) {
length := len(p)
if length < 1 {
return nil, p, false
}
{
p0 := p[0]
if !alphanum.ByteIs(p0) {
return nil, p, false
}
}
{
for i,b := range p {
if !alphanum.ByteIs(b) && '-' != b {
if !alphanum.ByteIs(p[i-1]) {
return p[:i-1], p[i-1:], true
}
return p[:i], p[i:], true
}
}
if !alphanum.ByteIs(p[length-1]) {
return p[:length-1], p[length-1:], true
}
return p, nil, true
}
}