Kaydet (Commit) 4cc4ab17 authored tarafından Guido van Rossum's avatar Guido van Rossum

Add third arg to split(). Add capwords() -- which uses that.

üst 8775d8b9
...@@ -50,8 +50,10 @@ def gsub(pat, repl, str): ...@@ -50,8 +50,10 @@ def gsub(pat, repl, str):
# Split string str in fields separated by delimiters matching pattern # Split string str in fields separated by delimiters matching pattern
# pat. Only non-empty matches for the pattern are considered, so e.g. # pat. Only non-empty matches for the pattern are considered, so e.g.
# split('abc', '') returns ['abc']. # split('abc', '') returns ['abc'].
# When the optional 3rd argument is true, the separators are also
# inserted to the list.
def split(str, pat): def split(str, pat, retain = 0):
prog = compile(pat) prog = compile(pat)
res = [] res = []
start = next = 0 start = next = 0
...@@ -64,11 +66,23 @@ def split(str, pat): ...@@ -64,11 +66,23 @@ def split(str, pat):
break break
else: else:
res.append(str[start:a]) res.append(str[start:a])
if retain:
res.append(str[a:b])
start = next = b start = next = b
res.append(str[start:]) res.append(str[start:])
return res return res
# Capitalize words split using a pattern
def capwords(str, pat):
import string
words = split(str, pat, 1)
for i in range(0, len(words), 2):
words[i] = string.capitalize(words[i])
return string.joinfields(words, "")
# Internal subroutines: # Internal subroutines:
# compile(pat): compile a pattern, caching already compiled patterns # compile(pat): compile a pattern, caching already compiled patterns
# expand(repl, regs, str): expand \digit escapes in replacement string # expand(repl, regs, str): expand \digit escapes in replacement string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment