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