# HG changeset patch # User Paul Boddie # Date 1485649994 -3600 # Node ID 149a38af3384f883cec89745943cd77a7c45a8c5 # Parent 360560918d918a58cb1775ae83234ff7dc639900 Fixed the find and split methods, added lstrip, rstrip, strip implementations. diff -r 360560918d91 -r 149a38af3384 lib/__builtins__/str.py --- a/lib/__builtins__/str.py Sun Jan 29 00:18:52 2017 +0100 +++ b/lib/__builtins__/str.py Sun Jan 29 01:33:14 2017 +0100 @@ -208,12 +208,14 @@ sublen = sub.__len__() - i = start or 0 - if end is None: end = self.__len__() - while i < end - sublen: + end -= sublen + + i = start or 0 + + while i <= end: if sub == self[i:i+sublen]: return i i += 1 @@ -259,8 +261,27 @@ return str(b) def lower(self): pass - def lstrip(self, chars=None): pass + + def lstrip(self, chars=None): + + """ + Strip any of the given 'chars' from the start of the string, or strip + whitespace characters is 'chars' is omitted or None. + """ + + if chars is not None and not chars: + return self + + i = 0 + end = self.__len__() + + while i < end and self[i] in (chars or WHITESPACE): + i += 1 + + return self[i:] + def replace(self, old, new, count=None): pass + def rfind(self, sub, start=None, end=None): """ @@ -286,8 +307,76 @@ return -1 - def rsplit(self, sep=None, maxsplit=None): pass - def rstrip(self, chars=None): pass + def rsplit(self, sep=None, maxsplit=None): + + """ + Split the string using the given 'sep' as separator (or any whitespace + character if omitted or specified as None), splitting at most 'maxsplit' + times (or as many times as is possible if omitted or specified as None). + Where 'maxsplit' is given, the number of split points is counted from + the end of the string. + """ + + if not maxsplit: + return self.split(sep, maxsplit) + + if sep is not None and not sep: + raise ValueError, sep + + seplen = sep and len(sep) or 1 + start = seplen + splits = 0 + + l = [] + i = last = self.__len__() + + while i >= start and (maxsplit is None or splits < maxsplit): + + # Find any specified separator. + + if sep and self[i-seplen:i] == sep: + l.insert(0, self[i:last]) + i -= seplen + last = i + splits += 1 + + # Find any whitespace character and skip adjacent characters. + + elif not sep and self[i-1] in WHITESPACE: + l.insert(0, self[i:last]) + while i > start: + i -= 1 + if self[i-1] not in WHITESPACE: + break + else: + break + last = i + splits += 1 + + # Check the next character. + + else: + i -= 1 + + l.insert(0, self[:last]) + return l + + def rstrip(self, chars=None): + + """ + Strip any of the given 'chars' from the end of the string, or strip + whitespace characters is 'chars' is omitted or None. + """ + + if chars is not None and not chars: + return self + + i = self.__len__() - 1 + + while i >= 0 and self[i] in (chars or WHITESPACE): + i -= 1 + + return self[:i+1] def split(self, sep=None, maxsplit=None): @@ -295,19 +384,24 @@ Split the string using the given 'sep' as separator (or any whitespace character if omitted or specified as None), splitting at most 'maxsplit' times (or as many times as is possible if omitted or specified as None). + Where 'maxsplit' is given, the number of split points is counted from + the start of the string. """ if sep is not None and not sep: raise ValueError, sep - end = self.__len__() - seplen = sep and len(sep) + if maxsplit is not None and not maxsplit: + return [self] + + seplen = sep and len(sep) or 1 + end = self.__len__() - seplen splits = 0 l = [] i = last = 0 - while i < end and (maxsplit is None or splits < maxsplit): + while i <= end and (maxsplit is None or splits < maxsplit): # Find any specified separator. @@ -346,7 +440,15 @@ return self[:s.__len__()] == s - def strip(self, chars=None): pass + def strip(self, chars=None): + + """ + Strip any of the given 'chars' from the start and end of the string, or + strip whitespace characters is 'chars' is omitted or None. + """ + + return self.lstrip(chars).rstrip(chars) + def upper(self): pass class string(basestring): diff -r 360560918d91 -r 149a38af3384 tests/string.py --- a/tests/string.py Sun Jan 29 00:18:52 2017 +0100 +++ b/tests/string.py Sun Jan 29 01:33:14 2017 +0100 @@ -87,3 +87,17 @@ print s7.split() # ["Hello...", "world,", "planet,", "globe."] print s7.split(maxsplit=2) # ["Hello...", "world,", "planet,\n globe."] print s7.split("\n") # ["Hello...", " world,", " planet,", " globe."] + +# NOTE: To test rsplit once list.insert is implemented. + +# Test stripping of strings. + +s8 = " \nHello world\n " +print repr(s8.strip()) # "Hello world" +print repr(s8.lstrip()) # "Hello world\n " +print repr(s8.rstrip()) # " \nHello world" + +s9 = "xyzHello worldXYZ" +print repr(s9.strip("xyYZ")) # "zHello worldX" +print repr(s9.lstrip("xyYZ")) # "zHello worldXYZ" +print repr(s9.rstrip("xyYZ")) # "xyzHello worldX"