forked from lytics/confl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lex_helper.go
93 lines (90 loc) · 1.56 KB
/
lex_helper.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
package confl
import "strings"
func stripSlashes(s string) string {
var builder strings.Builder
size := len(s)
var skip bool
var skipOnly bool
for i, ch := range s {
if skip {
builder.WriteRune(ch)
skip = false
continue
}
if skipOnly {
skipOnly = false
continue
}
if ch == '\\' {
if i+1 < size {
switch s[i+1] {
case '\\':
skip = true
case 't':
builder.WriteRune('\t')
skipOnly = true
case 'n':
builder.WriteRune('\n')
skipOnly = true
case 'r':
builder.WriteRune('\r')
skipOnly = true
case 'u', 'U', 'x':
builder.WriteRune(ch)
}
}
continue
}
builder.WriteRune(ch)
}
return builder.String()
}
func addSlashes(s string, b ...rune) string {
var builder strings.Builder
size := len(s)
for i, v := range s {
if v == '\\' {
start := i + 1
var ok bool
if start < size {
switch s[start] {
case 'u':
if start+5 < size {
for _, r := range s[start+1 : start+5] {
if !isHexadecimal(r) {
ok = false
break
}
ok = true
}
}
case 'U':
if start+9 < size {
for _, r := range s[start+1 : start+9] {
if !isHexadecimal(r) {
ok = false
break
}
ok = true
}
}
case 'x':
if start+3 < size {
for _, r := range s[start+1 : start+3] {
if !isHexadecimal(r) {
ok = false
break
}
ok = true
}
}
}
}
if !ok {
builder.WriteRune(v)
}
}
builder.WriteRune(v)
}
return builder.String()
}