-
-
Notifications
You must be signed in to change notification settings - Fork 459
/
string.go
165 lines (132 loc) · 2.6 KB
/
string.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package types
import (
"bytes"
"math"
"strings"
"github.com/pkg/errors"
)
// Convert a 1,2 or 3 digit unescaped octal string into the corresponding byte value.
func byteForOctalString(octalBytes []byte) (b byte) {
var j float64
for i := len(octalBytes) - 1; i >= 0; i-- {
b += (octalBytes[i] - '0') * byte(math.Pow(8, j))
j++
}
return
}
// Escape applies all defined escape sequences to s.
func Escape(s string) (*string, error) {
var b bytes.Buffer
for i := 0; i < len(s); i++ {
c := s[i]
switch c {
case 0x0A:
c = 'n'
case 0x0D:
c = 'r'
case 0x09:
c = 't'
case 0x08:
c = 'b'
case 0x0C:
c = 'f'
case '\\', '(', ')':
default:
b.WriteByte(c)
continue
}
b.WriteByte('\\')
b.WriteByte(c)
}
s1 := b.String()
return &s1, nil
}
func escaped(c byte) (bool, byte) {
switch c {
case 'n':
c = 0x0A
case 'r':
c = 0x0D
case 't':
c = 0x09
case 'b':
c = 0x08
case 'f':
c = 0x0C
case '(', ')':
case '0', '1', '2', '3', '4', '5', '6', '7':
return true, c
}
return false, c
}
func regularChar(c byte, esc bool) bool {
return c != 0x5c && !esc
}
// Unescape resolves all escape sequences of s.
func Unescape(s string) ([]byte, error) {
var esc bool
var longEol bool
var octalCode []byte
var b bytes.Buffer
for i := 0; i < len(s); i++ {
c := s[i]
if longEol {
esc = false
longEol = false
// c is completing a 0x5C0D0A line break.
if c == 0x0A {
continue
}
}
if regularChar(c, esc) {
b.WriteByte(c)
continue
}
if c == 0x5c { // '\'
if !esc { // Start escape sequence.
esc = true
} else { // Escaped \
if len(octalCode) > 0 {
return nil, errors.Errorf("Unescape: illegal \\ in octal code sequence detected %X", octalCode)
}
b.WriteByte(c)
esc = false
}
continue
}
// escaped = true && any other than \
if len(octalCode) > 0 {
if !strings.ContainsRune("01234567", rune(c)) {
return nil, errors.Errorf("Unescape: illegal octal sequence detected %X", octalCode)
}
octalCode = append(octalCode, c)
if len(octalCode) == 3 {
b.WriteByte(byteForOctalString(octalCode))
octalCode = nil
esc = false
}
continue
}
// Ignore \eol line breaks.
if c == 0x0A {
esc = false
continue
}
if c == 0x0D {
longEol = true
continue
}
if !strings.ContainsRune("nrtbf()01234567", rune(c)) {
return nil, errors.Errorf("Unescape: illegal escape sequence \\%c detected", c)
}
var octal bool
octal, c = escaped(c)
if octal {
octalCode = append(octalCode, c)
continue
}
b.WriteByte(c)
esc = false
}
return b.Bytes(), nil
}