summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-08-08 18:40:59 (GMT)
committerGuido van Rossum <guido@python.org>1996-08-08 18:40:59 (GMT)
commit306a8a633800f2f37d054fbf48a3e0628faf3073 (patch)
tree4d7650f6580397e1a6efdb218c76b4348c57a519
parenta59d3e6d507fe5dbe2583a23ab8f2cb631ebcafe (diff)
downloadcpython-306a8a633800f2f37d054fbf48a3e0628faf3073.zip
cpython-306a8a633800f2f37d054fbf48a3e0628faf3073.tar.gz
cpython-306a8a633800f2f37d054fbf48a3e0628faf3073.tar.bz2
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"].
-rw-r--r--Lib/string.py26
-rw-r--r--Lib/stringold.py26
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:])