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 _binary_op, _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 __iadd__(self, other): 58 59 "Return a new string for the operation." 60 61 return _binary_op(self, other, native._str_add) 62 63 __add__ = __radd__ = __iadd__ 64 65 def __mul__(self, other): pass 66 def __rmul__(self, other): pass 67 def __mod__(self, other): pass 68 def __rmod__(self, other): pass 69 70 def __lt__(self, other): 71 72 "Return a new boolean for the comparison." 73 74 return _binary_op(self, other, native._str_lt) 75 76 def __gt__(self, other): 77 78 "Return a new boolean for the comparison." 79 80 return _binary_op(self, other, native._str_gt) 81 82 def __le__(self, other): 83 84 "Return a new boolean for the comparison." 85 86 return _negate(self.__gt__(other)) 87 88 def __ge__(self, other): 89 90 "Return a new boolean for the comparison." 91 92 return _negate(self.__lt__(other)) 93 94 def __eq__(self, other): 95 96 "Return a new boolean for the comparison." 97 98 return _binary_op(self, other, native._str_eq) 99 100 def __ne__(self, other): 101 102 "Return a new boolean for the comparison." 103 104 return _negate(self.__eq__(other)) 105 106 def __len__(self): 107 return native._str_len(self) 108 109 def __str__(self): 110 111 "Return a string representation." 112 113 return self 114 115 def __repr__(self): 116 117 "Return a program representation." 118 119 # NOTE: To be implemented with proper quoting. 120 b = buffer(['"', self, '"']) 121 return str(b) 122 123 def __bool__(self): 124 return native._str_nonempty(self) 125 126 def endswith(self, s): pass 127 def find(self, sub, start=None, end=None): pass 128 def index(self, sub, start=None, end=None): pass 129 def join(self, l): pass 130 def lower(self): pass 131 def lstrip(self, chars=None): pass 132 def replace(self, old, new, count=None): pass 133 def rfind(self, sub, start=None, end=None): pass 134 def rsplit(self, sep=None, maxsplit=None): pass 135 def rstrip(self, chars=None): pass 136 def split(self, sep=None, maxsplit=None): pass 137 def splitlines(self, keepends=False): pass 138 def startswith(self, s): pass 139 def strip(self, chars=None): pass 140 def upper(self): pass 141 142 # Special implementation methods. 143 144 def __get_single_item__(self, index): 145 146 "Return the item at the normalised (positive) 'index'." 147 148 self._check_index(index) 149 return native._str_substr(self, index, 1) 150 151 class string(basestring): 152 pass 153 154 class unicode(basestring): 155 def encode(self, encoding): pass 156 157 def str(obj): 158 159 "Return the string representation of 'obj'." 160 161 # Class attributes of instances provide __str__. 162 163 return obj.__str__() 164 165 # vim: tabstop=4 expandtab shiftwidth=4