1.1 --- a/lib/__builtins__/str.py Sat Jan 28 22:37:41 2017 +0100
1.2 +++ b/lib/__builtins__/str.py Sat Jan 28 23:32:15 2017 +0100
1.3 @@ -25,6 +25,8 @@
1.4 from native import str_add, str_lt, str_gt, str_eq, str_len, str_nonempty, \
1.5 str_substr
1.6
1.7 +WHITESPACE = (" ", "\f", "\n", "\r", "\t")
1.8 +
1.9 class basestring(hashable):
1.10
1.11 "The base class for all strings."
1.12 @@ -198,15 +200,18 @@
1.13 def find(self, sub, start=None, end=None):
1.14
1.15 """
1.16 - Find 'sub' in the string, starting at 'start' (or 0, if omitted), ending
1.17 - at 'end' (or the end of the string, if omitted), returning -1 if 'sub'
1.18 - is not present.
1.19 + Find 'sub' in the string if it occurs from or after the 'start' position
1.20 + (or 0, if omitted) and before the 'end' position (or the end of the
1.21 + string, if omitted), returning the earliest occurrence or -1 if 'sub' is
1.22 + not present.
1.23 """
1.24
1.25 sublen = sub.__len__()
1.26
1.27 i = start or 0
1.28 - end = end or self.__len__()
1.29 +
1.30 + if end is None:
1.31 + end = self.__len__()
1.32
1.33 while i < end - sublen:
1.34 if sub == self[i:i+sublen]:
1.35 @@ -256,10 +261,83 @@
1.36 def lower(self): pass
1.37 def lstrip(self, chars=None): pass
1.38 def replace(self, old, new, count=None): pass
1.39 - def rfind(self, sub, start=None, end=None): pass
1.40 + def rfind(self, sub, start=None, end=None):
1.41 +
1.42 + """
1.43 + Find 'sub' in the string if it occurs from or after the 'start' position
1.44 + (or 0, if omitted) and before the 'end' position (or the end of the
1.45 + string, if omitted), returning the latest occurrence or -1 if 'sub' is
1.46 + not present.
1.47 + """
1.48 +
1.49 + sublen = sub.__len__()
1.50 +
1.51 + start = start or 0
1.52 +
1.53 + if end is None:
1.54 + end = self.__len__()
1.55 +
1.56 + i = end - sublen
1.57 +
1.58 + while i >= start:
1.59 + if sub == self[i:i+sublen]:
1.60 + return i
1.61 + i -= 1
1.62 +
1.63 + return -1
1.64 +
1.65 def rsplit(self, sep=None, maxsplit=None): pass
1.66 def rstrip(self, chars=None): pass
1.67 - def split(self, sep=None, maxsplit=None): pass
1.68 +
1.69 + def split(self, sep=None, maxsplit=None):
1.70 +
1.71 + """
1.72 + Split the string using the given 'sep' as separator (or any whitespace
1.73 + character if omitted or specified as None), splitting at most 'maxsplit'
1.74 + times (or as many times as is possible if omitted or specified as None).
1.75 + """
1.76 +
1.77 + if sep is not None and not sep:
1.78 + raise ValueError, sep
1.79 +
1.80 + end = self.__len__()
1.81 + seplen = sep and len(sep)
1.82 + splits = 0
1.83 +
1.84 + l = []
1.85 + i = last = 0
1.86 +
1.87 + while i < end and (maxsplit is None or splits < maxsplit):
1.88 +
1.89 + # Find any specified separator.
1.90 +
1.91 + if sep and self[i:i+seplen] == sep:
1.92 + l.append(self[last:i])
1.93 + i += seplen
1.94 + last = i
1.95 + splits += 1
1.96 +
1.97 + # Find any whitespace character and skip adjacent characters.
1.98 +
1.99 + elif not sep and self[i] in WHITESPACE:
1.100 + l.append(self[last:i])
1.101 + while i < end:
1.102 + i += 1
1.103 + if self[i] not in WHITESPACE:
1.104 + break
1.105 + else:
1.106 + break
1.107 + last = i
1.108 + splits += 1
1.109 +
1.110 + # Check the next character.
1.111 +
1.112 + else:
1.113 + i += 1
1.114 +
1.115 + l.append(self[last:])
1.116 + return l
1.117 +
1.118 def splitlines(self, keepends=False): pass
1.119
1.120 def startswith(self, s):
2.1 --- a/tests/string.py Sat Jan 28 22:37:41 2017 +0100
2.2 +++ b/tests/string.py Sat Jan 28 23:32:15 2017 +0100
2.3 @@ -10,6 +10,10 @@
2.4 print s.find("w", 7) # -1
2.5 print s.find("w", 0, 6) # -1
2.6 print s.index("o") # 4
2.7 +print s.rfind("o") # 7
2.8 +print s.rfind("o", 7) # 7
2.9 +print s.rfind("o", 8) # -1
2.10 +print s.rfind("o", 0, 7) # 4
2.11
2.12 try:
2.13 print s.index("p") # should raise an exception
2.14 @@ -76,3 +80,10 @@
2.15 print 0 * s6 #
2.16 print 1 * s6 # abc
2.17 print 2 * s6 # abcabc
2.18 +
2.19 +# Test splitting of strings.
2.20 +
2.21 +s7 = "Hello...\n world,\n planet,\n globe."
2.22 +print s7.split() # ["Hello...", "world,", "planet,", "globe."]
2.23 +print s7.split(maxsplit=2) # ["Hello...", "world,", "planet,\n globe."]
2.24 +print s7.split("\n") # ["Hello...", " world,", " planet,", " globe."]