1.1 --- a/lib/__builtins__/sequence.py Sun Jan 29 18:52:35 2017 +0100
1.2 +++ b/lib/__builtins__/sequence.py Sun Jan 29 22:56:43 2017 +0100
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Sequence operations.
1.6
1.7 -Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -36,15 +36,21 @@
1.13 if index < 0 or index >= self.__len__():
1.14 raise IndexError(index)
1.15
1.16 - def _check_end_index(self, index):
1.17 + def _confine_index(self, index):
1.18
1.19 """
1.20 - Check the given absolute end 'index', raising an IndexError if out of
1.21 - bounds.
1.22 + Return the given absolute 'index', confined by the bounds of the
1.23 + sequence.
1.24 """
1.25
1.26 - if index < -1 or index > self.__len__():
1.27 - raise IndexError(index)
1.28 + length = self.__len__()
1.29 +
1.30 + if index < 0:
1.31 + return 0
1.32 + elif index > length:
1.33 + return length
1.34 + else:
1.35 + return index
1.36
1.37 def __getitem__(self, index):
1.38
2.1 --- a/lib/__builtins__/str.py Sun Jan 29 18:52:35 2017 +0100
2.2 +++ b/lib/__builtins__/str.py Sun Jan 29 22:56:43 2017 +0100
2.3 @@ -96,10 +96,20 @@
2.4
2.5 while i < end:
2.6 c = self[i]
2.7 - n = ord(c)
2.8 +
2.9 + # Handle quotes before anything else.
2.10 +
2.11 + if c == quote:
2.12 + b.append("\\")
2.13 + b.append(quote)
2.14 + i += 1
2.15 + last = i
2.16 + continue
2.17
2.18 # Extended unquoted text.
2.19
2.20 + n = ord(c)
2.21 +
2.22 if 32 <= n < 128:
2.23 i += 1
2.24 continue
2.25 @@ -110,10 +120,7 @@
2.26
2.27 # Add quoted value.
2.28
2.29 - if c == quote:
2.30 - b.append("\\")
2.31 - b.append(quote)
2.32 - elif c == "\t":
2.33 + if c == "\t":
2.34 b.append("\\t")
2.35 elif c == "\n":
2.36 b.append("\\n")
2.37 @@ -530,8 +537,8 @@
2.38 intervals.
2.39 """
2.40
2.41 - self._check_index(start)
2.42 - self._check_end_index(end)
2.43 + start = self._confine_index(start)
2.44 + end = self._confine_index(end)
2.45 check_int(step)
2.46
2.47 if step == 0:
3.1 --- a/lib/__builtins__/unicode.py Sun Jan 29 18:52:35 2017 +0100
3.2 +++ b/lib/__builtins__/unicode.py Sun Jan 29 22:56:43 2017 +0100
3.3 @@ -3,7 +3,7 @@
3.4 """
3.5 Unicode objects.
3.6
3.7 -Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
3.8 +Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
3.9
3.10 This program is free software; you can redistribute it and/or modify it under
3.11 the terms of the GNU General Public License as published by the Free Software
3.12 @@ -179,8 +179,8 @@
3.13 intervals.
3.14 """
3.15
3.16 - self._check_index(start)
3.17 - self._check_end_index(end)
3.18 + start = self._confine_index(start)
3.19 + end = self._confine_index(end)
3.20 check_int(step)
3.21
3.22 if step == 0:
4.1 --- a/tests/string.py Sun Jan 29 18:52:35 2017 +0100
4.2 +++ b/tests/string.py Sun Jan 29 22:56:43 2017 +0100
4.3 @@ -6,6 +6,10 @@
4.4 print s[5:] # world!
4.5 print s[1:10:2] # el ol
4.6 print s[10:1:-2] # drwol
4.7 +print s[11:] # !
4.8 +print s[:-11] # H
4.9 +print s[12:] #
4.10 +print s[:-12] #
4.11 print s.find("w") # 6
4.12 print s.find("w", 7) # -1
4.13 print s.find("w", 0, 6) # -1