diff options
author | Georg Brandl <georg@python.org> | 2013-01-09 13:51:21 +0100 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2013-01-09 13:51:21 +0100 |
commit | 890bf1d3fe5a706fd699f2f7076785695e675f00 (patch) | |
tree | 1b14abc0dfeaedd11b424ede065ab3a46af825a4 /pygments/util.py | |
parent | 85d29e399ce3e8651af495ebb08baa0788480f17 (diff) | |
parent | c7baf27b4058f53f8d26be23e45fb3e7772656eb (diff) | |
download | pygments-890bf1d3fe5a706fd699f2f7076785695e675f00.tar.gz |
Merged in btiffin/cobol-pygments (pull request #72: adding an OpenCOBOL lexer)
Diffstat (limited to 'pygments/util.py')
-rw-r--r-- | pygments/util.py | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/pygments/util.py b/pygments/util.py index f8c6c824..caac1144 100644 --- a/pygments/util.py +++ b/pygments/util.py @@ -5,7 +5,7 @@ Utility functions. - :copyright: Copyright 2006-2012 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ @@ -206,6 +206,51 @@ def looks_like_xml(text): _looks_like_xml_cache[key] = rv return rv +# Python narrow build compatibility + +def _surrogatepair(c): + return (0xd7c0 + (c >> 10), (0xdc00 + (c & 0x3ff))) + +def unirange(a, b): + """ + Returns a regular expression string to match the given non-BMP range. + """ + if b < a: + raise ValueError("Bad character range") + if a < 0x10000 or b < 0x10000: + raise ValueError("unirange is only defined for non-BMP ranges") + + if sys.maxunicode > 0xffff: + # wide build + return u'[%s-%s]' % (unichr(a), unichr(b)) + else: + # narrow build stores surrogates, and the 're' module handles them + # (incorrectly) as characters. Since there is still ordering among + # these characters, expand the range to one that it understands. Some + # background in http://bugs.python.org/issue3665 and + # http://bugs.python.org/issue12749 + # + # Additionally, the lower constants are using unichr rather than + # literals because jython [which uses the wide path] can't load this + # file if they are literals. + ah, al = _surrogatepair(a) + bh, bl = _surrogatepair(b) + if ah == bh: + return u'(?:%s[%s-%s])' % (unichr(ah), unichr(al), unichr(bl)) + else: + buf = [] + buf.append(u'%s[%s-%s]' % + (unichr(ah), unichr(al), + ah == bh and unichr(bl) or unichr(0xdfff))) + if ah - bh > 1: + buf.append(u'[%s-%s][%s-%s]' % + unichr(ah+1), unichr(bh-1), unichr(0xdc00), unichr(0xdfff)) + if ah != bh: + buf.append(u'%s[%s-%s]' % + (unichr(bh), unichr(0xdc00), unichr(bl))) + + return u'(?:' + u'|'.join(buf) + u')' + # Python 2/3 compatibility if sys.version_info < (3,0): |