Retain file order of qstr definitions.

Want common qstrs to be first in the list so they have the lowest ids,
so that in the byte code they take up the least room.
This commit is contained in:
Damien George 2014-01-24 22:22:00 +00:00
parent 60aca4810f
commit 1976baeeb7

View File

@ -1,6 +1,12 @@
import argparse
import re
from htmlentitydefs import codepoint2name
# codepoint2name is different in Python 2 to Python 3
import platform
if platform.python_version_tuple()[0] == '2':
from htmlentitydefs import codepoint2name
elif platform.python_version_tuple()[0] == '3':
from html.entities import codepoint2name
# this must match the equivalent function in qstr.c
def compute_hash(qstr):
@ -37,13 +43,13 @@ def do_work(infiles):
if ident in qstrs:
continue
# add the qstr to the list
qstrs[ident] = qstr
# add the qstr to the list, with order number to retain original order in file
qstrs[ident] = (len(qstrs), ident, qstr)
# process the qstrs, printing out the generated C header file
print('// This file was automatically generated by makeqstrdata.py')
print('')
for ident, qstr in qstrs.items():
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
qhash = compute_hash(qstr)
qlen = len(qstr)
print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr))