From e65cce5eec23812d77a54095209c923937cc3c92 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 8 Nov 1993 15:05:21 +0000 Subject: * string.py: added rindex(), rfind(); changed index() to interpret negative start indices starting from the right. * ftplib.py: debug() -> set_debuglevel(); change demo to use __init__(). * os.py: added execl, execlp, and execvp. * lambda.py: removed (now that we have built-in map, reduce, bagof, lambda) * test_b{1,2}.py, testall.out: added tests for bagof, lambda, map, reduce * commands.py: use os, not posix * test_grammar.py: make it easy to disable non-portable int overflow tests * dis.py: don't abuse range() --- Lib/stringold.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'Lib/stringold.py') diff --git a/Lib/stringold.py b/Lib/stringold.py index 764c396bb0..afe5bec4ca 100644 --- a/Lib/stringold.py +++ b/Lib/stringold.py @@ -100,6 +100,7 @@ def index(s, sub, *args): if len(args) > 1: raise TypeError, 'string.index(): too many args' i = args[0] + if i < 0: i = i + len(s) else: i = 0 n = len(sub) @@ -109,6 +110,25 @@ def index(s, sub, *args): i = i+1 raise index_error, (s, sub) + args +# Find last substring, raise exception if not found +def rindex(s, sub, *args): + if args: + if len(args) > 1: + raise TypeError, 'string.rindex(): too many args' + i = args[0] + if i < 0: i = i + len(s) + else: + i = 0 + n = len(sub) + m = len(s) + 1 - n + r = None + while i < m: + if sub == s[i:i+n]: r = i + i = i+1 + if r is None: + raise index_error, (s, sub) + args + return r + # Find substring, return -1 if not found def find(*args): try: @@ -116,6 +136,13 @@ def find(*args): except index_error: return -1 +# Find last substring, return -1 if not found +def rfind(*args): + try: + return apply(rindex, args) + except index_error: + return -1 + # Convert string to float atof_error = 'non-float argument to string.atof' def atof(str): -- cgit v1.2.1