def str_split__String_String_ANY(space, w_self, w_by, w_maxsplit=-1): maxsplit = space.int_w(w_maxsplit) value = w_self._value by = w_by._value bylen = len(by) if bylen == 0: raise OperationError(space.w_ValueError, space.wrap("empty separator")) if bylen == 1 and maxsplit < 0: res = [] start = 0 # fast path: uses str.rfind(character) and str.count(character) by = by[0] # annotator hack: string -> char count = value.count(by) res = [None] * (count + 1) end = len(value) while count >= 0: assert end >= 0 prev = value.rfind(by, 0, end) start = prev + 1 assert start >= 0 res[count] = value[start:end] count -= 1 end = prev else: res = split(value, by, maxsplit) return space.newlist_str(res)
def test_split(): assert split("", 'x') == [''] assert split("a", "a", 1) == ['', ''] assert split(" ", " ", 1) == ['', ''] assert split("aa", "a", 2) == ['', '', ''] assert split('a|b|c|d', '|') == ['a', 'b', 'c', 'd'] assert split('a|b|c|d', '|', 2) == ['a', 'b', 'c|d'] assert split('a//b//c//d', '//') == ['a', 'b', 'c', 'd'] assert split('endcase test', 'test') == ['endcase ', ''] raises(ValueError, split, 'abc', '')
def _remove_const(name): return "".join(rstring.split(name, "const")) # poor man's replace