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@356 | 25 | from native import str_add, str_lt, str_gt, str_eq, str_len, str_nonempty, \ |
paul@356 | 26 | str_substr |
paul@6 | 27 | |
paul@292 | 28 | class basestring(itemaccess): |
paul@289 | 29 | |
paul@289 | 30 | "The base class for all strings." |
paul@289 | 31 | |
paul@311 | 32 | _p = maxint / 32 |
paul@300 | 33 | _a = 31 |
paul@300 | 34 | |
paul@289 | 35 | def __init__(self): |
paul@6 | 36 | |
paul@289 | 37 | "Initialise the string." |
paul@6 | 38 | |
paul@289 | 39 | # Note the __data__ member. Since strings are either initialised from |
paul@289 | 40 | # literals or converted using routines defined for other types, no form |
paul@289 | 41 | # of actual initialisation is performed here. |
paul@6 | 42 | |
paul@289 | 43 | self.__data__ = None |
paul@6 | 44 | |
paul@360 | 45 | # Note the __key__ member. This is also initialised statically. Where |
paul@360 | 46 | # a string is the same as an attribute name, the __key__ member contains |
paul@360 | 47 | # attribute position and code details. |
paul@360 | 48 | |
paul@360 | 49 | self.__key__ = None |
paul@360 | 50 | |
paul@300 | 51 | def __hash__(self): |
paul@300 | 52 | |
paul@300 | 53 | "Return a value for hashing purposes." |
paul@300 | 54 | |
paul@300 | 55 | result = 0 |
paul@300 | 56 | l = self.__len__() |
paul@300 | 57 | i = 0 |
paul@300 | 58 | |
paul@300 | 59 | while i < l: |
paul@300 | 60 | result = (result * self._a + ord(self.__get_single_item__(i))) % self._p |
paul@300 | 61 | i += 1 |
paul@300 | 62 | |
paul@300 | 63 | return result |
paul@300 | 64 | |
paul@303 | 65 | def _binary_op(self, op, other): |
paul@303 | 66 | |
paul@303 | 67 | "Perform 'op' on this int and 'other' if appropriate." |
paul@303 | 68 | |
paul@303 | 69 | if isinstance(other, basestring): |
paul@303 | 70 | return op(self.__data__, other.__data__) |
paul@303 | 71 | else: |
paul@303 | 72 | return NotImplemented |
paul@303 | 73 | |
paul@6 | 74 | def __iadd__(self, other): |
paul@289 | 75 | |
paul@303 | 76 | "Return a string combining this string with 'other'." |
paul@289 | 77 | |
paul@356 | 78 | return self._binary_op(str_add, other) |
paul@6 | 79 | |
paul@6 | 80 | __add__ = __radd__ = __iadd__ |
paul@6 | 81 | |
paul@6 | 82 | def __mul__(self, other): pass |
paul@6 | 83 | def __rmul__(self, other): pass |
paul@6 | 84 | def __mod__(self, other): pass |
paul@6 | 85 | def __rmod__(self, other): pass |
paul@6 | 86 | |
paul@6 | 87 | def __lt__(self, other): |
paul@289 | 88 | |
paul@303 | 89 | "Return whether this string is less than 'other'." |
paul@289 | 90 | |
paul@356 | 91 | return self._binary_op(str_lt, other) |
paul@6 | 92 | |
paul@6 | 93 | def __gt__(self, other): |
paul@289 | 94 | |
paul@303 | 95 | "Return whether this string is greater than 'other'." |
paul@289 | 96 | |
paul@356 | 97 | return self._binary_op(str_gt, other) |
paul@6 | 98 | |
paul@6 | 99 | def __le__(self, other): |
paul@289 | 100 | |
paul@303 | 101 | "Return whether this string is less than or equal to 'other'." |
paul@289 | 102 | |
paul@6 | 103 | return _negate(self.__gt__(other)) |
paul@6 | 104 | |
paul@6 | 105 | def __ge__(self, other): |
paul@289 | 106 | |
paul@303 | 107 | "Return whether this string is greater than or equal to 'other'." |
paul@289 | 108 | |
paul@6 | 109 | return _negate(self.__lt__(other)) |
paul@6 | 110 | |
paul@6 | 111 | def __eq__(self, other): |
paul@289 | 112 | |
paul@303 | 113 | "Return whether this string is equal to 'other'." |
paul@289 | 114 | |
paul@356 | 115 | return self._binary_op(str_eq, other) |
paul@6 | 116 | |
paul@6 | 117 | def __ne__(self, other): |
paul@289 | 118 | |
paul@303 | 119 | "Return whether this string is not equal to 'other'." |
paul@289 | 120 | |
paul@6 | 121 | return _negate(self.__eq__(other)) |
paul@6 | 122 | |
paul@140 | 123 | def __len__(self): |
paul@303 | 124 | |
paul@303 | 125 | "Return the length of this string." |
paul@303 | 126 | |
paul@356 | 127 | return str_len(self.__data__) |
paul@140 | 128 | |
paul@188 | 129 | def __str__(self): |
paul@289 | 130 | |
paul@222 | 131 | "Return a string representation." |
paul@289 | 132 | |
paul@188 | 133 | return self |
paul@6 | 134 | |
paul@222 | 135 | def __repr__(self): |
paul@289 | 136 | |
paul@227 | 137 | "Return a program representation." |
paul@289 | 138 | |
paul@222 | 139 | # NOTE: To be implemented with proper quoting. |
paul@222 | 140 | b = buffer(['"', self, '"']) |
paul@222 | 141 | return str(b) |
paul@222 | 142 | |
paul@6 | 143 | def __bool__(self): |
paul@356 | 144 | return str_nonempty(self.__data__) |
paul@6 | 145 | |
paul@6 | 146 | def endswith(self, s): pass |
paul@6 | 147 | def find(self, sub, start=None, end=None): pass |
paul@6 | 148 | def index(self, sub, start=None, end=None): pass |
paul@342 | 149 | |
paul@342 | 150 | def join(self, l): |
paul@342 | 151 | |
paul@342 | 152 | "Join the elements in 'l' with this string." |
paul@342 | 153 | |
paul@342 | 154 | # Empty strings just cause the list elements to be concatenated. |
paul@342 | 155 | |
paul@342 | 156 | if not self.__bool__(): |
paul@342 | 157 | return str(buffer(l)) |
paul@342 | 158 | |
paul@342 | 159 | # Non-empty strings join the elements together in a buffer. |
paul@342 | 160 | |
paul@342 | 161 | b = buffer() |
paul@342 | 162 | first = True |
paul@342 | 163 | |
paul@342 | 164 | for s in l: |
paul@342 | 165 | if first: |
paul@342 | 166 | first = False |
paul@342 | 167 | else: |
paul@342 | 168 | b.append(self) |
paul@342 | 169 | b.append(s) |
paul@342 | 170 | |
paul@342 | 171 | return str(b) |
paul@342 | 172 | |
paul@6 | 173 | def lower(self): pass |
paul@6 | 174 | def lstrip(self, chars=None): pass |
paul@6 | 175 | def replace(self, old, new, count=None): pass |
paul@6 | 176 | def rfind(self, sub, start=None, end=None): pass |
paul@6 | 177 | def rsplit(self, sep=None, maxsplit=None): pass |
paul@6 | 178 | def rstrip(self, chars=None): pass |
paul@6 | 179 | def split(self, sep=None, maxsplit=None): pass |
paul@6 | 180 | def splitlines(self, keepends=False): pass |
paul@6 | 181 | def startswith(self, s): pass |
paul@6 | 182 | def strip(self, chars=None): pass |
paul@6 | 183 | def upper(self): pass |
paul@6 | 184 | |
paul@292 | 185 | # Special implementation methods. |
paul@292 | 186 | |
paul@292 | 187 | def __get_single_item__(self, index): |
paul@292 | 188 | |
paul@292 | 189 | "Return the item at the normalised (positive) 'index'." |
paul@292 | 190 | |
paul@292 | 191 | self._check_index(index) |
paul@356 | 192 | return str_substr(self.__data__, index, 1) |
paul@292 | 193 | |
paul@188 | 194 | class string(basestring): |
paul@6 | 195 | pass |
paul@6 | 196 | |
paul@6 | 197 | class unicode(basestring): |
paul@6 | 198 | def encode(self, encoding): pass |
paul@6 | 199 | |
paul@188 | 200 | def str(obj): |
paul@188 | 201 | |
paul@188 | 202 | "Return the string representation of 'obj'." |
paul@188 | 203 | |
paul@248 | 204 | # Class attributes of instances provide __str__. |
paul@248 | 205 | |
paul@274 | 206 | return obj.__str__() |
paul@188 | 207 | |
paul@6 | 208 | # vim: tabstop=4 expandtab shiftwidth=4 |