forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.py
More file actions
103 lines (79 loc) · 2.38 KB
/
string.py
File metadata and controls
103 lines (79 loc) · 2.38 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
import pixie.vm.rt as rt
from pixie.vm.object import Object, Type
from pixie.vm.code import extend, as_var
from pixie.vm.primitives import nil, true, false
import pixie.vm.stdlib as proto
import pixie.vm.numbers as numbers
import pixie.vm.util as util
from rpython.rlib.rarithmetic import intmask, r_uint
class String(Object):
_type = Type(u"pixie.stdlib.String")
def type(self):
return String._type
def __init__(self, s):
#assert isinstance(s, unicode)
self._str = s
@extend(proto._str, String)
def _str(x):
return x
@extend(proto._repr, String)
def _repr(self):
return rt.wrap(u"\"" + self._str + u"\"")
@extend(proto._count, String)
def _count(self):
return rt.wrap(len(self._str))
@extend(proto._nth, String)
def _nth(self, idx):
i = idx.int_val()
if 0 <= i < len(self._str):
return Character(ord(self._str[i]))
raise IndexError()
@extend(proto._eq, String)
def _eq(self, v):
if not isinstance(v, String):
return false
return true if self._str == v._str else false
class Character(Object):
_type = Type(u"pixie.stdlib.Character")
_immutable_fields_ = ["_char_val"]
def type(self):
return Character._type
def __init__(self, i):
assert isinstance(i, int)
self._char_val = i
def char_val(self):
return self._char_val
@extend(proto._str, Character)
def _str(self):
assert isinstance(self, Character)
cv = self.char_val()
if cv < 128:
return rt.wrap(u"\\"+unicode(chr(cv)))
return rt.wrap(u"\\u"+unicode(str(cv)))
@extend(proto._repr, Character)
def _repr(self):
assert isinstance(self, Character)
cv = self.char_val()
if cv < 128:
return rt.wrap(u"\\"+unicode(chr(cv)))
return rt.wrap(u"\\u"+unicode(str(cv)))
@extend(proto._eq, Character)
def _eq(self, obj):
assert isinstance(self, Character)
if self is obj:
return true
if not isinstance(obj, Character):
return false
return true if self.char_val() == obj.char_val() else false
@extend(proto._hash, Character)
def _hash(self):
return rt.wrap(intmask(util.hash_int(r_uint(self.char_val()))))
@extend(proto._name, String)
def _name(self):
return self
@extend(proto._namespace, String)
def _namespace(self):
return nil
@extend(proto._hash, String)
def _hash(self):
return rt.wrap(intmask(util.hash_unencoded_chars(self._str)))