-
Notifications
You must be signed in to change notification settings - Fork 0
/
char.go
259 lines (232 loc) · 5.35 KB
/
char.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
255
256
257
258
259
package value
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
// ::Std::Char
//
// Represents a single UTF-8 character.
// Takes up 4 bytes.
var CharClass *Class
// Elk's Char value
type Char rune
func (Char) Class() *Class {
return CharClass
}
func (Char) DirectClass() *Class {
return CharClass
}
func (Char) SingletonClass() *Class {
return nil
}
func (c Char) Copy() Value {
return c
}
func (c Char) Inspect() string {
var buff strings.Builder
buff.WriteRune('`')
switch c {
case '\\':
buff.WriteString(`\\`)
case '\n':
buff.WriteString(`\n`)
case '\t':
buff.WriteString(`\t`)
case '`':
buff.WriteString("\\`")
case '\r':
buff.WriteString(`\r`)
case '\a':
buff.WriteString(`\a`)
case '\b':
buff.WriteString(`\b`)
case '\v':
buff.WriteString(`\v`)
case '\f':
buff.WriteString(`\f`)
default:
if unicode.IsGraphic(rune(c)) {
buff.WriteRune(rune(c))
} else if c>>8 == 0 {
fmt.Fprintf(&buff, `\x%02x`, c)
} else if c>>16 == 0 {
fmt.Fprintf(&buff, `\u%04x`, c)
} else {
fmt.Fprintf(&buff, `\U%08X`, c)
}
}
buff.WriteRune('`')
return buff.String()
}
func (Char) InstanceVariables() SymbolMap {
return nil
}
// Returns the number of bytes
// present in the character.
func (c Char) ByteCount() int {
return utf8.RuneLen(rune(c))
}
func (Char) CharCount() int {
return 1
}
func (Char) GraphemeCount() int {
return 1
}
// Cmp compares x and y and returns:
//
// -1 if x < y
// 0 if x == y
// +1 if x > y
func (x Char) Cmp(y Char) int {
if x > y {
return 1
}
if x < y {
return -1
}
return 0
}
// Concatenate another value with this character, creating a new string, and return the result.
// If the operation is illegal an error will be returned.
func (c Char) Concat(other Value) (String, *Error) {
switch o := other.(type) {
case Char:
var buff strings.Builder
buff.WriteRune(rune(c))
buff.WriteRune(rune(o))
return String(buff.String()), nil
case String:
var buff strings.Builder
buff.WriteRune(rune(c))
buff.WriteString(string(o))
return String(buff.String()), nil
default:
return "", Errorf(TypeErrorClass, "cannot concat %s with char %s", other.Inspect(), c.Inspect())
}
}
// Repeat this character n times and return a new string containing the result.
// If the operation is illegal an error will be returned.
func (c Char) Repeat(other Value) (String, *Error) {
switch o := other.(type) {
case SmallInt:
if o < 0 {
return "", Errorf(
OutOfRangeErrorClass,
"repeat count cannot be negative: %s",
o.Inspect(),
)
}
var builder strings.Builder
for i := 0; i < int(o); i++ {
builder.WriteRune(rune(c))
}
return String(builder.String()), nil
case *BigInt:
return "", Errorf(
OutOfRangeErrorClass,
"repeat count is too large %s",
o.Inspect(),
)
default:
return "", Errorf(TypeErrorClass, "cannot repeat a char using %s", other.Inspect())
}
}
// Returns 1 if i is greater than other
// Returns 0 if both are equal.
// Returns -1 if i is less than other.
// Returns nil if the comparison was impossible (NaN)
func (c Char) Compare(other Value) (Value, *Error) {
switch o := other.(type) {
case Char:
return SmallInt(c.Cmp(o)), nil
case String:
return SmallInt(String(c).Cmp(o)), nil
default:
return nil, NewCoerceError(c.Class(), other.Class())
}
}
// Check whether c is greater than other and return an error
// if something went wrong.
func (c Char) GreaterThan(other Value) (Value, *Error) {
switch o := other.(type) {
case Char:
return ToElkBool(c > o), nil
case String:
return ToElkBool(String(c).Cmp(o) == 1), nil
default:
return nil, NewCoerceError(c.Class(), other.Class())
}
}
// Check whether c is greater than or equal to other and return an error
// if something went wrong.
func (c Char) GreaterThanEqual(other Value) (Value, *Error) {
switch o := other.(type) {
case Char:
return ToElkBool(c >= o), nil
case String:
return ToElkBool(String(c).Cmp(o) >= 0), nil
default:
return nil, NewCoerceError(c.Class(), other.Class())
}
}
// Check whether c is less than other and return an error
// if something went wrong.
func (c Char) LessThan(other Value) (Value, *Error) {
switch o := other.(type) {
case Char:
return ToElkBool(c < o), nil
case String:
return ToElkBool(String(c).Cmp(o) == -1), nil
default:
return nil, NewCoerceError(c.Class(), other.Class())
}
}
// Check whether c is less than or equal to other and return an error
// if something went wrong.
func (c Char) LessThanEqual(other Value) (Value, *Error) {
switch o := other.(type) {
case Char:
return ToElkBool(c <= o), nil
case String:
return ToElkBool(String(c).Cmp(o) <= 0), nil
default:
return nil, NewCoerceError(c.Class(), other.Class())
}
}
// Check whether c is equal to other
func (c Char) LaxEqual(other Value) Value {
switch o := other.(type) {
case Char:
return ToElkBool(c == o)
case String:
ch, ok := o.ToChar()
if !ok {
return False
}
return ToElkBool(c == ch)
default:
return False
}
}
// Check whether s is equal to other
func (c Char) Equal(other Value) Value {
switch o := other.(type) {
case Char:
return ToElkBool(c == o)
default:
return False
}
}
// Check whether s is strictly equal to other
func (c Char) StrictEqual(other Value) Value {
return c.Equal(other)
}
func initChar() {
CharClass = NewClassWithOptions(
ClassWithSealed(),
ClassWithNoInstanceVariables(),
)
StdModule.AddConstantString("Char", CharClass)
}