Kaynağa Gözat

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

Blake Mizerany 1 yıl önce
ebeveyn
işleme
2e1ea6ecaa
1 değiştirilmiş dosya ile 14 ekleme ve 16 silme
  1. 14 16
      x/build/blob/ref.go

+ 14 - 16
x/build/blob/ref.go

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