-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
221 lines (196 loc) · 5 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package djson
import (
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
)
type Stringer interface {
String() string
}
type Byter interface {
Bytes() []byte
}
type String interface {
TypeConverter
Comparable
Arithmacable
Copy() String
Concat([]byte)
Replace([]byte, []byte)
}
type str struct {
bytes []byte
*CallableRegister
}
var _ String = &str{}
func NewString(bs ...byte) *str {
s := &str{bytes: bs, CallableRegister: NewCallableRegister("string")}
s.RegisterCall("index", indexString)
s.RegisterCall("match", matchString)
s.RegisterCall("sub", subString)
return s
}
func (s *str) Bytes() []byte {
return s.bytes
}
func (s *str) Copy() String {
return NewString(s.bytes...)
}
func (s *str) String() string {
return string(s.bytes)
}
func (s *str) Int() (int64, error) {
return strconv.ParseInt(string(s.bytes), 10, 64)
}
func (s *str) Float() (float64, error) {
return strconv.ParseFloat(string(s.bytes), 64)
}
func (s *str) Bool() bool {
return bytes.EqualFold(s.bytes, []byte{'t', 'r', 'u', 'e'})
}
func (s *str) Add(val Value) (ret Value, err error) {
if l, ok := val.Value.(Byter); ok {
r := s.Copy()
r.Concat(l.Bytes())
ret = Value{Type: ValueString, Value: r}
return
} else if l, ok := val.Value.(Stringer); ok {
r := s.Copy()
r.Concat([]byte(l.String()))
ret = Value{Type: ValueString, Value: r}
return
}
err = fmt.Errorf("string can't + a [%s]", val.TypeName())
return
}
func (s *str) Minus(val Value) (ret Value, err error) {
if l, ok := val.Value.(Byter); ok {
r := s.Copy()
r.Replace(l.Bytes(), []byte{})
ret = Value{Type: ValueString, Value: r}
return
} else if l, ok := val.Value.(Stringer); ok {
r := s.Copy()
r.Replace([]byte(l.String()), []byte{})
ret = Value{Type: ValueString, Value: r}
return
}
err = fmt.Errorf("string can't - a [%s]", val.TypeName())
return
}
func (s *str) Multiply(val Value) (ret Value, err error) {
err = fmt.Errorf("string can't * a [%s]", val.TypeName())
return
}
func (s *str) Devide(val Value) (ret Value, err error) {
err = fmt.Errorf("string can't / a [%s]", val.TypeName())
return
}
func (s *str) And(val Value) (ret Value, err error) {
ret = Value{Type: ValueBool, Value: s.Bool() && val.Bool()}
return
}
func (s *str) Or(val Value) (ret Value, err error) {
ret = Value{Type: ValueBool, Value: s.Bool() || val.Bool()}
return
}
func (s *str) Compare(val Value) (ret int, err error) {
if val.Type != ValueString {
err = fmt.Errorf("can't compare string with [%s]", val.TypeName())
return
}
ret = bytes.Compare(s.Bytes(), val.Value.(String).Bytes())
return
}
func (s *str) Replace(search []byte, r []byte) {
s.bytes = bytes.ReplaceAll(s.bytes, search, r)
}
func (s *str) Concat(ss []byte) {
s.bytes = append(s.bytes, ss...)
}
func indexString(val Value, scanner TokenScanner, vars Context) (ret Value, err error) {
scanner.PushEnds(TokenParenthesesClose)
defer scanner.PopEnds(TokenParenthesesClose)
stmt := NewStmtExecutor(scanner, vars)
if err = stmt.Execute(); err != nil {
return
}
if stmt.Exited() {
Exit()
}
if stmt.value.Type != ValueString {
err = errors.New("string match only accept a string as the regexp")
return
}
sub := stmt.value.Value.(String).Bytes()
s := val.Value.(String).Bytes()
ret = Value{Type: ValueInt, Value: Int(int64(bytes.Index(s, sub)))}
return
}
func subString(val Value, scanner TokenScanner, vars Context) (ret Value, err error) {
scanner.PushEnds(TokenParenthesesClose)
defer scanner.PopEnds(TokenParenthesesClose)
stmt := NewStmtExecutor(scanner, vars)
if err = stmt.Execute(); err != nil {
return
}
if stmt.Exited() {
Exit()
}
if !(stmt.value.Type == ValueArray && stmt.value.Value.(Array).Total() == 2) {
err = errors.New("string sub only accept a [start, end] as the range")
return
}
s := val.Value.(String).Bytes()
arr := stmt.value.Value.(Array)
a1 := arr.Get(0)
var start int
if a1.Type == ValueInt {
v, _ := a1.Value.(Inter).Int()
start = int(v)
} else if a1.Type == ValueNull {
start = 0
} else {
err = errors.New("string sub only accept a [start, end] as the range")
return
}
a2 := arr.Get(1)
var end int
if a2.Type == ValueInt {
v, _ := a2.Value.(Inter).Int()
end = int(v) + 1
} else if a2.Type == ValueNull {
end = len(s)
} else {
err = errors.New("string sub only accept a [start, end] as the range")
return
}
if end > len(s) {
end = len(s)
}
ret = Value{Type: ValueString, Value: NewString(s[start:end]...)}
return
}
func matchString(val Value, scanner TokenScanner, vars Context) (ret Value, err error) {
scanner.PushEnds(TokenParenthesesClose)
defer scanner.PopEnds(TokenParenthesesClose)
stmt := NewStmtExecutor(scanner, vars)
if err = stmt.Execute(); err != nil {
return
}
if stmt.Exited() {
Exit()
}
if stmt.value.Type != ValueString {
err = errors.New("string match only accept a string as the regexp")
return
}
var reg *regexp.Regexp
if reg, err = regexp.Compile(string(stmt.value.Value.(String).Bytes())); err != nil {
return
}
ret = Value{Type: ValueBool, Value: reg.Match(val.Value.(String).Bytes())}
return
}