summaryrefslogtreecommitdiffstats
path: root/Lib/sre.py
diff options
context:
space:
mode:
authorFredrik Lundh <fredrik@pythonware.com>2000-07-23 21:46:17 (GMT)
committerFredrik Lundh <fredrik@pythonware.com>2000-07-23 21:46:17 (GMT)
commit8a3ebf8ca87a7d2989148d7c218974ab4235ca5d (patch)
tree1694bf24acdb9defbf770ad34e1181b84bf17428 /Lib/sre.py
parent4f1b2081e9dde910048ef679f5afe252023a4031 (diff)
downloadcpython-8a3ebf8ca87a7d2989148d7c218974ab4235ca5d.zip
cpython-8a3ebf8ca87a7d2989148d7c218974ab4235ca5d.tar.gz
cpython-8a3ebf8ca87a7d2989148d7c218974ab4235ca5d.tar.bz2
-- SRE 0.9.6 sync. this includes:
+ added "regs" attribute + fixed "pos" and "endpos" attributes + reset "lastindex" and "lastgroup" in scanner methods + removed (?P#id) syntax; the "lastindex" and "lastgroup" attributes are now always set + removed string module dependencies in sre_parse + better debugging support in sre_parse + various tweaks to build under 1.5.2
Diffstat (limited to 'Lib/sre.py')
-rw-r--r--Lib/sre.py33
1 files changed, 24 insertions, 9 deletions
diff --git a/Lib/sre.py b/Lib/sre.py
index fbf44a5..6dd1df9 100644
--- a/Lib/sre.py
+++ b/Lib/sre.py
@@ -10,9 +10,13 @@
# other compatibility work.
#
+# FIXME: change all FIXME's to XXX ;-)
+
import sre_compile
import sre_parse
+import string
+
# flags
I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE
L = LOCALE = sre_compile.SRE_FLAG_LOCALE
@@ -53,6 +57,9 @@ def findall(pattern, string, maxsplit=0):
def compile(pattern, flags=0):
return _compile(pattern, flags)
+def purge():
+ _cache.clear()
+
def template(pattern, flags=0):
return _compile(pattern, flags|T)
@@ -65,7 +72,7 @@ def escape(pattern):
s[i] = "\\000"
else:
s[i] = "\\" + c
- return pattern[:0].join(s)
+ return _join(s, pattern)
# --------------------------------------------------------------------
# internals
@@ -73,10 +80,14 @@ def escape(pattern):
_cache = {}
_MAXCACHE = 100
+def _join(seq, sep):
+ # internal: join into string having the same type as sep
+ return string.join(seq, sep[:0])
+
def _compile(pattern, flags=0):
# internal: compile pattern
tp = type(pattern)
- if tp not in (type(""), type(u"")):
+ if tp not in sre_compile.STRING_TYPES:
return pattern
key = (tp, pattern, flags)
try:
@@ -89,10 +100,6 @@ def _compile(pattern, flags=0):
_cache[key] = p
return p
-def purge():
- # clear pattern cache
- _cache.clear()
-
def _sub(pattern, template, string, count=0):
# internal: pattern.sub implementation hook
return _subn(pattern, template, string, count)[0]
@@ -120,7 +127,7 @@ def _subn(pattern, template, string, count=0):
i = e
n = n + 1
append(string[i:])
- return string[:0].join(s), n
+ return _join(s, string[:0]), n
def _split(pattern, string, maxsplit=0):
# internal: pattern.split implementation hook
@@ -161,11 +168,19 @@ copy_reg.pickle(type(_compile("")), _pickle, _compile)
class Scanner:
def __init__(self, lexicon):
+ from sre_constants import BRANCH, SUBPATTERN, INDEX
self.lexicon = lexicon
+ # combine phrases into a compound pattern
p = []
+ s = sre_parse.Pattern()
for phrase, action in lexicon:
- p.append("(?:%s)(?P#%d)" % (phrase, len(p)))
- self.scanner = _compile("|".join(p))
+ p.append(sre_parse.SubPattern(s, [
+ (SUBPATTERN, (None, sre_parse.parse(phrase))),
+ (INDEX, len(p))
+ ]))
+ p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
+ s.groups = len(p)
+ self.scanner = sre_compile.compile(p)
def scan(self, string):
result = []
append = result.append