邓心一 6 vuotta sitten
vanhempi
commit
1d360c6345
1 muutettua tiedostoa jossa 32 lisäystä ja 11 poistoa
  1. 32 11
      hard/591.tag-validator.go

+ 32 - 11
hard/591.tag-validator.go

@@ -1,22 +1,43 @@
 func isValid(code string) bool {
 	st := make([]string, 0)
-	for i, r := range code {
-		if 0 < i && len(st) == 0 {
+	for i := 0; i < len(code); i++ {
+		if 0 < i && len(st) == 0 { // Unmatched content
 			return false
 		}
-		if code[i:i+9] == "<![CDATA[" { // Parse cdata
-			i = strings.Index(code[i+8:], "]]>")
-			if i < 0 {
+		if strings.HasPrefix(code[i:], "<![CDATA[") { // Parse cdata
+			j := strings.Index(code[i+9:], "]]>")
+			if j < 0 {
 				return false
 			}
+			i += j + 11
+		} else if strings.HasPrefix(code[i:], "</") { // Parse end
 			i += 2
-		} else if code[i:i+2] == "</" {
-			name := 
-		} else if r == '<' {
-			
+			j := strings.Index(code[i:], ">")
+			if j < 0 {
+				return false
+			}
+			name := code[i : i+j]
+			if l := len(st); l == 0 || st[l-1] != name {
+				return false
+			} else {
+				st = st[:l-1]
+			}
+			i += j
+		} else if code[i] == '<' { // Parse begin
+			i += 1
+			j := strings.Index(code[i:], ">")
+			if j < 1 || 9 < j {
+				return false
+			}
+			name := code[i : i+j]
+			for _, r := range name {
+				if r < 'A' || 'Z' < r {
+					return false
+				}
+			}
+			st = append(st, name)
+			i += j
 		}
 	}
 	return len(st) == 0
 }
-
-