Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 1 | # |
| 2 | # Secret Labs' Regular Expression Engine |
| 3 | # $Id$ |
| 4 | # |
| 5 | # re-compatible interface for the sre matching engine |
| 6 | # |
| 7 | # Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved. |
| 8 | # |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 9 | # Portions of this engine have been developed in cooperation with |
| 10 | # CNRI. Hewlett-Packard provided funding for 1.6 integration and |
| 11 | # other compatibility work. |
| 12 | # |
| 13 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 14 | import sre_compile |
| 15 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 16 | # flags |
| 17 | I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE |
| 18 | L = LOCALE = sre_compile.SRE_FLAG_LOCALE |
| 19 | M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE |
| 20 | S = DOTALL = sre_compile.SRE_FLAG_DOTALL |
| 21 | X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE |
| 22 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 23 | # -------------------------------------------------------------------- |
| 24 | # public interface |
| 25 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 26 | # FIXME: add docstrings |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 27 | |
| 28 | def match(pattern, string, flags=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 29 | return _compile(pattern, flags).match(string) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 30 | |
| 31 | def search(pattern, string, flags=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 32 | return _compile(pattern, flags).search(string) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 33 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 34 | def sub(pattern, repl, string, count=0): |
| 35 | return _compile(pattern).sub(repl, string, count) |
| 36 | |
| 37 | def subn(pattern, repl, string, count=0): |
| 38 | return _compile(pattern).subn(repl, string, count) |
| 39 | |
| 40 | def split(pattern, string, maxsplit=0): |
| 41 | return _compile(pattern).split(string, maxsplit) |
| 42 | |
| 43 | def findall(pattern, string, maxsplit=0): |
| 44 | return _compile(pattern).findall(string, maxsplit) |
| 45 | |
| 46 | def compile(pattern, flags=0): |
| 47 | return _compile(pattern, flags) |
| 48 | |
| 49 | def escape(pattern): |
| 50 | s = list(pattern) |
| 51 | for i in range(len(pattern)): |
| 52 | c = pattern[i] |
| 53 | if not ("a" <= c <= "z" or "A" <= c <= "Z" or "0" <= c <= "9"): |
| 54 | if c == "\000": |
| 55 | s[i] = "\\000" |
| 56 | else: |
| 57 | s[i] = "\\" + c |
| 58 | return pattern[:0].join(s) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 59 | |
| 60 | # -------------------------------------------------------------------- |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 61 | # internals |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 62 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 63 | _cache = {} |
| 64 | _MAXCACHE = 100 |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 65 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 66 | def _compile(pattern, flags=0): |
| 67 | # internal: compile pattern |
| 68 | tp = type(pattern) |
| 69 | if tp not in (type(""), type(u"")): |
| 70 | return pattern |
| 71 | key = (tp, pattern, flags) |
| 72 | try: |
| 73 | return _cache[key] |
| 74 | except KeyError: |
| 75 | pass |
| 76 | p = sre_compile.compile(pattern, flags) |
| 77 | if len(_cache) >= _MAXCACHE: |
| 78 | _cache.clear() |
| 79 | _cache[key] = p |
| 80 | return p |
| 81 | |
| 82 | def _sub(pattern, template, string, count=0): |
| 83 | # internal: pattern.sub implementation hook |
| 84 | return _subn(pattern, template, string, count)[0] |
| 85 | |
| 86 | def _expand(match, template): |
| 87 | # internal: expand template |
| 88 | return template # FIXME |
| 89 | |
| 90 | def _subn(pattern, template, string, count=0): |
| 91 | # internal: pattern.subn implementation hook |
| 92 | if callable(template): |
| 93 | filter = callable |
| 94 | else: |
| 95 | # FIXME: prepare template |
| 96 | def filter(match, template=template): |
| 97 | return _expand(match, template) |
| 98 | n = i = 0 |
| 99 | s = [] |
| 100 | append = s.append |
| 101 | c = pattern.cursor(string) |
| 102 | while not count or n < count: |
| 103 | m = c.search() |
| 104 | if not m: |
| 105 | break |
| 106 | j = m.start() |
| 107 | if j > i: |
| 108 | append(string[i:j]) |
| 109 | append(filter(m)) |
| 110 | i = m.end() |
| 111 | n = n + 1 |
| 112 | if i < len(string): |
| 113 | append(string[i:]) |
| 114 | return string[:0].join(s), n |
| 115 | |
| 116 | def _split(pattern, string, maxsplit=0): |
| 117 | # internal: pattern.split implementation hook |
| 118 | n = i = 0 |
| 119 | s = [] |
| 120 | append = s.append |
| 121 | c = pattern.cursor(string) |
| 122 | while not maxsplit or n < maxsplit: |
| 123 | m = c.search() |
| 124 | if not m: |
| 125 | break |
| 126 | j = m.start() |
| 127 | append(string[i:j]) |
| 128 | i = m.end() |
| 129 | n = n + 1 |
| 130 | if i < len(string): |
| 131 | append(string[i:]) |
| 132 | return s |