add dummy base to atoi/atol; careful about negative start indices in find/count
This commit is contained in:
parent
55d2f3997e
commit
8c1688e132
|
@ -115,7 +115,7 @@ def rindex(s, sub, i = 0):
|
||||||
|
|
||||||
# Count non-overlapping occurrences of substring
|
# Count non-overlapping occurrences of substring
|
||||||
def count(s, sub, i = 0):
|
def count(s, sub, i = 0):
|
||||||
if i < 0: i = i + len(s)
|
if i < 0: i = max(0, i + len(s))
|
||||||
n = len(sub)
|
n = len(sub)
|
||||||
m = len(s) + 1 - n
|
m = len(s) + 1 - n
|
||||||
if n == 0: return m-i
|
if n == 0: return m-i
|
||||||
|
@ -130,7 +130,7 @@ 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):
|
||||||
if i < 0: i = i + len(s)
|
if i < 0: i = max(0, i + len(s))
|
||||||
n = len(sub)
|
n = len(sub)
|
||||||
m = len(s) + 1 - n
|
m = len(s) + 1 - n
|
||||||
while i < m:
|
while i < m:
|
||||||
|
@ -140,7 +140,7 @@ def find(s, sub, i = 0):
|
||||||
|
|
||||||
# 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):
|
||||||
if i < 0: i = i + len(s)
|
if i < 0: i = max(0, i + len(s))
|
||||||
n = len(sub)
|
n = len(sub)
|
||||||
m = len(s) + 1 - n
|
m = len(s) + 1 - n
|
||||||
r = -1
|
r = -1
|
||||||
|
@ -168,7 +168,10 @@ def atof(str):
|
||||||
raise ValueError, 'non-float argument to string.atof'
|
raise ValueError, 'non-float argument to string.atof'
|
||||||
|
|
||||||
# Convert string to integer
|
# Convert string to integer
|
||||||
def atoi(str):
|
def atoi(str, base=10):
|
||||||
|
if base != 10:
|
||||||
|
# We only get here if strop doesn't define atoi()
|
||||||
|
raise ValueError, "this string.atoi doesn't support base != 10"
|
||||||
sign = ''
|
sign = ''
|
||||||
s = str
|
s = str
|
||||||
if s and s[0] in '+-':
|
if s and s[0] in '+-':
|
||||||
|
@ -183,7 +186,10 @@ def atoi(str):
|
||||||
return eval(sign + s)
|
return eval(sign + s)
|
||||||
|
|
||||||
# Convert string to long integer
|
# Convert string to long integer
|
||||||
def atol(str):
|
def atol(str, base=10):
|
||||||
|
if base != 10:
|
||||||
|
# We only get here if strop doesn't define atol()
|
||||||
|
raise ValueError, "this string.atol doesn't support base != 10"
|
||||||
sign = ''
|
sign = ''
|
||||||
s = str
|
s = str
|
||||||
if s and s[0] in '+-':
|
if s and s[0] in '+-':
|
||||||
|
|
|
@ -115,7 +115,7 @@ def rindex(s, sub, i = 0):
|
||||||
|
|
||||||
# Count non-overlapping occurrences of substring
|
# Count non-overlapping occurrences of substring
|
||||||
def count(s, sub, i = 0):
|
def count(s, sub, i = 0):
|
||||||
if i < 0: i = i + len(s)
|
if i < 0: i = max(0, i + len(s))
|
||||||
n = len(sub)
|
n = len(sub)
|
||||||
m = len(s) + 1 - n
|
m = len(s) + 1 - n
|
||||||
if n == 0: return m-i
|
if n == 0: return m-i
|
||||||
|
@ -130,7 +130,7 @@ 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):
|
||||||
if i < 0: i = i + len(s)
|
if i < 0: i = max(0, i + len(s))
|
||||||
n = len(sub)
|
n = len(sub)
|
||||||
m = len(s) + 1 - n
|
m = len(s) + 1 - n
|
||||||
while i < m:
|
while i < m:
|
||||||
|
@ -140,7 +140,7 @@ def find(s, sub, i = 0):
|
||||||
|
|
||||||
# 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):
|
||||||
if i < 0: i = i + len(s)
|
if i < 0: i = max(0, i + len(s))
|
||||||
n = len(sub)
|
n = len(sub)
|
||||||
m = len(s) + 1 - n
|
m = len(s) + 1 - n
|
||||||
r = -1
|
r = -1
|
||||||
|
@ -168,7 +168,10 @@ def atof(str):
|
||||||
raise ValueError, 'non-float argument to string.atof'
|
raise ValueError, 'non-float argument to string.atof'
|
||||||
|
|
||||||
# Convert string to integer
|
# Convert string to integer
|
||||||
def atoi(str):
|
def atoi(str, base=10):
|
||||||
|
if base != 10:
|
||||||
|
# We only get here if strop doesn't define atoi()
|
||||||
|
raise ValueError, "this string.atoi doesn't support base != 10"
|
||||||
sign = ''
|
sign = ''
|
||||||
s = str
|
s = str
|
||||||
if s and s[0] in '+-':
|
if s and s[0] in '+-':
|
||||||
|
@ -183,7 +186,10 @@ def atoi(str):
|
||||||
return eval(sign + s)
|
return eval(sign + s)
|
||||||
|
|
||||||
# Convert string to long integer
|
# Convert string to long integer
|
||||||
def atol(str):
|
def atol(str, base=10):
|
||||||
|
if base != 10:
|
||||||
|
# We only get here if strop doesn't define atol()
|
||||||
|
raise ValueError, "this string.atol doesn't support base != 10"
|
||||||
sign = ''
|
sign = ''
|
||||||
s = str
|
s = str
|
||||||
if s and s[0] in '+-':
|
if s and s[0] in '+-':
|
||||||
|
|
Loading…
Reference in New Issue