Commits

Anonymous committed 8e309be

Changed split() to be compatible with changes to string.split(): the
optional third argument gives a maximum number of delimiters to parse.
The new function splitx() is like split() but returns a list
containing the words as well as the delimiters.

Comments (0)

Files changed (1)

 # Regular expression subroutines:
 # sub(pat, repl, str): replace first occurrence of pattern in string
 # gsub(pat, repl, str): replace all occurrences of pattern in string
-# split(str, pat): split string using pattern as delimiter
+# split(str, pat, maxsplit): split string using pattern as delimiter
+# splitx(str, pat, maxsplit): split string using pattern as delimiter plus
+#			      return delimiters
 
 
 import regex
 # Split string str in fields separated by delimiters matching pattern
 # pat.  Only non-empty matches for the pattern are considered, so e.g.
 # split('abc', '') returns ['abc'].
-# When the optional 3rd argument is true, the separators are also
-# inserted to the list.
+# The optional 3rd argument sets the number of splits that are performed.
 
-def split(str, pat, retain = 0):
+def split(str, pat, maxsplit = 0):
+	return intsplit(str, pat, maxsplit, 0)
+
+# Split string str in fields separated by delimiters matching pattern
+# pat.  Only non-empty matches for the pattern are considered, so e.g.
+# split('abc', '') returns ['abc']. The delimiters are also included
+# in the list.
+# The optional 3rd argument sets the number of splits that are performed.
+
+
+def splitx(str, pat, maxsplit = 0):
+	return intsplit(str, pat, maxsplit, 1)
+	
+# Internal function used to implement split() and splitx().
+
+def intsplit(str, pat, maxsplit, retain):
 	prog = compile(pat)
 	res = []
 	start = next = 0
+	splitcount = 0
 	while prog.search(str, next) >= 0:
 		regs = prog.regs
 		a, b = regs[0]
 			if retain:
 				res.append(str[a:b])
 			start = next = b
+			splitcount = splitcount + 1
+			if (maxsplit and (splitcount >= maxsplit)):
+			    break
 	res.append(str[start:])
 	return res
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.