forked from Esgorhannoth/gelo
/
util.go
254 lines (232 loc) · 4.43 KB
/
util.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package gelo
import "bytes"
type reader interface {
Read([]byte) (int, error)
}
type _recordingReader struct {
buf *bytes.Buffer
src reader
}
func newRecordingReader(src reader) *_recordingReader {
if rr, ok := src.(*_recordingReader); ok {
return rr
}
return &_recordingReader{new(bytes.Buffer), src}
}
func (r *_recordingReader) Read(p []byte) (n int, err error) {
n, err = r.src.Read(p)
if err != nil {
return
}
r.buf.Write(p[:n])
return
}
func (r *_recordingReader) Bytes() []byte {
return r.buf.Bytes()
}
type buffer struct {
*bytes.Buffer
}
func newBuf(sz int) *buffer {
if sz < 64 {
return &buffer{new(bytes.Buffer)}
}
return &buffer{bytes.NewBuffer(make([]byte, 0, sz))}
}
func newBufFrom(s []byte) *buffer {
return &buffer{bytes.NewBuffer(s)}
}
func newBufFromString(s string) *buffer {
return &buffer{bytes.NewBufferString(s)}
}
func (b *buffer) WriteWord(w Word) {
if w == nil { //XXX this is just for catching errors
b.WriteString("NIL")
return
}
if l, ok := w.(*List); ok && l == nil {
b.WriteString("{}")
return
}
b.Write(w.Ser().Bytes())
}
func (b *buffer) CopyBytes() []byte {
out := make([]byte, b.Buffer.Len())
copy(out, b.Buffer.Bytes())
b.Buffer.Reset()
return out
}
func (b *buffer) CopyString() string {
return string(b.CopyBytes())
}
func (b *buffer) Symbol() Symbol {
return BytesToSym(b.Bytes())
}
func (b *buffer) CopySymbol() Symbol {
return BytesToSym(b.CopyBytes())
}
func (b *buffer) Bytes() []byte {
out := b.Buffer.Bytes()
b.Buffer.Reset()
return out
}
func (b *buffer) String() string {
if b.Len() == 0 {
return ""
}
return string(b.Bytes())
}
func dup(s []byte) []byte {
out := make([]byte, len(s))
copy(out, s)
return out
}
//used by list and dict's Ser methods
func EscapeItem(item []byte) []byte {
var cur byte
var out []byte
is_str := false
buf := newBuf(0)
buf.WriteString("\"") //stripped if is_str is false at the end
for pos := 0; pos < len(item); pos++ {
cur = item[pos]
switch cur {
case ' ', '\t', '\f', '\n':
is_str = true
case '\\', '"', '{', '}':
buf.WriteString("\\")
}
buf.WriteByte(cur)
}
if is_str || buf.Len() == 1 {
buf.WriteString("\"")
out = buf.Bytes()
} else {
//strip the initial "
out = buf.Bytes()[1:]
}
return out
}
func UnescapeItem(item []byte, pos int) ([]byte, int, bool) {
str := false
if item[pos] == '"' {
str = true
pos++
if pos >= len(item) {
return nil, 0, false
}
if item[pos] == '"' {
//Null
return []byte(""), pos + 1, true
}
}
buf := newBuf(0)
for ; pos < len(item); pos++ {
if item[pos] == '\\' {
pos++
if pos >= len(item) {
return nil, 0, false
}
buf.WriteByte(item[pos])
pos++
continue
}
if str && item[pos] == '"' {
pos++
break
}
if !str && item[pos] == ' ' {
break
}
if item[pos] == '}' {
if !str {
break
} else {
//invalid encoding
return nil, 0, false
}
}
buf.WriteByte(item[pos])
}
return buf.Bytes(), pos, true
}
func SlurpWS(s []byte, pos int) int {
if pos >= len(s) {
return pos
}
for {
switch s[pos] {
case ' ', '\n', '\f', '\t':
pos++
if pos >= len(s) {
return pos
}
default:
return pos
}
}
panic("SlurpWS in impossible state") //Issue 65
}
func Convert(item interface{}) Word {
var word Word
word, ok := NewNumberFromGo(item) //easier to check this first
if !ok {
switch t := item.(type) {
default:
programmerError(nil, "Convert given unknown type")
case nil:
word = Null
case func(*VM, *List, uint) Word:
word = Alien(t)
case Word:
word = t
case *defert:
word = t
case bool:
word = ToBool(t)
case string:
word = interns(t)
case []byte:
word = intern(t)
case []rune:
word = interns(string(t))
case []string:
if len(t) == 0 {
word = EmptyList
} else {
l := &List{interns(t[0]), nil}
word = Word(l)
if len(t) > 1 {
for _, val := range t[1:] {
l.Next = &List{interns(val), nil}
l = l.Next
}
}
}
case []Word:
word = NewListFrom(t)
case []interface{}:
if len(t) == 0 {
word = EmptyList
} else {
l := &List{Convert(t[0]), nil}
word = l
if len(t) > 1 {
for _, val := range t[1:] {
l.Next = &List{Convert(val), nil}
l = l.Next
}
}
}
case map[string]interface{}:
tmp := make(map[string]Word)
for k, v := range t {
tmp[k] = Convert(v)
}
word = &Dict{rep: tmp}
case map[string]Word:
word = NewDictFrom(t)
}
}
return word
}