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