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