1.1 --- a/lib/__builtins__/str.py Sun Jan 29 00:18:52 2017 +0100
1.2 +++ b/lib/__builtins__/str.py Sun Jan 29 01:33:14 2017 +0100
1.3 @@ -208,12 +208,14 @@
1.4
1.5 sublen = sub.__len__()
1.6
1.7 - i = start or 0
1.8 -
1.9 if end is None:
1.10 end = self.__len__()
1.11
1.12 - while i < end - sublen:
1.13 + end -= sublen
1.14 +
1.15 + i = start or 0
1.16 +
1.17 + while i <= end:
1.18 if sub == self[i:i+sublen]:
1.19 return i
1.20 i += 1
1.21 @@ -259,8 +261,27 @@
1.22 return str(b)
1.23
1.24 def lower(self): pass
1.25 - def lstrip(self, chars=None): pass
1.26 +
1.27 + def lstrip(self, chars=None):
1.28 +
1.29 + """
1.30 + Strip any of the given 'chars' from the start of the string, or strip
1.31 + whitespace characters is 'chars' is omitted or None.
1.32 + """
1.33 +
1.34 + if chars is not None and not chars:
1.35 + return self
1.36 +
1.37 + i = 0
1.38 + end = self.__len__()
1.39 +
1.40 + while i < end and self[i] in (chars or WHITESPACE):
1.41 + i += 1
1.42 +
1.43 + return self[i:]
1.44 +
1.45 def replace(self, old, new, count=None): pass
1.46 +
1.47 def rfind(self, sub, start=None, end=None):
1.48
1.49 """
1.50 @@ -286,8 +307,76 @@
1.51
1.52 return -1
1.53
1.54 - def rsplit(self, sep=None, maxsplit=None): pass
1.55 - def rstrip(self, chars=None): pass
1.56 + def rsplit(self, sep=None, maxsplit=None):
1.57 +
1.58 + """
1.59 + Split the string using the given 'sep' as separator (or any whitespace
1.60 + character if omitted or specified as None), splitting at most 'maxsplit'
1.61 + times (or as many times as is possible if omitted or specified as None).
1.62 + Where 'maxsplit' is given, the number of split points is counted from
1.63 + the end of the string.
1.64 + """
1.65 +
1.66 + if not maxsplit:
1.67 + return self.split(sep, maxsplit)
1.68 +
1.69 + if sep is not None and not sep:
1.70 + raise ValueError, sep
1.71 +
1.72 + seplen = sep and len(sep) or 1
1.73 + start = seplen
1.74 + splits = 0
1.75 +
1.76 + l = []
1.77 + i = last = self.__len__()
1.78 +
1.79 + while i >= start and (maxsplit is None or splits < maxsplit):
1.80 +
1.81 + # Find any specified separator.
1.82 +
1.83 + if sep and self[i-seplen:i] == sep:
1.84 + l.insert(0, self[i:last])
1.85 + i -= seplen
1.86 + last = i
1.87 + splits += 1
1.88 +
1.89 + # Find any whitespace character and skip adjacent characters.
1.90 +
1.91 + elif not sep and self[i-1] in WHITESPACE:
1.92 + l.insert(0, self[i:last])
1.93 + while i > start:
1.94 + i -= 1
1.95 + if self[i-1] not in WHITESPACE:
1.96 + break
1.97 + else:
1.98 + break
1.99 + last = i
1.100 + splits += 1
1.101 +
1.102 + # Check the next character.
1.103 +
1.104 + else:
1.105 + i -= 1
1.106 +
1.107 + l.insert(0, self[:last])
1.108 + return l
1.109 +
1.110 + def rstrip(self, chars=None):
1.111 +
1.112 + """
1.113 + Strip any of the given 'chars' from the end of the string, or strip
1.114 + whitespace characters is 'chars' is omitted or None.
1.115 + """
1.116 +
1.117 + if chars is not None and not chars:
1.118 + return self
1.119 +
1.120 + i = self.__len__() - 1
1.121 +
1.122 + while i >= 0 and self[i] in (chars or WHITESPACE):
1.123 + i -= 1
1.124 +
1.125 + return self[:i+1]
1.126
1.127 def split(self, sep=None, maxsplit=None):
1.128
1.129 @@ -295,19 +384,24 @@
1.130 Split the string using the given 'sep' as separator (or any whitespace
1.131 character if omitted or specified as None), splitting at most 'maxsplit'
1.132 times (or as many times as is possible if omitted or specified as None).
1.133 + Where 'maxsplit' is given, the number of split points is counted from
1.134 + the start of the string.
1.135 """
1.136
1.137 if sep is not None and not sep:
1.138 raise ValueError, sep
1.139
1.140 - end = self.__len__()
1.141 - seplen = sep and len(sep)
1.142 + if maxsplit is not None and not maxsplit:
1.143 + return [self]
1.144 +
1.145 + seplen = sep and len(sep) or 1
1.146 + end = self.__len__() - seplen
1.147 splits = 0
1.148
1.149 l = []
1.150 i = last = 0
1.151
1.152 - while i < end and (maxsplit is None or splits < maxsplit):
1.153 + while i <= end and (maxsplit is None or splits < maxsplit):
1.154
1.155 # Find any specified separator.
1.156
1.157 @@ -346,7 +440,15 @@
1.158
1.159 return self[:s.__len__()] == s
1.160
1.161 - def strip(self, chars=None): pass
1.162 + def strip(self, chars=None):
1.163 +
1.164 + """
1.165 + Strip any of the given 'chars' from the start and end of the string, or
1.166 + strip whitespace characters is 'chars' is omitted or None.
1.167 + """
1.168 +
1.169 + return self.lstrip(chars).rstrip(chars)
1.170 +
1.171 def upper(self): pass
1.172
1.173 class string(basestring):