Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c1688e

Browse files
committed
add dummy base to atoi/atol; careful about negative start indices in find/count
1 parent 55d2f39 commit 8c1688e

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

Lib/string.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def rindex(s, sub, i = 0):
115115

116116
# Count non-overlapping occurrences of substring
117117
def count(s, sub, i = 0):
118-
if i < 0: i = i + len(s)
118+
if i < 0: i = max(0, i + len(s))
119119
n = len(sub)
120120
m = len(s) + 1 - n
121121
if n == 0: return m-i
@@ -130,7 +130,7 @@ def count(s, sub, i = 0):
130130

131131
# Find substring, return -1 if not found
132132
def find(s, sub, i = 0):
133-
if i < 0: i = i + len(s)
133+
if i < 0: i = max(0, i + len(s))
134134
n = len(sub)
135135
m = len(s) + 1 - n
136136
while i < m:
@@ -140,7 +140,7 @@ def find(s, sub, i = 0):
140140

141141
# Find last substring, return -1 if not found
142142
def rfind(s, sub, i = 0):
143-
if i < 0: i = i + len(s)
143+
if i < 0: i = max(0, i + len(s))
144144
n = len(sub)
145145
m = len(s) + 1 - n
146146
r = -1
@@ -168,7 +168,10 @@ def atof(str):
168168
raise ValueError, 'non-float argument to string.atof'
169169

170170
# Convert string to integer
171-
def atoi(str):
171+
def atoi(str, base=10):
172+
if base != 10:
173+
# We only get here if strop doesn't define atoi()
174+
raise ValueError, "this string.atoi doesn't support base != 10"
172175
sign = ''
173176
s = str
174177
if s and s[0] in '+-':
@@ -183,7 +186,10 @@ def atoi(str):
183186
return eval(sign + s)
184187

185188
# Convert string to long integer
186-
def atol(str):
189+
def atol(str, base=10):
190+
if base != 10:
191+
# We only get here if strop doesn't define atol()
192+
raise ValueError, "this string.atol doesn't support base != 10"
187193
sign = ''
188194
s = str
189195
if s and s[0] in '+-':

Lib/stringold.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def rindex(s, sub, i = 0):
115115

116116
# Count non-overlapping occurrences of substring
117117
def count(s, sub, i = 0):
118-
if i < 0: i = i + len(s)
118+
if i < 0: i = max(0, i + len(s))
119119
n = len(sub)
120120
m = len(s) + 1 - n
121121
if n == 0: return m-i
@@ -130,7 +130,7 @@ def count(s, sub, i = 0):
130130

131131
# Find substring, return -1 if not found
132132
def find(s, sub, i = 0):
133-
if i < 0: i = i + len(s)
133+
if i < 0: i = max(0, i + len(s))
134134
n = len(sub)
135135
m = len(s) + 1 - n
136136
while i < m:
@@ -140,7 +140,7 @@ def find(s, sub, i = 0):
140140

141141
# Find last substring, return -1 if not found
142142
def rfind(s, sub, i = 0):
143-
if i < 0: i = i + len(s)
143+
if i < 0: i = max(0, i + len(s))
144144
n = len(sub)
145145
m = len(s) + 1 - n
146146
r = -1
@@ -168,7 +168,10 @@ def atof(str):
168168
raise ValueError, 'non-float argument to string.atof'
169169

170170
# Convert string to integer
171-
def atoi(str):
171+
def atoi(str, base=10):
172+
if base != 10:
173+
# We only get here if strop doesn't define atoi()
174+
raise ValueError, "this string.atoi doesn't support base != 10"
172175
sign = ''
173176
s = str
174177
if s and s[0] in '+-':
@@ -183,7 +186,10 @@ def atoi(str):
183186
return eval(sign + s)
184187

185188
# Convert string to long integer
186-
def atol(str):
189+
def atol(str, base=10):
190+
if base != 10:
191+
# We only get here if strop doesn't define atol()
192+
raise ValueError, "this string.atol doesn't support base != 10"
187193
sign = ''
188194
s = str
189195
if s and s[0] in '+-':

0 commit comments

Comments
 (0)