aboutsummaryrefslogtreecommitdiff
path: root/py/makeqstrdata.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-24 22:22:00 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-24 22:22:00 +0000
commit1976baeeb744299d795b0556719cae84c17ca803 (patch)
tree819aac01b5a0a18aba8f2fc06f59731ef5a39d73 /py/makeqstrdata.py
parent60aca4810f68811b6fc0b69d02ccb05ddf2e555f (diff)
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.
Diffstat (limited to 'py/makeqstrdata.py')
-rw-r--r--py/makeqstrdata.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py
index dbafd47d5..c5ad708e8 100644
--- a/py/makeqstrdata.py
+++ b/py/makeqstrdata.py
@@ -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))