libxml2/tools/genEscape.py
Nick Wellnhofer 2adcde3920 save: Optimize xmlSerializeText
Use lookup tables.
2024-07-16 17:42:10 +02:00

79 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
entities = [
[ '', '�' ],
[ '\t', '	' ],
[ '\n', '
' ],
[ '\r', '
' ],
[ '"', '"' ],
[ '&', '&' ],
[ '<', '&lt;' ],
[ '>', '&gt;' ],
]
### xmlEscapeContent
offset = [ None ] * 128
pos = 0
r = ''
for rec in entities:
char, repl = rec
if char:
offset[ord(char)] = pos
if pos % 12 == 0: r += '\n '
else: r += ' '
r += '%3d,' % len(repl)
pos += 1
for c in repl:
if pos % 12 == 0: r += '\n '
else: r += ' '
r += "'%s'," % c
pos += 1
print('static const char xmlEscapeContent[] = {%s\n};\n' % r)
### xmlEscapeTab
escape = '\r&<>'
r = ''
for i in range(0x80):
if chr(i) in escape:
v = offset[i]
elif i != 9 and i != 10 and i < 20:
v = 0
else:
v = -1
if i % 16 == 0: r += '\n '
else: r += ' '
r += '%2d,' % v
print('static const char xmlEscapeTab[128] = {%s\n};\n' % r)
### xmlEscapeTabAttr
escape = '\t\n\r"&<>'
r = ''
for i in range(0x80):
if chr(i) in escape:
v = offset[i]
elif i != 9 and i != 10 and i < 20:
v = 0
else:
v = -1
if i % 16 == 0: r += '\n '
else: r += ' '
r += '%2d,' % v
print('static const char xmlEscapeTabAttr[128] = {%s\n};\n' % r)