-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
208 lines (191 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
/*
Copyright 2015 Lee Boynton
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ell
import (
"strings"
. "github.com/boynton/ell/data"
)
// AsStringValue - return the native string representation of the object, if possible
func AsStringValue(obj Value) (string, error) {
if p, ok := obj.(*String); ok {
return p.Value, nil
}
return "", NewError(ArgumentErrorKey, StringType, obj)
}
// ToString - convert the object to a string, if possible
func ToString(a Value) (*String, error) {
switch p := a.(type) {
case *Character:
return NewString(string([]rune{p.Value})), nil
case *String:
return p, nil
case *Blob:
return NewString(string(p.Value)), nil
case *Symbol:
return NewString(p.Text), nil
case *Keyword:
return NewString(p.Name()), nil
case *Type:
return NewString(p.Name()), nil
case *Number:
return NewString(p.String()), nil
case *Boolean:
return NewString(p.String()), nil
case *Vector:
var chars []rune
for _, c := range p.Elements {
if pc, ok := c.(*Character); ok {
chars = append(chars, pc.Value)
} else {
return nil, NewError(ArgumentErrorKey, "to-string: vector element is not a <character>: ", c)
}
}
return NewString(string(chars)), nil
case *List:
var chars []rune
for p != EmptyList {
c := p.Car
if pc, ok := c.(*Character); ok {
chars = append(chars, pc.Value)
} else {
return nil, NewError(ArgumentErrorKey, "to-string: list element is not a <character>: ", c)
}
p = p.Cdr
}
return NewString(string(chars)), nil
default:
return nil, NewError(ArgumentErrorKey, "to-string: cannot convert argument to <string>: ", a)
}
}
// ToCharacter - convert object to a <character> object, if possible
func ToCharacter(c Value) (*Character, error) {
switch p := c.(type) {
case *Character:
return p, nil
case *String:
if len(p.Value) == 1 {
for _, r := range p.Value {
return NewCharacter(r), nil
}
}
case *Number:
r := rune(int(p.Value))
return NewCharacter(r), nil
}
return nil, NewError(ArgumentErrorKey, "Cannot convert to <character>: ", c)
}
// StringCharacters - return a slice of <character> objects that represent the string
func StringCharacters(s *String) []Value {
var chars []Value
for _, c := range s.Value {
chars = append(chars, NewCharacter(c))
}
return chars
}
// StringRef - return the <character> object at the specified string index
func StringRef(s *String, idx int) Value {
//utf8 requires a scan
for i, r := range s.Value {
if i == idx {
return NewCharacter(r)
}
}
return Null
}
func StringToVector(s *String) *Vector {
return NewVector(StringCharacters(s)...)
}
func StringToList(s *String) *List {
return NewList(StringCharacters(s)...)
}
func StringSplit(obj Value, delims Value) (*List, error) {
str, ok := obj.(*String)
if !ok {
return nil, NewError(ArgumentErrorKey, "split expected a <string> for argument 1, got ", obj)
}
del, ok := delims.(*String)
if !ok {
return nil, NewError(ArgumentErrorKey, "split expected a <string> for argument 2, got ", delims)
}
lst := EmptyList
tail := EmptyList
for _, s := range strings.Split(str.Value, del.Value) {
if lst == EmptyList {
lst = NewList(NewString(s))
tail = lst
} else {
tail.Cdr = NewList(NewString(s))
tail = tail.Cdr
}
}
return lst, nil
}
func StringJoin(seq Value, delims Value) (*String, error) {
del, ok := delims.(*String)
if !ok {
return nil, NewError(ArgumentErrorKey, "join expected a <string> for argument 2, got ", delims)
}
switch p := seq.(type) {
case *List:
result := ""
for p != EmptyList {
o := p.Car
if o != EmptyString && o != Null {
if result != "" {
result += del.Value
}
result += o.String()
}
p = p.Cdr
}
return NewString(result), nil
case *Vector:
result := ""
elements := p.Elements
count := len(elements)
for i := 0; i < count; i++ {
o := elements[i]
if o != EmptyString && o != Null {
if result != "" {
result += del.Value
}
result += o.String()
}
}
return NewString(result), nil
default:
return nil, NewError(ArgumentErrorKey, "join expected a <list> or <vector> for argument 1, got a ", seq.Type)
}
}
// RuneValue - return native rune value of the object
func RuneValue(obj Value) rune {
if p, ok := obj.(*Character); ok {
return p.Value
}
return 0
}
// StringValue - return native string value of the object
func StringValue(obj Value) string {
switch p := obj.(type) {
case *String:
return p.Value
case *Symbol:
return p.Name()
case *Keyword:
return p.Name()
case *Type:
return p.Name()
default:
return p.String()
}
}