paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | String objects. |
paul@6 | 5 | |
paul@6 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@311 | 22 | from __builtins__.int import maxint, minint |
paul@303 | 23 | from __builtins__.operator import _negate |
paul@292 | 24 | from __builtins__.sequence import itemaccess |
paul@384 | 25 | from __builtins__.types import check_int |
paul@356 | 26 | from native import str_add, str_lt, str_gt, str_eq, str_len, str_nonempty, \ |
paul@356 | 27 | str_substr |
paul@6 | 28 | |
paul@292 | 29 | class basestring(itemaccess): |
paul@289 | 30 | |
paul@289 | 31 | "The base class for all strings." |
paul@289 | 32 | |
paul@311 | 33 | _p = maxint / 32 |
paul@300 | 34 | _a = 31 |
paul@300 | 35 | |
paul@289 | 36 | def __init__(self): |
paul@6 | 37 | |
paul@289 | 38 | "Initialise the string." |
paul@6 | 39 | |
paul@289 | 40 | # Note the __data__ member. Since strings are either initialised from |
paul@289 | 41 | # literals or converted using routines defined for other types, no form |
paul@289 | 42 | # of actual initialisation is performed here. |
paul@6 | 43 | |
paul@289 | 44 | self.__data__ = None |
paul@6 | 45 | |
paul@360 | 46 | # Note the __key__ member. This is also initialised statically. Where |
paul@360 | 47 | # a string is the same as an attribute name, the __key__ member contains |
paul@360 | 48 | # attribute position and code details. |
paul@360 | 49 | |
paul@360 | 50 | self.__key__ = None |
paul@360 | 51 | |
paul@300 | 52 | def __hash__(self): |
paul@300 | 53 | |
paul@300 | 54 | "Return a value for hashing purposes." |
paul@300 | 55 | |
paul@300 | 56 | result = 0 |
paul@300 | 57 | l = self.__len__() |
paul@300 | 58 | i = 0 |
paul@300 | 59 | |
paul@300 | 60 | while i < l: |
paul@300 | 61 | result = (result * self._a + ord(self.__get_single_item__(i))) % self._p |
paul@300 | 62 | i += 1 |
paul@300 | 63 | |
paul@300 | 64 | return result |
paul@300 | 65 | |
paul@303 | 66 | def _binary_op(self, op, other): |
paul@303 | 67 | |
paul@303 | 68 | "Perform 'op' on this int and 'other' if appropriate." |
paul@303 | 69 | |
paul@303 | 70 | if isinstance(other, basestring): |
paul@303 | 71 | return op(self.__data__, other.__data__) |
paul@303 | 72 | else: |
paul@303 | 73 | return NotImplemented |
paul@303 | 74 | |
paul@6 | 75 | def __iadd__(self, other): |
paul@289 | 76 | |
paul@303 | 77 | "Return a string combining this string with 'other'." |
paul@289 | 78 | |
paul@356 | 79 | return self._binary_op(str_add, other) |
paul@6 | 80 | |
paul@6 | 81 | __add__ = __radd__ = __iadd__ |
paul@6 | 82 | |
paul@6 | 83 | def __mul__(self, other): pass |
paul@6 | 84 | def __rmul__(self, other): pass |
paul@6 | 85 | def __mod__(self, other): pass |
paul@6 | 86 | def __rmod__(self, other): pass |
paul@6 | 87 | |
paul@6 | 88 | def __lt__(self, other): |
paul@289 | 89 | |
paul@303 | 90 | "Return whether this string is less than 'other'." |
paul@289 | 91 | |
paul@356 | 92 | return self._binary_op(str_lt, other) |
paul@6 | 93 | |
paul@6 | 94 | def __gt__(self, other): |
paul@289 | 95 | |
paul@303 | 96 | "Return whether this string is greater than 'other'." |
paul@289 | 97 | |
paul@356 | 98 | return self._binary_op(str_gt, other) |
paul@6 | 99 | |
paul@6 | 100 | def __le__(self, other): |
paul@289 | 101 | |
paul@303 | 102 | "Return whether this string is less than or equal to 'other'." |
paul@289 | 103 | |
paul@6 | 104 | return _negate(self.__gt__(other)) |
paul@6 | 105 | |
paul@6 | 106 | def __ge__(self, other): |
paul@289 | 107 | |
paul@303 | 108 | "Return whether this string is greater than or equal to 'other'." |
paul@289 | 109 | |
paul@6 | 110 | return _negate(self.__lt__(other)) |
paul@6 | 111 | |
paul@6 | 112 | def __eq__(self, other): |
paul@289 | 113 | |
paul@303 | 114 | "Return whether this string is equal to 'other'." |
paul@289 | 115 | |
paul@356 | 116 | return self._binary_op(str_eq, other) |
paul@6 | 117 | |
paul@6 | 118 | def __ne__(self, other): |
paul@289 | 119 | |
paul@303 | 120 | "Return whether this string is not equal to 'other'." |
paul@289 | 121 | |
paul@6 | 122 | return _negate(self.__eq__(other)) |
paul@6 | 123 | |
paul@140 | 124 | def __len__(self): |
paul@303 | 125 | |
paul@303 | 126 | "Return the length of this string." |
paul@303 | 127 | |
paul@356 | 128 | return str_len(self.__data__) |
paul@140 | 129 | |
paul@188 | 130 | def __str__(self): |
paul@289 | 131 | |
paul@222 | 132 | "Return a string representation." |
paul@289 | 133 | |
paul@188 | 134 | return self |
paul@6 | 135 | |
paul@222 | 136 | def __repr__(self): |
paul@289 | 137 | |
paul@227 | 138 | "Return a program representation." |
paul@289 | 139 | |
paul@222 | 140 | # NOTE: To be implemented with proper quoting. |
paul@222 | 141 | b = buffer(['"', self, '"']) |
paul@222 | 142 | return str(b) |
paul@222 | 143 | |
paul@6 | 144 | def __bool__(self): |
paul@356 | 145 | return str_nonempty(self.__data__) |
paul@6 | 146 | |
paul@6 | 147 | def endswith(self, s): pass |
paul@6 | 148 | def find(self, sub, start=None, end=None): pass |
paul@6 | 149 | def index(self, sub, start=None, end=None): pass |
paul@342 | 150 | |
paul@342 | 151 | def join(self, l): |
paul@342 | 152 | |
paul@342 | 153 | "Join the elements in 'l' with this string." |
paul@342 | 154 | |
paul@342 | 155 | # Empty strings just cause the list elements to be concatenated. |
paul@342 | 156 | |
paul@342 | 157 | if not self.__bool__(): |
paul@342 | 158 | return str(buffer(l)) |
paul@342 | 159 | |
paul@342 | 160 | # Non-empty strings join the elements together in a buffer. |
paul@342 | 161 | |
paul@342 | 162 | b = buffer() |
paul@342 | 163 | first = True |
paul@342 | 164 | |
paul@342 | 165 | for s in l: |
paul@342 | 166 | if first: |
paul@342 | 167 | first = False |
paul@342 | 168 | else: |
paul@342 | 169 | b.append(self) |
paul@342 | 170 | b.append(s) |
paul@342 | 171 | |
paul@342 | 172 | return str(b) |
paul@342 | 173 | |
paul@6 | 174 | def lower(self): pass |
paul@6 | 175 | def lstrip(self, chars=None): pass |
paul@6 | 176 | def replace(self, old, new, count=None): pass |
paul@6 | 177 | def rfind(self, sub, start=None, end=None): pass |
paul@6 | 178 | def rsplit(self, sep=None, maxsplit=None): pass |
paul@6 | 179 | def rstrip(self, chars=None): pass |
paul@6 | 180 | def split(self, sep=None, maxsplit=None): pass |
paul@6 | 181 | def splitlines(self, keepends=False): pass |
paul@6 | 182 | def startswith(self, s): pass |
paul@6 | 183 | def strip(self, chars=None): pass |
paul@6 | 184 | def upper(self): pass |
paul@6 | 185 | |
paul@292 | 186 | # Special implementation methods. |
paul@292 | 187 | |
paul@292 | 188 | def __get_single_item__(self, index): |
paul@292 | 189 | |
paul@292 | 190 | "Return the item at the normalised (positive) 'index'." |
paul@292 | 191 | |
paul@292 | 192 | self._check_index(index) |
paul@384 | 193 | return str_substr(self.__data__, index, index + 1, 1) |
paul@384 | 194 | |
paul@384 | 195 | def __get_multiple_items__(self, start, end, step): |
paul@384 | 196 | |
paul@384 | 197 | """ |
paul@384 | 198 | Return items from 'start' until (but excluding) 'end', at 'step' |
paul@384 | 199 | intervals. |
paul@384 | 200 | """ |
paul@384 | 201 | |
paul@384 | 202 | self._check_index(start) |
paul@384 | 203 | self._check_end_index(end) |
paul@384 | 204 | check_int(step) |
paul@384 | 205 | |
paul@384 | 206 | if step == 0: |
paul@384 | 207 | raise ValueError(step) |
paul@384 | 208 | |
paul@384 | 209 | if start == end: |
paul@384 | 210 | return "" |
paul@384 | 211 | |
paul@384 | 212 | return str_substr(self.__data__, start, end, step) |
paul@292 | 213 | |
paul@188 | 214 | class string(basestring): |
paul@6 | 215 | pass |
paul@6 | 216 | |
paul@6 | 217 | class unicode(basestring): |
paul@6 | 218 | def encode(self, encoding): pass |
paul@6 | 219 | |
paul@188 | 220 | def str(obj): |
paul@188 | 221 | |
paul@188 | 222 | "Return the string representation of 'obj'." |
paul@188 | 223 | |
paul@248 | 224 | # Class attributes of instances provide __str__. |
paul@248 | 225 | |
paul@274 | 226 | return obj.__str__() |
paul@188 | 227 | |
paul@6 | 228 | # vim: tabstop=4 expandtab shiftwidth=4 |