mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Add optional 4th argument to count(), matching find() etc.
Also change all occurrences of "x == None" to "x is None" (not that it matters much, these functions are all reimplemented in strop -- but count() is not).
This commit is contained in:
parent
dd65975ac7
commit
1510565cb5
2 changed files with 28 additions and 14 deletions
|
@ -121,7 +121,7 @@ def joinfields(words, sep = ' '):
|
||||||
|
|
||||||
# Find substring, raise exception if not found
|
# Find substring, raise exception if not found
|
||||||
def index(s, sub, i = 0, last=None):
|
def index(s, sub, i = 0, last=None):
|
||||||
if last == None: last = len(s)
|
if last is None: last = len(s)
|
||||||
res = find(s, sub, i, last)
|
res = find(s, sub, i, last)
|
||||||
if res < 0:
|
if res < 0:
|
||||||
raise ValueError, 'substring not found in string.index'
|
raise ValueError, 'substring not found in string.index'
|
||||||
|
@ -129,17 +129,24 @@ def index(s, sub, i = 0, last=None):
|
||||||
|
|
||||||
# Find last substring, raise exception if not found
|
# Find last substring, raise exception if not found
|
||||||
def rindex(s, sub, i = 0, last=None):
|
def rindex(s, sub, i = 0, last=None):
|
||||||
if last == None: last = len(s)
|
if last is None: last = len(s)
|
||||||
res = rfind(s, sub, i, last)
|
res = rfind(s, sub, i, last)
|
||||||
if res < 0:
|
if res < 0:
|
||||||
raise ValueError, 'substring not found in string.index'
|
raise ValueError, 'substring not found in string.index'
|
||||||
return res
|
return res
|
||||||
|
|
||||||
# Count non-overlapping occurrences of substring
|
# Count non-overlapping occurrences of substring
|
||||||
def count(s, sub, i = 0):
|
def count(s, sub, i = 0, last=None):
|
||||||
if i < 0: i = max(0, i + len(s))
|
Slen = len(s) # cache this value, for speed
|
||||||
|
if last is None:
|
||||||
|
last = Slen
|
||||||
|
elif last < 0:
|
||||||
|
last = max(0, last + Slen)
|
||||||
|
elif last > Slen:
|
||||||
|
last = Slen
|
||||||
|
if i < 0: i = max(0, i + Slen)
|
||||||
n = len(sub)
|
n = len(sub)
|
||||||
m = len(s) + 1 - n
|
m = last + 1 - n
|
||||||
if n == 0: return m-i
|
if n == 0: return m-i
|
||||||
r = 0
|
r = 0
|
||||||
while i < m:
|
while i < m:
|
||||||
|
@ -153,7 +160,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, last=None):
|
def find(s, sub, i = 0, last=None):
|
||||||
Slen = len(s) # cache this value, for speed
|
Slen = len(s) # cache this value, for speed
|
||||||
if last == None:
|
if last is None:
|
||||||
last = Slen
|
last = Slen
|
||||||
elif last < 0:
|
elif last < 0:
|
||||||
last = max(0, last + Slen)
|
last = max(0, last + Slen)
|
||||||
|
@ -170,7 +177,7 @@ def find(s, sub, i = 0, last=None):
|
||||||
# Find last substring, return -1 if not found
|
# Find last substring, return -1 if not found
|
||||||
def rfind(s, sub, i = 0, last=None):
|
def rfind(s, sub, i = 0, last=None):
|
||||||
Slen = len(s) # cache this value, for speed
|
Slen = len(s) # cache this value, for speed
|
||||||
if last == None:
|
if last is None:
|
||||||
last = Slen
|
last = Slen
|
||||||
elif last < 0:
|
elif last < 0:
|
||||||
last = max(0, last + Slen)
|
last = max(0, last + Slen)
|
||||||
|
|
|
@ -121,7 +121,7 @@ def joinfields(words, sep = ' '):
|
||||||
|
|
||||||
# Find substring, raise exception if not found
|
# Find substring, raise exception if not found
|
||||||
def index(s, sub, i = 0, last=None):
|
def index(s, sub, i = 0, last=None):
|
||||||
if last == None: last = len(s)
|
if last is None: last = len(s)
|
||||||
res = find(s, sub, i, last)
|
res = find(s, sub, i, last)
|
||||||
if res < 0:
|
if res < 0:
|
||||||
raise ValueError, 'substring not found in string.index'
|
raise ValueError, 'substring not found in string.index'
|
||||||
|
@ -129,17 +129,24 @@ def index(s, sub, i = 0, last=None):
|
||||||
|
|
||||||
# Find last substring, raise exception if not found
|
# Find last substring, raise exception if not found
|
||||||
def rindex(s, sub, i = 0, last=None):
|
def rindex(s, sub, i = 0, last=None):
|
||||||
if last == None: last = len(s)
|
if last is None: last = len(s)
|
||||||
res = rfind(s, sub, i, last)
|
res = rfind(s, sub, i, last)
|
||||||
if res < 0:
|
if res < 0:
|
||||||
raise ValueError, 'substring not found in string.index'
|
raise ValueError, 'substring not found in string.index'
|
||||||
return res
|
return res
|
||||||
|
|
||||||
# Count non-overlapping occurrences of substring
|
# Count non-overlapping occurrences of substring
|
||||||
def count(s, sub, i = 0):
|
def count(s, sub, i = 0, last=None):
|
||||||
if i < 0: i = max(0, i + len(s))
|
Slen = len(s) # cache this value, for speed
|
||||||
|
if last is None:
|
||||||
|
last = Slen
|
||||||
|
elif last < 0:
|
||||||
|
last = max(0, last + Slen)
|
||||||
|
elif last > Slen:
|
||||||
|
last = Slen
|
||||||
|
if i < 0: i = max(0, i + Slen)
|
||||||
n = len(sub)
|
n = len(sub)
|
||||||
m = len(s) + 1 - n
|
m = last + 1 - n
|
||||||
if n == 0: return m-i
|
if n == 0: return m-i
|
||||||
r = 0
|
r = 0
|
||||||
while i < m:
|
while i < m:
|
||||||
|
@ -153,7 +160,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, last=None):
|
def find(s, sub, i = 0, last=None):
|
||||||
Slen = len(s) # cache this value, for speed
|
Slen = len(s) # cache this value, for speed
|
||||||
if last == None:
|
if last is None:
|
||||||
last = Slen
|
last = Slen
|
||||||
elif last < 0:
|
elif last < 0:
|
||||||
last = max(0, last + Slen)
|
last = max(0, last + Slen)
|
||||||
|
@ -170,7 +177,7 @@ def find(s, sub, i = 0, last=None):
|
||||||
# Find last substring, return -1 if not found
|
# Find last substring, return -1 if not found
|
||||||
def rfind(s, sub, i = 0, last=None):
|
def rfind(s, sub, i = 0, last=None):
|
||||||
Slen = len(s) # cache this value, for speed
|
Slen = len(s) # cache this value, for speed
|
||||||
if last == None:
|
if last is None:
|
||||||
last = Slen
|
last = Slen
|
||||||
elif last < 0:
|
elif last < 0:
|
||||||
last = max(0, last + Slen)
|
last = max(0, last + Slen)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue