11import argparse
22import re
3+ from htmlentitydefs import codepoint2name
34
45# this must match the equivalent function in qstr.c
56def compute_hash (qstr ):
@@ -10,7 +11,7 @@ def compute_hash(qstr):
1011
1112def do_work (infiles ):
1213 # read the qstrs in from the input files
13- qstrs = []
14+ qstrs = {}
1415 for infile in infiles :
1516 with open (infile , 'rt' ) as f :
1617 line_number = 0
@@ -23,28 +24,29 @@ def do_work(infiles):
2324 continue
2425
2526 # verify line is of the correct form
26- match = re .match (r'Q\(([0-9A-Za-z_] +)\)$' , line )
27+ match = re .match (r'Q\((. +)\)$' , line )
2728 if not match :
2829 print ('({}:{}) bad qstr format, got {}' .format (infile , line_number , line ))
2930 return False
3031
3132 # get the qstr value
3233 qstr = match .group (1 )
34+ ident = re .sub (r'[^A-Za-z0-9_]' , lambda s : "_" + codepoint2name [ord (s .group (0 ))] + "_" , qstr )
3335
3436 # don't add duplicates
35- if qstr in qstrs :
37+ if ident in qstrs :
3638 continue
3739
3840 # add the qstr to the list
39- qstrs . append ( qstr )
41+ qstrs [ ident ] = qstr
4042
4143 # process the qstrs, printing out the generated C header file
4244 print ('// This file was automatically generated by makeqstrdata.py' )
4345 print ('' )
44- for qstr in qstrs :
46+ for ident , qstr in qstrs . items () :
4547 qhash = compute_hash (qstr )
4648 qlen = len (qstr )
47- print ('Q({}, (const byte*)"\\ x{:02x}\\ x{:02x}\\ x{:02x}\\ x{:02x}" "{}")' .format (qstr , qhash & 0xff , (qhash >> 8 ) & 0xff , qlen & 0xff , (qlen >> 8 ) & 0xff , qstr ))
49+ 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 ))
4850
4951 return True
5052
0 commit comments