From 306a8a633800f2f37d054fbf48a3e0628faf3073 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 8 Aug 1996 18:40:59 +0000 Subject: Add optional third parameter to split() and splitfields(), giving the maximum number of delimiters to parse; e.g. splitfields("a,b,c,d", ",", 2) -> ["a", "b", "c,d"]. --- Lib/string.py | 26 ++++++++++++++++++++++---- Lib/stringold.py | 26 ++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/Lib/string.py b/Lib/string.py index afa0787..1953bfc 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -55,10 +55,23 @@ def strip(s): while i < j and s[j-1] in whitespace: j = j-1 return s[i:j] +# Strip leading tabs and spaces +def lstrip(s): + i, j = 0, len(s) + while i < j and s[i] in whitespace: i = i+1 + return s[i:j] + +# Strip trailing tabs and spaces +def rstrip(s): + i, j = 0, len(s) + while i < j and s[j-1] in whitespace: j = j-1 + return s[i:j] + + # Split a string into a list of space/tab-separated words # NB: split(s) is NOT the same as splitfields(s, ' ')! -def split(s, sep=None): - if sep is not None: return splitfields(s, sep) +def split(s, sep=None, maxsplit=0): + if sep is not None: return splitfields(s, sep, maxsplit) res = [] i, n = 0, len(s) while i < n: @@ -73,18 +86,23 @@ def split(s, sep=None): # Split a list into fields separated by a given string # NB: splitfields(s, ' ') is NOT the same as split(s)! # splitfields(s, '') returns [s] (in analogy with split() in nawk) -def splitfields(s, sep=None): - if sep is None: return split(s) +def splitfields(s, sep=None, maxsplit=0): + if sep is None: return split(s, None, maxsplit) res = [] nsep = len(sep) if nsep == 0: return [s] ns = len(s) i = j = 0 + count = 0 while j+nsep <= ns: if s[j:j+nsep] == sep: + count = count + 1 res.append(s[i:j]) i = j = j + nsep + if (maxsplit and (count >= maxsplit)): + break + else: j = j + 1 res.append(s[i:]) diff --git a/Lib/stringold.py b/Lib/stringold.py index afa0787..1953bfc 100644 --- a/Lib/stringold.py +++ b/Lib/stringold.py @@ -55,10 +55,23 @@ def strip(s): while i < j and s[j-1] in whitespace: j = j-1 return s[i:j] +# Strip leading tabs and spaces +def lstrip(s): + i, j = 0, len(s) + while i < j and s[i] in whitespace: i = i+1 + return s[i:j] + +# Strip trailing tabs and spaces +def rstrip(s): + i, j = 0, len(s) + while i < j and s[j-1] in whitespace: j = j-1 + return s[i:j] + + # Split a string into a list of space/tab-separated words # NB: split(s) is NOT the same as splitfields(s, ' ')! -def split(s, sep=None): - if sep is not None: return splitfields(s, sep) +def split(s, sep=None, maxsplit=0): + if sep is not None: return splitfields(s, sep, maxsplit) res = [] i, n = 0, len(s) while i < n: @@ -73,18 +86,23 @@ def split(s, sep=None): # Split a list into fields separated by a given string # NB: splitfields(s, ' ') is NOT the same as split(s)! # splitfields(s, '') returns [s] (in analogy with split() in nawk) -def splitfields(s, sep=None): - if sep is None: return split(s) +def splitfields(s, sep=None, maxsplit=0): + if sep is None: return split(s, None, maxsplit) res = [] nsep = len(sep) if nsep == 0: return [s] ns = len(s) i = j = 0 + count = 0 while j+nsep <= ns: if s[j:j+nsep] == sep: + count = count + 1 res.append(s[i:j]) i = j = j + nsep + if (maxsplit and (count >= maxsplit)): + break + else: j = j + 1 res.append(s[i:]) -- cgit v0.12