# HG changeset patch # User Paul Boddie # Date 1485727003 -3600 # Node ID b2b0c13d325228f54533a1aebc5f0a3fd4f3ebff # Parent c79b1be65ef255c53be0591b8b31e634ee4e9f59 Confine indexes used in slicing operations, returning empty sequences where indexes specify regions outside the bounds of the provided sequences. Handle quoting of quote characters before typically-unquoted characters. diff -r c79b1be65ef2 -r b2b0c13d3252 lib/__builtins__/sequence.py --- a/lib/__builtins__/sequence.py Sun Jan 29 18:52:35 2017 +0100 +++ b/lib/__builtins__/sequence.py Sun Jan 29 22:56:43 2017 +0100 @@ -3,7 +3,7 @@ """ Sequence operations. -Copyright (C) 2015, 2016 Paul Boddie +Copyright (C) 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -36,15 +36,21 @@ if index < 0 or index >= self.__len__(): raise IndexError(index) - def _check_end_index(self, index): + def _confine_index(self, index): """ - Check the given absolute end 'index', raising an IndexError if out of - bounds. + Return the given absolute 'index', confined by the bounds of the + sequence. """ - if index < -1 or index > self.__len__(): - raise IndexError(index) + length = self.__len__() + + if index < 0: + return 0 + elif index > length: + return length + else: + return index def __getitem__(self, index): diff -r c79b1be65ef2 -r b2b0c13d3252 lib/__builtins__/str.py --- a/lib/__builtins__/str.py Sun Jan 29 18:52:35 2017 +0100 +++ b/lib/__builtins__/str.py Sun Jan 29 22:56:43 2017 +0100 @@ -96,10 +96,20 @@ while i < end: c = self[i] - n = ord(c) + + # Handle quotes before anything else. + + if c == quote: + b.append("\\") + b.append(quote) + i += 1 + last = i + continue # Extended unquoted text. + n = ord(c) + if 32 <= n < 128: i += 1 continue @@ -110,10 +120,7 @@ # Add quoted value. - if c == quote: - b.append("\\") - b.append(quote) - elif c == "\t": + if c == "\t": b.append("\\t") elif c == "\n": b.append("\\n") @@ -530,8 +537,8 @@ intervals. """ - self._check_index(start) - self._check_end_index(end) + start = self._confine_index(start) + end = self._confine_index(end) check_int(step) if step == 0: diff -r c79b1be65ef2 -r b2b0c13d3252 lib/__builtins__/unicode.py --- a/lib/__builtins__/unicode.py Sun Jan 29 18:52:35 2017 +0100 +++ b/lib/__builtins__/unicode.py Sun Jan 29 22:56:43 2017 +0100 @@ -3,7 +3,7 @@ """ Unicode objects. -Copyright (C) 2015, 2016 Paul Boddie +Copyright (C) 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -179,8 +179,8 @@ intervals. """ - self._check_index(start) - self._check_end_index(end) + start = self._confine_index(start) + end = self._confine_index(end) check_int(step) if step == 0: diff -r c79b1be65ef2 -r b2b0c13d3252 tests/string.py --- a/tests/string.py Sun Jan 29 18:52:35 2017 +0100 +++ b/tests/string.py Sun Jan 29 22:56:43 2017 +0100 @@ -6,6 +6,10 @@ print s[5:] # world! print s[1:10:2] # el ol print s[10:1:-2] # drwol +print s[11:] # ! +print s[:-11] # H +print s[12:] # +print s[:-12] # print s.find("w") # 6 print s.find("w", 7) # -1 print s.find("w", 0, 6) # -1