improved unescaping a bit
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 1986d2c · Browse files · 1 file·+28 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+28 added−23 removed
- other:+28 added−23 removed
Provenance
- Commit
1d51f63- Tree
79d6447- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1986d2c
1 file changed+28 added−23 removed
modified:jinja/lexer.py+28 added-23 removed
17 unmodified lines18 19 This is required in order to make "class" and some other python keywords20 we don't use valid identifiers.21 22- :copyright: 2007 by Armin Ronacher.22+ :copyright: 2007-2008 by Armin Ronacher.23 :license: BSD, see LICENSE for more details.24 """25 import re26 import unicodedata63 unmodified lines90 assert len(operators) == len(reverse_operators), 'operators dropped'91 operator_re = re.compile('(%s)' % '|'.join([re.escape(x) for x in92 sorted(operators, key=lambda x: -len(x))]))93 94+simple_escapes = {95+ 'a': '\a',96+ 'n': '\n',97+ 'r': '\r',98+ 'f': '\f',99+ 't': '\t',100+ 'v': '\v',101+ '\\': '\\',102+ '"': '"',103+ "'": "'",104+ '0': '\x00'105+}106+unicode_escapes = {107+ 'x': 2,108+ 'u': 4,109+ 'U': 8110+}111 112+113 def unescape_string(lineno, filename, s):114 r"""115 Unescape a string. Supported escapes:116 \a, \n, \r\, \f, \v, \\, \", \', \03 unmodified lines120 Not supported are \101 because imho redundant.121 """122 result = []123 write = result.append106- simple_escapes = {107- 'a': '\a',108- 'n': '\n',109- 'r': '\r',110- 'f': '\f',111- 't': '\t',112- 'v': '\v',113- '\\': '\\',114- '"': '"',115- "'": "'",116- '0': '\x00'117- }118- unicode_escapes = {119- 'x': 2,120- 'u': 4,121- 'U': 8122- }124 chariter = iter(s)125 next_char = chariter.next126 127+ # faster lookup128+ sescapes = simple_escapes129+ uescapes = unicode_escapes130+131 try:132 for char in chariter:133 if char == '\\':134 char = next_char()130- if char in simple_escapes:131- write(simple_escapes[char])132- elif char in unicode_escapes:133- seq = [next_char() for x in xrange(unicode_escapes[char])]135+ if char in sescapes:136+ write(sescapes[char])137+ elif char in uescapes:138+ seq = [next_char() for x in xrange(uescapes[char])]139 try:140 write(unichr(int(''.join(seq), 16)))141 except ValueError:142 raise TemplateSyntaxError('invalid unicode codepoint',2 unmodified lines145 if next_char() != '{':146 raise TemplateSyntaxError('no name for codepoint',147 lineno, filename)148 seq = []144- while True:149+ while 1:150 char = next_char()151 if char == '}':152 break153 seq.append(char)365 unmodified lines