annotate runes.py @ 20:be0fd5c8121d

Can't use memoryview to base Runes on; different methods inside.
author David Barts <n5jrn@me.com>
date Sat, 28 Dec 2019 01:22:03 -0800
parents 0c8d787bc7e1
children 35f29952b51e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 #!/usr/bin/env python3
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
3
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 # I m p o r t s
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
5
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import array
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import codecs
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import collections
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import struct
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import sys
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
11
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 # C l a s s e s
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
13
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 class Runes(object):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 """
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 A mutable, fixed-length sequence of UTF-16 runes. The attributes
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 encoding and codec contain the name of the encoding and the codec
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 used to generate the UTF-16. The attribute buffer contains the
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 buffer (an array of 16-bit unsigned integers) used to back this
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 object; modifications to that array will be reflected in this
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 object.
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 """
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 # The most efficient 16-bit one on this platform
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 encoding = "UTF-16" + sys.byteorder[0].upper() + "E"
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 codec = codecs.lookup(encoding)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
26
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 def __init__(self, based_on=None):
20
be0fd5c8121d Can't use memoryview to base Runes on; different methods inside.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
28 if isinstance(based_on, array.array):
be0fd5c8121d Can't use memoryview to base Runes on; different methods inside.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
29 if based_on.typecode == 'H':
10
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 self.buffer = based_on
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 self.buffer = array.array('H', based_on)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 elif isinstance(based_on, str):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 # A string should always be able to encode to runes.
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 self.buffer = array.array('H', self.codec.encode(based_on, 'strict')[0])
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 elif based_on is None:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 self.buffer = array.array('H', bytes())
12
ab7d6e908034 Allow cloning.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
38 elif isinstance(based_on, Runes):
ab7d6e908034 Allow cloning.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
39 self.buffer = array.array('H', based_on.buffer)
10
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 self.buffer = array.array('H', based_on)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
42
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 def __str__(self):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 """
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 Convert this object to a string. We deliberately do not have a
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 __repr__ method, to underscore that runes are not strings.
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 """
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 # Runes might not always be able to decode to a string.
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 return self.codec.decode(self.buffer, 'replace')[0]
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
50
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 def __bytes__(self):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 return bytes(self.buffer)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
53
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 def __len__(self):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 return len(self.buffer)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
56
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 def __lt__(self, other):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 return self.buffer < other.buffer
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
59
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 def __le__(self, other):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 return self.buffer <= other.buffer
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
62
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 def __gt__(self, other):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 return self.buffer > other.buffer
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
65
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 def __ge__(self, other):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 return self.buffer >= other.buffer
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
68
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 def __eq__(self, other):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 return self.buffer == other.buffer
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
71
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 def __ne__(self, other):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 return self.buffer != other.buffer
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
74
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 def __hash__(self):
13
6acda841a690 Add support for shared substrings.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
76 raise TypeError("unhashable type")
10
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
77
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 def __bool__(self):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 return bool(self.buffer)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
80
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 def __getitem__(self, key):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 ret = self.buffer[key]
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 if isinstance(ret, array.array):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 return Runes(ret)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 return ret
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
87
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 def __setitem__(self, key, value):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 if isinstance(key, int):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 if isinstance(value, int):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 self.buffer[key] = value
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 raise TypeError("integer required")
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 elif isinstance(value, Runes):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 self.buffer[key] = value.buffer
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
96 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 raise TypeError("runes required")
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
98
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
99 def __delitem__(self, key):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 del self.buffer[key]
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
101
13
6acda841a690 Add support for shared substrings.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
102 def __del__(self):
6acda841a690 Add support for shared substrings.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
103 # Paranoid
17
0c8d787bc7e1 ... but not paranoid enough.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
104 if hasattr(self, 'buffer') and isinstance(self.buffer, memoryview):
13
6acda841a690 Add support for shared substrings.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
105 self.buffer.release()
6acda841a690 Add support for shared substrings.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
106
10
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
107 def clear(self):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
108 del self[:]
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
109
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
110 def __iter__(self):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
111 return iter(self.buffer)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
112
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
113 def __reversed__(self):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
114 return reversed(self.buffer)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
115
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
116 def append(self, value):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
117 if isinstance(value, int):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
118 self.buffer.append(value)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
119 elif isinstance(value, Runes):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
120 self.buffer.extend(value.buffer)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
121 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
122 raise TypeError("integer or runes required")
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
123
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
124 def __contains__(self, value):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
125 return value in self.buffer
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
126
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
127 def index(self, value):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
128 return self.buffer.index(value)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
129
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
130 def find(self, value):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
131 try:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
132 return self.index(value)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
133 except ValueError:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
134 return -1
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
135
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
136 class Workspace(Runes):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
137 """
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
138 A Runes object that acts a bit more string-like, in that __setitem__
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
139 also accepts a string as an argument and __getitem__ always returns
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
140 a string. We also return empty strings instead of throwing IndexError
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
141 when attempting to read out-of-range values, because that makes life
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
142 easier for us when curling quotes.
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
143 """
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
144 def __setitem__(self, key, value):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
145 if isinstance(value, str):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
146 if isinstance(key, int):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
147 Runes.__setitem__(self, key, self._ord(value))
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
148 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
149 Runes.__setitem__(self, key, Runes(value))
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
150 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
151 Runes.__setitem__(self, key, value)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
152
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
153 def __getitem__(self, key):
13
6acda841a690 Add support for shared substrings.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
154 view = memoryview(self.buffer)
10
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
155 try:
15
0be0586104b7 Plug the leak properly (I hope).
David Barts <n5jrn@me.com>
parents: 14
diff changeset
156 result = view[key]
16
61772bf1f77c Remove blank.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
157 if isinstance(result, int):
15
0be0586104b7 Plug the leak properly (I hope).
David Barts <n5jrn@me.com>
parents: 14
diff changeset
158 return chr(result)
0be0586104b7 Plug the leak properly (I hope).
David Barts <n5jrn@me.com>
parents: 14
diff changeset
159 if isinstance(result, memoryview):
0be0586104b7 Plug the leak properly (I hope).
David Barts <n5jrn@me.com>
parents: 14
diff changeset
160 ret = self.codec.decode(result, 'replace')[0]
0be0586104b7 Plug the leak properly (I hope).
David Barts <n5jrn@me.com>
parents: 14
diff changeset
161 result.release()
0be0586104b7 Plug the leak properly (I hope).
David Barts <n5jrn@me.com>
parents: 14
diff changeset
162 return ret
0be0586104b7 Plug the leak properly (I hope).
David Barts <n5jrn@me.com>
parents: 14
diff changeset
163 else:
0be0586104b7 Plug the leak properly (I hope).
David Barts <n5jrn@me.com>
parents: 14
diff changeset
164 raise AssertionError("this shouldn't happen")
10
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
165 except IndexError:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
166 return ""
13
6acda841a690 Add support for shared substrings.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
167 finally:
6acda841a690 Add support for shared substrings.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
168 view.release()
10
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
169
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
170 def append(self, value):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
171 if isinstance(value, str):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
172 Runes.append(self, Runes(value))
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
173 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
174 Runes.append(self, value)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
175
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
176 def index(self, value):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
177 if isinstance(value, str):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
178 return Runes.index(self, self._ord(value))
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
179 else:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
180 return Runes.index(self, value)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
181
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
182 def find(self, value):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
183 try:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
184 return self.index(value)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
185 except ValueError:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
186 return -1
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
187
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
188 def _ord(self, string):
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
189 length = len(string)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
190 if length != 1:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
191 raise ValueError("expected a character, but string of length {0} found".format(length))
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
192 raw = Runes(string)
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
193 if len(raw) != 1:
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
194 raise ValueError("character not in BMP")
397c178c5b98 Make it array-based.
David Barts <n5jrn@me.com>
parents:
diff changeset
195 return raw[0]