-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
224 lines (207 loc) · 5.26 KB
/
string.go
File metadata and controls
224 lines (207 loc) · 5.26 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
217
218
219
220
221
222
223
224
package vesper
import (
"strings"
)
// EmptyString an empty string
var EmptyString = String("")
// String - create a new string object
func String(s string) *Object {
return &Object{
Type: StringType,
text: s,
}
}
// IsString returns true if the object is a string
func IsString(obj *Object) bool {
return obj.Type == StringType
}
// AsStringValue - return the native string representation of the object, if possible
func AsStringValue(obj *Object) (string, error) {
if !IsString(obj) {
return "", Error(ArgumentErrorKey, StringType, obj)
}
return obj.text, nil
}
// ToString - convert the object to a string, if possible
func ToString(a *Object) (*Object, error) {
switch a.Type {
case NullType:
return a, nil
case CharacterType:
return String(string([]rune{rune(a.fval)})), nil
case StringType:
return a, nil
case BlobType:
return String(string(BlobValue(a))), nil
case SymbolType, KeywordType, TypeType:
return String(a.text), nil
case NumberType, BooleanType:
return String(a.String()), nil
case ArrayType:
var chars []rune
for _, c := range a.elements {
if !IsCharacter(c) {
return nil, Error(ArgumentErrorKey, "to-string: array element is not a <character>: ", c)
}
chars = append(chars, rune(c.fval))
}
return String(string(chars)), nil
case ListType:
var chars []rune
for a != EmptyList {
c := Car(a)
if !IsCharacter(c) {
return nil, Error(ArgumentErrorKey, "to-string: list element is not a <character>: ", c)
}
chars = append(chars, rune(c.fval))
a = a.cdr
}
return String(string(chars)), nil
default:
return nil, Error(ArgumentErrorKey, "to-string: cannot convert argument to <string>: ", a)
}
}
// StringLength - return the string length
func StringLength(s string) int {
return len([]rune(s))
}
// EncodeString - return the encoded form of a string value
func EncodeString(s string) string {
var buf []rune
buf = append(buf, '"')
for _, c := range s {
switch c {
case '"':
buf = append(buf, '\\', '"')
case '\\':
buf = append(buf, '\\', '\\')
case '\n':
buf = append(buf, '\\', 'n')
case '\t':
buf = append(buf, '\\', 't')
case '\f':
buf = append(buf, '\\', 'f')
case '\b':
buf = append(buf, '\\', 'b')
case '\r':
buf = append(buf, '\\', 'r')
default:
buf = append(buf, c)
}
}
buf = append(buf, '"')
return string(buf)
}
// Character - return a new <character> object
func Character(c rune) *Object {
return &Object{
Type: CharacterType,
fval: float64(c),
}
}
// ToCharacter - convert object to a <character> object, if possible
func ToCharacter(c *Object) (*Object, error) {
switch c.Type {
case CharacterType:
return c, nil
case StringType:
if len(c.text) == 1 {
for _, r := range c.text {
return Character(r), nil
}
}
case NumberType:
r := rune(int(c.fval))
return Character(r), nil
}
return nil, Error(ArgumentErrorKey, "Cannot convert to <character>: ", c)
}
// AsRuneValue - return the native rune representation of the character object, if possible
func AsRuneValue(c *Object) (rune, error) {
if !IsCharacter(c) {
return 0, Error(ArgumentErrorKey, "Not a <character>", c)
}
return rune(c.fval), nil
}
// StringCharacters - return a slice of <character> objects that represent the string
func StringCharacters(s *Object) []*Object {
chars := make([]*Object, 0, len([]rune(s.text)))
for _, c := range s.text {
chars = append(chars, Character(c))
}
return chars
}
// StringRef - return the <character> object at the specified string index
func StringRef(s *Object, idx int) *Object {
// utf8 requires a scan
for i, r := range s.text {
if i == idx {
return Character(r)
}
}
return Null
}
func stringToArray(s *Object) *Object {
return Array(StringCharacters(s)...)
}
func stringToList(s *Object) *Object {
return List(StringCharacters(s)...)
}
// StringSplit splits a string by the delimiters
func StringSplit(obj *Object, delims *Object) (*Object, error) {
if !IsString(obj) {
return nil, Error(ArgumentErrorKey, "split expected a <string> for argument 1, got ", obj)
}
if !IsString(delims) {
return nil, Error(ArgumentErrorKey, "split expected a <string> for argument 2, got ", delims)
}
lst := EmptyList
tail := EmptyList
for _, s := range strings.Split(obj.text, delims.text) {
if lst == EmptyList {
lst = List(String(s))
tail = lst
} else {
tail.cdr = List(String(s))
tail = tail.cdr
}
}
return lst, nil
}
// StringJoin joins a string with the given delimters
func StringJoin(seq *Object, delims *Object) (*Object, error) {
if !IsString(delims) {
return nil, Error(ArgumentErrorKey, "join expected a <string> for argument 2, got ", delims)
}
switch seq.Type {
case ListType:
result := ""
for seq != EmptyList {
o := seq.car
if o != EmptyString && o != Null {
if result != "" {
result += delims.text
}
result += o.String()
}
seq = seq.cdr
}
return String(result), nil
case ArrayType:
result := ""
elements := seq.elements
count := len(elements)
for i := 0; i < count; i++ {
o := elements[i]
if o != EmptyString && o != Null {
if result != "" {
result += delims.text
}
result += o.String()
}
}
return String(result), nil
default:
return nil, Error(ArgumentErrorKey, "join expected a <list> or <array> for argument 1, got a ", seq.Type)
}
}