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