Kaydet (Commit) 710c352d authored tarafından Guido van Rossum's avatar Guido van Rossum

* Lib/string.py: find/rfind is now the main implementation and

	index/rindex is a wrapper that raises index_error (which is now
	always ValueError)
üst 5806a4f5
...@@ -21,6 +21,12 @@ _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:] ...@@ -21,6 +21,12 @@ _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:]
_swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:] _swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:]
del i del i
# Backward compatible names for exceptions
index_error = ValueError
atoi_error = ValueError
atof_error = ValueError
atol_error = ValueError
# convert UPPER CASE letters to lower case # convert UPPER CASE letters to lower case
def lower(s): def lower(s):
res = '' res = ''
...@@ -94,28 +100,18 @@ def joinfields(words, sep): ...@@ -94,28 +100,18 @@ def joinfields(words, sep):
return res[len(sep):] return res[len(sep):]
# Find substring, raise exception if not found # Find substring, raise exception if not found
index_error = 'substring not found in string.index'
def index(s, sub, i = 0): def index(s, sub, i = 0):
if i < 0: i = i + len(s) res = find(s, sub, i)
n = len(sub) if res < 0:
m = len(s) + 1 - n raise ValueError, 'substring not found in string.index'
while i < m: return res
if sub == s[i:i+n]: return i
i = i+1
raise index_error, (s, sub, i)
# Find last substring, raise exception if not found # Find last substring, raise exception if not found
def rindex(s, sub, i = 0): def rindex(s, sub, i = 0):
if i < 0: i = i + len(s) res = rfind(s, sub, i)
n = len(sub) if res < 0:
m = len(s) + 1 - n raise ValueError, 'substring not found in string.index'
r = None return res
while i < m:
if sub == s[i:i+n]: r = i
i = i+1
if r is None:
raise index_error, (s, sub, i)
return r
# Count non-overlapping occurrences of substring # Count non-overlapping occurrences of substring
def count(s, sub, i = 0): def count(s, sub, i = 0):
...@@ -134,20 +130,26 @@ def count(s, sub, i = 0): ...@@ -134,20 +130,26 @@ def count(s, sub, i = 0):
# Find substring, return -1 if not found # Find substring, return -1 if not found
def find(s, sub, i = 0): def find(s, sub, i = 0):
try: if i < 0: i = i + len(s)
return index(s, sub, i) n = len(sub)
except index_error: m = len(s) + 1 - n
return -1 while i < m:
if sub == s[i:i+n]: return i
i = i+1
return -1
# Find last substring, return -1 if not found # Find last substring, return -1 if not found
def rfind(s, sub, i = 0): def rfind(s, sub, i = 0):
try: if i < 0: i = i + len(s)
return rindex(s, sub, i) n = len(sub)
except index_error: m = len(s) + 1 - n
return -1 r = -1
while i < m:
if sub == s[i:i+n]: r = i
i = i+1
return r
# Convert string to float # Convert string to float
atof_error = 'non-float argument to string.atof'
def atof(str): def atof(str):
import regex import regex
sign = '' sign = ''
...@@ -155,41 +157,44 @@ def atof(str): ...@@ -155,41 +157,44 @@ def atof(str):
if s and s[0] in '+-': if s and s[0] in '+-':
sign = s[0] sign = s[0]
s = s[1:] s = s[1:]
if not s: raise atof_error, str if not s:
raise ValueError, 'non-float argument to string.atof'
while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:] while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
if regex.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s) != len(s): if regex.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s) != len(s):
raise atof_error, str raise ValueError, 'non-float argument to string.atof'
try: try:
return float(eval(sign + s)) return float(eval(sign + s))
except SyntaxError: except SyntaxError:
raise atof_error, str raise ValueError, 'non-float argument to string.atof'
# Convert string to integer # Convert string to integer
atoi_error = 'non-integer argument to string.atoi'
def atoi(str): def atoi(str):
sign = '' sign = ''
s = str s = str
if s and s[0] in '+-': if s and s[0] in '+-':
sign = s[0] sign = s[0]
s = s[1:] s = s[1:]
if not s: raise atoi_error, str if not s:
raise ValueError, 'non-integer argument to string.atoi'
while s[0] == '0' and len(s) > 1: s = s[1:] while s[0] == '0' and len(s) > 1: s = s[1:]
for c in s: for c in s:
if c not in digits: raise atoi_error, str if c not in digits:
raise ValueError, 'non-integer argument to string.atoi'
return eval(sign + s) return eval(sign + s)
# Convert string to long integer # Convert string to long integer
atol_error = 'non-integer argument to string.atol'
def atol(str): def atol(str):
sign = '' sign = ''
s = str s = str
if s and s[0] in '+-': if s and s[0] in '+-':
sign = s[0] sign = s[0]
s = s[1:] s = s[1:]
if not s: raise atoi_error, str if not s:
raise ValueError, 'non-integer argument to string.atol'
while s[0] == '0' and len(s) > 1: s = s[1:] while s[0] == '0' and len(s) > 1: s = s[1:]
for c in s: for c in s:
if c not in digits: raise atoi_error, str if c not in digits:
raise ValueError, 'non-integer argument to string.atol'
return eval(sign + s + 'L') return eval(sign + s + 'L')
# Left-justify a string # Left-justify a string
...@@ -251,30 +256,3 @@ try: ...@@ -251,30 +256,3 @@ try:
letters = lowercase + uppercase letters = lowercase + uppercase
except ImportError: except ImportError:
pass # Use the original, slow versions pass # Use the original, slow versions
# If certain functions are found, redefine the corresponding exceptions
# as ValueError
try:
from strop import index
index_error = ValueError
except ImportError:
pass # Use the original, slow versions
try:
from strop import atoi
atoi_error = ValueError
except ImportError:
pass # Use the original, slow versions
try:
from strop import atof
atof_error = ValueError
except ImportError:
pass # Use the original, slow versions
try:
from strop import atol
atol_error = ValueError
except ImportError:
pass # Use the original, slow versions
...@@ -21,6 +21,12 @@ _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:] ...@@ -21,6 +21,12 @@ _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:]
_swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:] _swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:]
del i del i
# Backward compatible names for exceptions
index_error = ValueError
atoi_error = ValueError
atof_error = ValueError
atol_error = ValueError
# convert UPPER CASE letters to lower case # convert UPPER CASE letters to lower case
def lower(s): def lower(s):
res = '' res = ''
...@@ -94,28 +100,18 @@ def joinfields(words, sep): ...@@ -94,28 +100,18 @@ def joinfields(words, sep):
return res[len(sep):] return res[len(sep):]
# Find substring, raise exception if not found # Find substring, raise exception if not found
index_error = 'substring not found in string.index'
def index(s, sub, i = 0): def index(s, sub, i = 0):
if i < 0: i = i + len(s) res = find(s, sub, i)
n = len(sub) if res < 0:
m = len(s) + 1 - n raise ValueError, 'substring not found in string.index'
while i < m: return res
if sub == s[i:i+n]: return i
i = i+1
raise index_error, (s, sub, i)
# Find last substring, raise exception if not found # Find last substring, raise exception if not found
def rindex(s, sub, i = 0): def rindex(s, sub, i = 0):
if i < 0: i = i + len(s) res = rfind(s, sub, i)
n = len(sub) if res < 0:
m = len(s) + 1 - n raise ValueError, 'substring not found in string.index'
r = None return res
while i < m:
if sub == s[i:i+n]: r = i
i = i+1
if r is None:
raise index_error, (s, sub, i)
return r
# Count non-overlapping occurrences of substring # Count non-overlapping occurrences of substring
def count(s, sub, i = 0): def count(s, sub, i = 0):
...@@ -134,20 +130,26 @@ def count(s, sub, i = 0): ...@@ -134,20 +130,26 @@ def count(s, sub, i = 0):
# Find substring, return -1 if not found # Find substring, return -1 if not found
def find(s, sub, i = 0): def find(s, sub, i = 0):
try: if i < 0: i = i + len(s)
return index(s, sub, i) n = len(sub)
except index_error: m = len(s) + 1 - n
return -1 while i < m:
if sub == s[i:i+n]: return i
i = i+1
return -1
# Find last substring, return -1 if not found # Find last substring, return -1 if not found
def rfind(s, sub, i = 0): def rfind(s, sub, i = 0):
try: if i < 0: i = i + len(s)
return rindex(s, sub, i) n = len(sub)
except index_error: m = len(s) + 1 - n
return -1 r = -1
while i < m:
if sub == s[i:i+n]: r = i
i = i+1
return r
# Convert string to float # Convert string to float
atof_error = 'non-float argument to string.atof'
def atof(str): def atof(str):
import regex import regex
sign = '' sign = ''
...@@ -155,41 +157,44 @@ def atof(str): ...@@ -155,41 +157,44 @@ def atof(str):
if s and s[0] in '+-': if s and s[0] in '+-':
sign = s[0] sign = s[0]
s = s[1:] s = s[1:]
if not s: raise atof_error, str if not s:
raise ValueError, 'non-float argument to string.atof'
while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:] while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
if regex.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s) != len(s): if regex.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s) != len(s):
raise atof_error, str raise ValueError, 'non-float argument to string.atof'
try: try:
return float(eval(sign + s)) return float(eval(sign + s))
except SyntaxError: except SyntaxError:
raise atof_error, str raise ValueError, 'non-float argument to string.atof'
# Convert string to integer # Convert string to integer
atoi_error = 'non-integer argument to string.atoi'
def atoi(str): def atoi(str):
sign = '' sign = ''
s = str s = str
if s and s[0] in '+-': if s and s[0] in '+-':
sign = s[0] sign = s[0]
s = s[1:] s = s[1:]
if not s: raise atoi_error, str if not s:
raise ValueError, 'non-integer argument to string.atoi'
while s[0] == '0' and len(s) > 1: s = s[1:] while s[0] == '0' and len(s) > 1: s = s[1:]
for c in s: for c in s:
if c not in digits: raise atoi_error, str if c not in digits:
raise ValueError, 'non-integer argument to string.atoi'
return eval(sign + s) return eval(sign + s)
# Convert string to long integer # Convert string to long integer
atol_error = 'non-integer argument to string.atol'
def atol(str): def atol(str):
sign = '' sign = ''
s = str s = str
if s and s[0] in '+-': if s and s[0] in '+-':
sign = s[0] sign = s[0]
s = s[1:] s = s[1:]
if not s: raise atoi_error, str if not s:
raise ValueError, 'non-integer argument to string.atol'
while s[0] == '0' and len(s) > 1: s = s[1:] while s[0] == '0' and len(s) > 1: s = s[1:]
for c in s: for c in s:
if c not in digits: raise atoi_error, str if c not in digits:
raise ValueError, 'non-integer argument to string.atol'
return eval(sign + s + 'L') return eval(sign + s + 'L')
# Left-justify a string # Left-justify a string
...@@ -251,30 +256,3 @@ try: ...@@ -251,30 +256,3 @@ try:
letters = lowercase + uppercase letters = lowercase + uppercase
except ImportError: except ImportError:
pass # Use the original, slow versions pass # Use the original, slow versions
# If certain functions are found, redefine the corresponding exceptions
# as ValueError
try:
from strop import index
index_error = ValueError
except ImportError:
pass # Use the original, slow versions
try:
from strop import atoi
atoi_error = ValueError
except ImportError:
pass # Use the original, slow versions
try:
from strop import atof
atof_error = ValueError
except ImportError:
pass # Use the original, slow versions
try:
from strop import atol
atol_error = ValueError
except ImportError:
pass # Use the original, slow versions
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment