@@ -45,7 +45,10 @@ def compute_hash(qstr, bytes_hash):
4545 # Make sure that valid hash is never zero, zero means "hash not computed"
4646 return (hash & ((1 << (8 * bytes_hash )) - 1 )) or 1
4747
48- def do_work (infiles ):
48+ def qstr_escape (qst ):
49+ return re .sub (r'[^A-Za-z0-9_]' , lambda s : "_" + codepoint2name [ord (s .group (0 ))] + '_' , qst )
50+
51+ def parse_input_headers (infiles ):
4952 # read the qstrs in from the input files
5053 qcfgs = {}
5154 qstrs = {}
@@ -71,7 +74,7 @@ def do_work(infiles):
7174
7275 # get the qstr value
7376 qstr = match .group (1 )
74- ident = re . sub ( r'[^A-Za-z0-9_]' , lambda s : "_" + codepoint2name [ ord ( s . group ( 0 ))] + "_" , qstr )
77+ ident = qstr_escape ( qstr )
7578
7679 # don't add duplicates
7780 if ident in qstrs :
@@ -84,10 +87,24 @@ def do_work(infiles):
8487 sys .stderr .write ("ERROR: Empty preprocessor output - check for errors above\n " )
8588 sys .exit (1 )
8689
90+ return qcfgs , qstrs
91+
92+ def make_bytes (cfg_bytes_len , cfg_bytes_hash , qstr ):
93+ qhash = compute_hash (qstr , cfg_bytes_hash )
94+ # Calculate len of str, taking escapes into account
95+ qlen = len (qstr .replace ("\\ \\ " , "-" ).replace ("\\ " , "" ))
96+ qdata = qstr .replace ('"' , '\\ "' )
97+ if qlen >= (1 << (8 * cfg_bytes_len )):
98+ print ('qstr is too long:' , qstr )
99+ assert False
100+ qlen_str = ('\\ x%02x' * cfg_bytes_len ) % tuple (((qlen >> (8 * i )) & 0xff ) for i in range (cfg_bytes_len ))
101+ qhash_str = ('\\ x%02x' * cfg_bytes_hash ) % tuple (((qhash >> (8 * i )) & 0xff ) for i in range (cfg_bytes_hash ))
102+ return '(const byte*)"%s%s" "%s"' % (qhash_str , qlen_str , qdata )
103+
104+ def print_qstr_data (qcfgs , qstrs ):
87105 # get config variables
88106 cfg_bytes_len = int (qcfgs ['BYTES_IN_LEN' ])
89107 cfg_bytes_hash = int (qcfgs ['BYTES_IN_HASH' ])
90- cfg_max_len = 1 << (8 * cfg_bytes_len )
91108
92109 # print out the starter of the generated C header file
93110 print ('// This file was automatically generated by makeqstrdata.py' )
@@ -98,16 +115,12 @@ def do_work(infiles):
98115
99116 # go through each qstr and print it out
100117 for order , ident , qstr in sorted (qstrs .values (), key = lambda x : x [0 ]):
101- qhash = compute_hash (qstr , cfg_bytes_hash )
102- # Calculate len of str, taking escapes into account
103- qlen = len (qstr .replace ("\\ \\ " , "-" ).replace ("\\ " , "" ))
104- qdata = qstr .replace ('"' , '\\ "' )
105- if qlen >= cfg_max_len :
106- print ('qstr is too long:' , qstr )
107- assert False
108- qlen_str = ('\\ x%02x' * cfg_bytes_len ) % tuple (((qlen >> (8 * i )) & 0xff ) for i in range (cfg_bytes_len ))
109- qhash_str = ('\\ x%02x' * cfg_bytes_hash ) % tuple (((qhash >> (8 * i )) & 0xff ) for i in range (cfg_bytes_hash ))
110- print ('QDEF(MP_QSTR_%s, (const byte*)"%s%s" "%s")' % (ident , qhash_str , qlen_str , qdata ))
118+ qbytes = make_bytes (cfg_bytes_len , cfg_bytes_hash , qstr )
119+ print ('QDEF(MP_QSTR_%s, %s)' % (ident , qbytes ))
120+
121+ def do_work (infiles ):
122+ qcfgs , qstrs = parse_input_headers (infiles )
123+ print_qstr_data (qcfgs , qstrs )
111124
112125if __name__ == "__main__" :
113126 do_work (sys .argv [1 :])
0 commit comments