diff options
author | thatch <devnull@localhost> | 2008-07-23 22:39:42 -0700 |
---|---|---|
committer | thatch <devnull@localhost> | 2008-07-23 22:39:42 -0700 |
commit | 1ca25b3a82670fd96be885ef5682e6a0962d48db (patch) | |
tree | de8c3d4d175e408ca358fa8ffb302a004ad6c27c /pygments/util.py | |
parent | d73de5d469217d37388acdeebec7623a860a24a8 (diff) | |
download | pygments-1ca25b3a82670fd96be885ef5682e6a0962d48db.tar.gz |
Add a cache for `looks_like_xml` and limit it to the first thousand bytes, as
the regex has a tendency to go off into the woods when fed non-xml data.
Diffstat (limited to 'pygments/util.py')
-rw-r--r-- | pygments/util.py | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/pygments/util.py b/pygments/util.py index 8232964c..0e8c952a 100644 --- a/pygments/util.py +++ b/pygments/util.py @@ -179,11 +179,18 @@ def html_doctype_matches(text): return doctype_matches(text, r'html\s+PUBLIC\s+"-//W3C//DTD X?HTML.*') +_looks_like_xml_cache = {} def looks_like_xml(text): """ Check if a doctype exists or if we have some tags. """ - m = doctype_lookup_re.match(text) - if m is not None: - return True - return tag_re.search(text) is not None + key = hash(text) + try: + return _looks_like_xml_cache[key] + except KeyError: + m = doctype_lookup_re.match(text) + if m is not None: + return True + rv = tag_re.search(text[:1000]) is not None + _looks_like_xml_cache[key] = rv + return rv |