use __contains__, index or split instead of str.find

str.find return -1 when the substring is not found, -1 evaluate
to True and is a valid index, which can lead to bugs.
Using alternatives when possible makes the code clearer and less
prone to bugs. (and __contains__ is faster in microbenchmarks)
This commit is contained in:
Benoit Boissinot 2006-07-09 01:30:30 +02:00
parent 79ae0c65b1
commit 7dd019b60b
7 changed files with 13 additions and 14 deletions

View File

@ -231,7 +231,7 @@ def revrange(ui, repo, revs):
"""Yield revision as strings from a list of revision specifications."""
seen = {}
for spec in revs:
if spec.find(revrangesep) >= 0:
if revrangesep in spec:
start, end = spec.split(revrangesep, 1)
start = revfix(repo, start, 0)
end = revfix(repo, end, repo.changelog.count() - 1)
@ -2742,7 +2742,7 @@ def tag(ui, repo, name, rev_=None, **opts):
disallowed = (revrangesep, '\r', '\n')
for c in disallowed:
if name.find(c) >= 0:
if c in name:
raise util.Abort(_("%s cannot be used in a tag name") % repr(c))
repo.hook('pretag', throw=True, node=r, tag=name,

View File

@ -34,14 +34,14 @@ class filelog(revlog):
t = self.revision(node)
if not t.startswith('\1\n'):
return t
s = t.find('\1\n', 2)
s = t.index('\1\n', 2)
return t[s+2:]
def readmeta(self, node):
t = self.revision(node)
if not t.startswith('\1\n'):
return {}
s = t.find('\1\n', 2)
s = t.index('\1\n', 2)
mt = t[2:s]
m = {}
for l in mt.splitlines():

View File

@ -61,8 +61,7 @@ def repository(ui, path=None, create=0):
if not path: path = ''
scheme = path
if scheme:
c = scheme.find(':')
scheme = c >= 0 and scheme[:c]
scheme = scheme.split(":", 1)[0]
ctor = schemes.get(scheme) or schemes['file']
if create:
try:

View File

@ -462,7 +462,7 @@ class hgweb(object):
continue
remain = f[l:]
if "/" in remain:
short = remain[:remain.find("/") + 1] # bleah
short = remain[:remain.index("/") + 1] # bleah
files[short] = (f, None)
else:
short = os.path.basename(remain)

View File

@ -85,14 +85,14 @@ class lock(object):
# see if locker is alive. if locker is on this machine but
# not alive, we can safely break lock.
locker = util.readlock(self.f)
c = locker.find(':')
if c == -1:
try:
host, pid = locker.split(":", 1)
except ValueError:
return locker
host = locker[:c]
if host != self.host:
return locker
try:
pid = int(locker[c+1:])
pid = int(pid)
except:
return locker
if util.testpid(pid):

View File

@ -76,7 +76,7 @@ class ui(object):
if root is None:
root = os.path.expanduser('~')
for name, path in self.configitems("paths"):
if path and path.find("://") == -1 and not os.path.isabs(path):
if path and "://" not in path and not os.path.isabs(path):
self.cdata.set("paths", name, os.path.join(root, path))
def setconfig(self, section, name, val):
@ -208,7 +208,7 @@ class ui(object):
def expandpath(self, loc, default=None):
"""Return repository location relative to cwd or from [paths]"""
if loc.find("://") != -1 or os.path.exists(loc):
if "://" in loc or os.path.exists(loc):
return loc
path = self.config("paths", loc)

View File

@ -620,7 +620,7 @@ else:
def parse_patch_output(output_line):
"""parses the output produced by patch and returns the file name"""
pf = output_line[14:]
if pf.startswith("'") and pf.endswith("'") and pf.find(" ") >= 0:
if pf.startswith("'") and pf.endswith("'") and " " in pf:
pf = pf[1:-1] # Remove the quotes
return pf