1 #!/usr/bin/env python 2 3 """ 4 String objects. 5 6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from __builtins__.operator import _negate 23 from __builtins__.sequence import itemaccess 24 import native 25 26 class basestring(itemaccess): 27 28 "The base class for all strings." 29 30 _p = 0x03ffffff 31 _a = 31 32 33 def __init__(self): 34 35 "Initialise the string." 36 37 # Note the __data__ member. Since strings are either initialised from 38 # literals or converted using routines defined for other types, no form 39 # of actual initialisation is performed here. 40 41 self.__data__ = None 42 43 def __hash__(self): 44 45 "Return a value for hashing purposes." 46 47 result = 0 48 l = self.__len__() 49 i = 0 50 51 while i < l: 52 result = (result * self._a + ord(self.__get_single_item__(i))) % self._p 53 i += 1 54 55 return result 56 57 def _binary_op(self, op, other): 58 59 "Perform 'op' on this int and 'other' if appropriate." 60 61 if isinstance(other, basestring): 62 return op(self.__data__, other.__data__) 63 else: 64 return NotImplemented 65 66 def __iadd__(self, other): 67 68 "Return a string combining this string with 'other'." 69 70 return self._binary_op(native._str_add, other) 71 72 __add__ = __radd__ = __iadd__ 73 74 def __mul__(self, other): pass 75 def __rmul__(self, other): pass 76 def __mod__(self, other): pass 77 def __rmod__(self, other): pass 78 79 def __lt__(self, other): 80 81 "Return whether this string is less than 'other'." 82 83 return self._binary_op(native._str_lt, other) 84 85 def __gt__(self, other): 86 87 "Return whether this string is greater than 'other'." 88 89 return self._binary_op(native._str_gt, other) 90 91 def __le__(self, other): 92 93 "Return whether this string is less than or equal to 'other'." 94 95 return _negate(self.__gt__(other)) 96 97 def __ge__(self, other): 98 99 "Return whether this string is greater than or equal to 'other'." 100 101 return _negate(self.__lt__(other)) 102 103 def __eq__(self, other): 104 105 "Return whether this string is equal to 'other'." 106 107 return self._binary_op(native._str_eq, other) 108 109 def __ne__(self, other): 110 111 "Return whether this string is not equal to 'other'." 112 113 return _negate(self.__eq__(other)) 114 115 def __len__(self): 116 117 "Return the length of this string." 118 119 return native._str_len(self.__data__) 120 121 def __str__(self): 122 123 "Return a string representation." 124 125 return self 126 127 def __repr__(self): 128 129 "Return a program representation." 130 131 # NOTE: To be implemented with proper quoting. 132 b = buffer(['"', self, '"']) 133 return str(b) 134 135 def __bool__(self): 136 return native._str_nonempty(self.__data__) 137 138 def endswith(self, s): pass 139 def find(self, sub, start=None, end=None): pass 140 def index(self, sub, start=None, end=None): pass 141 def join(self, l): pass 142 def lower(self): pass 143 def lstrip(self, chars=None): pass 144 def replace(self, old, new, count=None): pass 145 def rfind(self, sub, start=None, end=None): pass 146 def rsplit(self, sep=None, maxsplit=None): pass 147 def rstrip(self, chars=None): pass 148 def split(self, sep=None, maxsplit=None): pass 149 def splitlines(self, keepends=False): pass 150 def startswith(self, s): pass 151 def strip(self, chars=None): pass 152 def upper(self): pass 153 154 # Special implementation methods. 155 156 def __get_single_item__(self, index): 157 158 "Return the item at the normalised (positive) 'index'." 159 160 self._check_index(index) 161 return native._str_substr(self.__data__, index, 1) 162 163 class string(basestring): 164 pass 165 166 class unicode(basestring): 167 def encode(self, encoding): pass 168 169 def str(obj): 170 171 "Return the string representation of 'obj'." 172 173 # Class attributes of instances provide __str__. 174 175 return obj.__str__() 176 177 # vim: tabstop=4 expandtab shiftwidth=4