forked from chrislusf/gelo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsym.go
140 lines (109 loc) · 2.28 KB
/
sym.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
package gelo
import "bytes"
var Null Symbol = _iSymbol([]byte("")) //TODO replace with 0
func StrToSym(s string) Symbol {
return _dSymbol([]byte(s))
}
func BytesToSym(s []byte) Symbol {
return _dSymbol(s)
}
func RuneToSym(s []rune) Symbol {
return _dSymbol([]byte(string(s)))
}
func StrEqualsSym(a string, b Symbol) bool {
return bytes.Equal([]byte(a), b.Bytes())
}
//returns true if w is the Symbol ""
func IsNullString(w Word) bool {
s, ok := w.(Symbol)
if !ok {
return false
}
return len(s.Bytes()) == 0
}
func intern(s []byte) Symbol {
if len(s) == 0 {
return Null
}
return _iSymbol(s)
}
func interns(s string) Symbol {
return intern([]byte(s))
}
//noncopying for internal use when we absolutely know that it will not end
//in horror
func bytesof(s Symbol) []byte {
if s.interned() {
return []byte(s.(_iSymbol))
}
return []byte(s.(_dSymbol))
}
func stringof(s Symbol) string {
return string(bytesof(s))
}
//dynamic, ie not interned, symbols
type _dSymbol []byte
func (s _dSymbol) Bytes() []byte {
return dup([]byte(s))
}
func (s _dSymbol) String() string {
return string([]byte(s))
}
func (s _dSymbol) Runes() []rune {
return []rune(string([]byte(s)))
}
func (_dSymbol) interned() bool {
return false
}
func (s _dSymbol) Ser() Symbol {
return s
}
func (s _dSymbol) Equals(w Word) bool {
return bytes.Equal([]byte(s), w.Ser().Bytes())
}
func (s _dSymbol) Copy() Word {
sb := []byte(s)
if len(sb) == 0 {
return Null
}
return _dSymbol(dup(sb))
}
func (s _dSymbol) DeepCopy() Word {
return s.Copy()
}
func (_dSymbol) Type() Symbol {
return interns("*SYMBOL*")
}
//This type is to be replaced by an index into an intern pool
type _iSymbol []byte
func (s _iSymbol) Bytes() []byte {
return dup([]byte(s))
}
func (s _iSymbol) String() string {
return string([]byte(s))
}
func (s _iSymbol) Runes() []rune {
return []rune(string([]byte(s)))
}
func (_iSymbol) interned() bool {
return true
}
func (s _iSymbol) Ser() Symbol {
return s
}
func (s _iSymbol) Equals(w Word) bool {
return bytes.Equal([]byte(s), w.Ser().Bytes())
}
func (s _iSymbol) Copy() Word {
sb := []byte(s)
if len(s) == 0 {
return Null
}
return _dSymbol(dup(sb)) //not a typo
}
func (s _iSymbol) DeepCopy() Word {
return s.Copy()
}
func (_iSymbol) Type() Symbol {
return interns("*SYMBOL*")
}