-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsebraceattr.go
97 lines (86 loc) · 1.6 KB
/
parsebraceattr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2024 Command Line Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package htmltoken
import "fmt"
func (z *Tokenizer) parseBraceAttr() {
braceCount := 1
inString := false
prevStrBackslash := false
z.pendingAttr[1].start = z.raw.end
for {
ch := z.readByte()
if z.err != nil {
z.pendingAttr[1].end = z.raw.end
return
}
if inString {
if prevStrBackslash {
prevStrBackslash = false
continue
}
if ch == '\\' {
prevStrBackslash = true
continue
}
if ch == '"' {
inString = false
continue
}
continue
}
if ch == '{' {
braceCount++
continue
}
if ch == '"' {
inString = true
continue
}
if ch == '}' {
braceCount--
if braceCount == 0 {
z.pendingAttr[1].end = z.raw.end - 1
return
}
continue
}
}
}
func (z *Tokenizer) parseBraceAttrEx(input string) (string, error) {
var result []rune
braceCount := 0
inString := false
for i := 0; i < len(input); i++ {
ch := rune(input[i])
if inString {
// Handle string escape sequences
if ch == '\\' && i+1 < len(input) {
result = append(result, ch, rune(input[i+1]))
i++
continue
}
if ch == '"' {
inString = false
}
result = append(result, ch)
continue
}
switch ch {
case '{':
braceCount++
case '}':
braceCount--
if braceCount == 0 {
return string(result), nil
}
case '"':
inString = true
}
result = append(result, ch)
}
if braceCount != 0 {
return "", fmt.Errorf("unbalanced braces")
}
return string(result), nil
}