-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
216 lines (195 loc) · 5.03 KB
/
string.go
File metadata and controls
216 lines (195 loc) · 5.03 KB
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
package gmnlisp
import (
"context"
"strconv"
"strings"
"unicode/utf8"
)
type String string
var stringClass = registerNewBuiltInClass[String]("<string>", basicArrayClass, basicVectorClass)
func (String) ClassOf() Class {
return stringClass
}
func (s String) String() string {
return string(s)
}
func (s String) GoString() string {
return strconv.Quote(string(s))
}
func (s String) Equals(n Node, m EqlMode) bool {
ns, ok := n.(String)
if !ok {
_ns, ok := n.(String)
if !ok {
return false
}
if m == STRICT {
return false
}
ns = String(string(_ns))
}
if m == EQUALP {
return strings.EqualFold(string(s), string(ns))
}
return string(s) == string(ns)
}
func (s String) firstRuneAndRestString() (Rune, String, bool) {
if len(s) <= 0 {
return Rune(utf8.RuneError), "", false
}
r, siz := utf8.DecodeRuneInString(string(s))
return Rune(r), String(s[siz:]), true
}
func (s String) EachRune(f func(Rune) error) error {
for _, r := range s {
if err := f(Rune(r)); err != nil {
return err
}
}
return nil
}
func (s String) FirstAndRest() (Node, Node, bool) {
if len(s) <= 0 {
return nil, Null, false
}
r, siz := utf8.DecodeRuneInString(string(s))
return Rune(r), String(s[siz:]), true
}
func (s String) Add(ctx context.Context, w *World, n Node) (Node, error) {
value, err := ExpectClass[String](ctx, w, n)
if err != nil {
return nil, err
}
news := make([]byte, 0, len(s)+len(value)+1)
news = append(news, s...)
news = append(news, value...)
return String(news), nil
}
func (s String) LessThan(ctx context.Context, w *World, n Node) (bool, error) {
ns, err := ExpectClass[String](ctx, w, n)
if err != nil {
return false, err
}
return string(s) < string(ns), nil
}
func funStringAppend(ctx context.Context, w *World, list []Node) (Node, error) {
var buffer strings.Builder
for _, s := range list {
str, err := ExpectClass[String](ctx, w, s)
if err != nil {
return nil, err
}
buffer.WriteString(str.String())
}
return String(buffer.String()), nil
}
func compareString(ctx context.Context, w *World, argv []Node, f func(int) bool) (Node, error) {
left, err := ExpectClass[String](ctx, w, argv[0])
if err != nil {
return nil, err
}
right, err := ExpectClass[String](ctx, w, argv[1])
if err != nil {
return nil, err
}
cmp := strings.Compare(left.String(), right.String())
if f(cmp) {
return True, nil
}
return Null, nil
}
func funStringLt(ctx context.Context, w *World, argv []Node) (Node, error) {
return compareString(ctx, w, argv, func(cmp int) bool { return cmp < 0 })
}
func funStringLe(ctx context.Context, w *World, argv []Node) (Node, error) {
return compareString(ctx, w, argv, func(cmp int) bool { return cmp <= 0 })
}
func funStringEq(ctx context.Context, w *World, argv []Node) (Node, error) {
return compareString(ctx, w, argv, func(cmp int) bool { return cmp == 0 })
}
func funStringGt(ctx context.Context, w *World, argv []Node) (Node, error) {
return compareString(ctx, w, argv, func(cmp int) bool { return cmp > 0 })
}
func funStringGe(ctx context.Context, w *World, argv []Node) (Node, error) {
return compareString(ctx, w, argv, func(cmp int) bool { return cmp >= 0 })
}
func funStringNe(ctx context.Context, w *World, argv []Node) (Node, error) {
return compareString(ctx, w, argv, func(cmp int) bool { return cmp != 0 })
}
func funStringIndex(ctx context.Context, w *World, argv []Node) (Node, error) {
_subStr, err := ExpectClass[String](ctx, w, argv[0])
if err != nil {
return nil, err
}
subStr := _subStr.String()
_str, err := ExpectClass[String](ctx, w, argv[1])
if err != nil {
return nil, err
}
str := _str.String()
start := 0
if len(argv) >= 3 {
_start, err := ExpectClass[Integer](ctx, w, argv[2])
if err != nil {
return nil, err
}
start = int(_start)
if L := utf8.RuneCountInString(str); start >= L {
return nil, ErrIndexOutOfRange
}
if start < 0 {
return nil, &DomainError{
Reason: "Expect not a negative integer",
Object: _start,
}
}
}
for i := 0; i < start && len(str) > 0; i++ {
_, siz := utf8.DecodeRuneInString(str)
str = str[siz:]
}
index := strings.Index(str, subStr)
if index < 0 {
return Null, nil
}
return Integer(start + utf8.RuneCountInString(str[:index])), nil
}
func funCreateString(ctx context.Context, w *World, list []Node) (Node, error) {
length, err := ExpectClass[Integer](ctx, w, list[0])
if err != nil {
return nil, err
}
ch := Rune(' ')
if len(list) == 2 {
ch, err = ExpectClass[Rune](ctx, w, list[1])
if err != nil {
return nil, err
}
}
if length < 0 {
condition := &DomainError{
Object: length,
ExpectedClass: integerClass,
}
return callHandler[Node](ctx, w, false, condition)
}
if length >= Integer(ExhaustThreshold) {
return nil, StorageExhausted{}
}
return String(strings.Repeat(string(ch), int(length))), nil
}
func (s String) Elt(n int) (Node, error) {
if n < 0 {
return nil, &DomainError{
Object: Integer(n),
Reason: "Not a non-negative integer",
}
}
for _, c := range s {
if n == 0 {
return Rune(c), nil
}
n--
}
return nil, ErrIndexOutOfRange
}