# HG changeset patch # User Paul Boddie # Date 1485732685 -3600 # Node ID c38bb702df65be751f2de0ed4e3ffae6b85d9b5b # Parent b2b0c13d325228f54533a1aebc5f0a3fd4f3ebff Changed string and Unicode slicing methods with use of the generic sequence slicing functionality, producing string/Unicode results. This is less efficient than using the substring native functions, but it reduces code complexity. diff -r b2b0c13d3252 -r c38bb702df65 lib/__builtins__/sequence.py --- a/lib/__builtins__/sequence.py Sun Jan 29 22:56:43 2017 +0100 +++ b/lib/__builtins__/sequence.py Mon Jan 30 00:31:25 2017 +0100 @@ -36,22 +36,6 @@ if index < 0 or index >= self.__len__(): raise IndexError(index) - def _confine_index(self, index): - - """ - Return the given absolute 'index', confined by the bounds of the - sequence. - """ - - length = self.__len__() - - if index < 0: - return 0 - elif index > length: - return length - else: - return index - def __getitem__(self, index): "Return the item or slice specified by 'index'." @@ -153,9 +137,18 @@ def __get_multiple_items__(self, start, end, step): - "Method to be overridden by subclasses." + """ + Return items from 'start' until (but excluding) 'end', at 'step' + intervals. + """ - return None + result = [] + + while step > 0 and start < end or step < 0 and start > end: + result.append(self.__get_single_item__(start)) + start += step + + return result def __len__(self): @@ -283,21 +276,6 @@ raise StopIteration() - def __get_multiple_items__(self, start, end, step): - - """ - Return items from 'start' until (but excluding) 'end', at 'step' - intervals. - """ - - result = [] - - while step > 0 and start < end or step < 0 and start > end: - result.append(self.__get_single_item__(start)) - start += step - - return result - def _get_absolute_index(index, length): """ diff -r b2b0c13d3252 -r c38bb702df65 lib/__builtins__/str.py --- a/lib/__builtins__/str.py Sun Jan 29 22:56:43 2017 +0100 +++ b/lib/__builtins__/str.py Mon Jan 30 00:31:25 2017 +0100 @@ -537,17 +537,16 @@ intervals. """ - start = self._confine_index(start) - end = self._confine_index(end) + if start == end: + return "" + check_int(step) if step == 0: raise ValueError(step) - if start == end: - return "" - - return str_substr(self.__data__, start, end, step) + l = get_using(basestring.__get_multiple_items__, self)(start, end, step) + return "".join(l) def str(obj): diff -r b2b0c13d3252 -r c38bb702df65 lib/__builtins__/unicode.py --- a/lib/__builtins__/unicode.py Sun Jan 29 22:56:43 2017 +0100 +++ b/lib/__builtins__/unicode.py Mon Jan 30 00:31:25 2017 +0100 @@ -179,17 +179,16 @@ intervals. """ - start = self._confine_index(start) - end = self._confine_index(end) + if start == end: + return "" + check_int(step) if step == 0: raise ValueError(step) - if start == end: - return "" - - return utf8string(unicode_substr(self.__data__, start, end, step), self.encoding) + l = get_using(basestring.__get_multiple_items__, self)(start, end, step) + return utf8string("".join(l), self.encoding) def unicode(s, encoding):