Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e18ec7

Browse files
committed
Correctly implement sub, subn, and split. Also correct and augment
the cache code.
1 parent a4f1a78 commit 9e18ec7

1 file changed

Lines changed: 88 additions & 10 deletions

File tree

Lib/re.py

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def valid_identifier(id):
4040
_cache = {}
4141
_MAXCACHE = 20
4242

43-
def _cachecompile(pattern, flags):
43+
def _cachecompile(pattern, flags=0):
4444
key = (pattern, flags)
4545
try:
4646
return _cache[key]
@@ -59,13 +59,19 @@ def search(pattern, string, flags=0):
5959
return _cachecompile(pattern, flags).search(string)
6060

6161
def sub(pattern, repl, string, count=0):
62-
return _cachecompile(pattern).sub(repl, string, count)
62+
if type(pattern) == type(''):
63+
pattern = _cachecompile(pattern)
64+
return pattern.sub(repl, string, count)
6365

6466
def subn(pattern, repl, string, count=0):
65-
return _cachecompile(pattern).subn(repl, string, count)
67+
if type(pattern) == type(''):
68+
pattern = _cachecompile(pattern)
69+
return pattern.subn(repl, string, count)
6670

6771
def split(pattern, string, maxsplit=0):
68-
return _cachecompile(pattern).subn(string, maxsplit)
72+
if type(pattern) == type(''):
73+
pattern = _cachecompile(pattern)
74+
return pattern.split(string, maxsplit)
6975

7076
#
7177
#
@@ -126,14 +132,86 @@ def match(self, string, pos=0):
126132
regs)
127133

128134
def sub(self, repl, string, count=0):
129-
pass
130-
131-
def subn(self, repl, string, count=0):
132-
pass
135+
return self.subn(repl, string, count)[0]
133136

134-
def split(self, string, maxsplit=0):
135-
pass
137+
def subn(self, repl, source, count=0):
138+
if count < 0: raise error, "negative substibution count"
139+
if count == 0: import sys; count = sys.maxint
140+
if type(repl) == type(''):
141+
if '\\' in repl:
142+
repl = lambda m, r=repl: _expand(m, r)
143+
else:
144+
repl = lambda m, r=repl: r
145+
n = 0 # Number of matches
146+
pos = 0 # Where to start searching
147+
lastmatch = -1 # End of last match
148+
results = [] # Substrings making up the result
149+
end = len(source)
150+
while n < count and pos <= end:
151+
m = self.search(source, pos)
152+
if not m: break
153+
i, j = m.span(0)
154+
if i == j == lastmatch:
155+
# Empty match adjacent to previous match
156+
pos = pos+1
157+
results.append(source[lastmatch:pos])
158+
continue
159+
if pos < i: results.append(source[pos:i])
160+
results.append(repl(m))
161+
pos = lastmatch = j
162+
if i == j:
163+
# Last match was empty; don't try here again
164+
pos = pos+1
165+
results.append(source[lastmatch:pos])
166+
n = n+1
167+
results.append(source[pos:])
168+
return (string.join(results, ''), n)
136169

170+
def split(self, source, maxsplit=0):
171+
if maxsplit < 0: raise error, "negative split count"
172+
if maxsplit == 0: import sys; maxsplit = sys.maxint
173+
n = 0
174+
pos = 0
175+
lastmatch = 0
176+
results = []
177+
end = len(source)
178+
while n < maxsplit:
179+
m = self.search(source, pos)
180+
if not m: break
181+
i, j = m.span(0)
182+
if i == j:
183+
# Empty match
184+
if pos >= end: break
185+
pos = pos+1
186+
continue
187+
results.append(source[lastmatch:i])
188+
g = m.group()
189+
if g:
190+
results[len(results):] = list(g)
191+
pos = lastmatch = j
192+
results.append(source[lastmatch:])
193+
return results
194+
195+
def _expand(m, repl):
196+
results = []
197+
index = 0
198+
size = len(repl)
199+
while index < size:
200+
found = string.find(repl, '\\', index)
201+
if found < 0:
202+
results.append(repl[index:])
203+
break
204+
if found > index:
205+
results.append(repl[index:found])
206+
escape_type, value, index = expand_escape(repl, found+1, REPLACEMENT)
207+
if escape_type == CHAR:
208+
results.append(value)
209+
elif escape_type == MEMORY_REFERENCE:
210+
results.append(m.group(value))
211+
else:
212+
raise error, "bad escape in replacement"
213+
return string.join(results, '')
214+
137215
class MatchObject:
138216
def __init__(self, re, string, pos, regs):
139217
self.re = re

0 commit comments

Comments
 (0)