x/build/blob: move most commit value checks to emit func

This commit is contained in:
Blake Mizerany 2024-04-03 22:55:53 -07:00
parent 6d2da77ce2
commit 2e1ea6ecaa

View File

@ -220,19 +220,21 @@ func Parts(s string) iter.Seq2[Kind, string] {
s = s[len("https://"):] s = s[len("https://"):]
} }
emit := func(kind Kind, value string) bool {
if !isValidPart(value) {
return false
}
return yield(kind, value)
}
state, j := Build, len(s) state, j := Build, len(s)
for i := len(s) - 1; i >= 0; i-- { for i := len(s) - 1; i >= 0; i-- {
c := s[i] switch s[i] {
switch c {
case '+': case '+':
switch state { switch state {
case Build: case Build:
v := s[i+1 : j] v := strings.ToUpper(s[i+1 : j])
if v == "" { if !emit(Build, v) {
return
}
v = strings.ToUpper(v)
if !yield(Build, v) {
return return
} }
state, j = Tag, i state, j = Tag, i
@ -242,12 +244,8 @@ func Parts(s string) iter.Seq2[Kind, string] {
case ':': case ':':
switch state { switch state {
case Build, Tag: case Build, Tag:
v := s[i+1 : j] if emit(Tag, s[i+1:j]) {
if v == "" { state, j = Tag, i
return
}
if !yield(Tag, v) {
return
} }
state, j = Name, i state, j = Name, i
default: default:
@ -256,12 +254,12 @@ func Parts(s string) iter.Seq2[Kind, string] {
case '/': case '/':
switch state { switch state {
case Name, Tag, Build: case Name, Tag, Build:
if !yield(Name, s[i+1:j]) { if !emit(Name, s[i+1:j]) {
return return
} }
state, j = Namespace, i state, j = Namespace, i
case Namespace: case Namespace:
if !yield(Namespace, s[i+1:j]) { if !emit(Namespace, s[i+1:j]) {
return return
} }
state, j = Domain, i state, j = Domain, i