From 2c61c77faf573b4b0155c126400c87b264157492 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 14 Apr 2014 14:08:13 +0100 Subject: [PATCH 1/2] make makeqstrdata.py more pythonic replace list-of-tuples with a dict --- py/makeqstrdata.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index e0917b3367391..fd338ce59079a 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -24,10 +24,15 @@ def compute_hash(qstr): hash = (hash * 33) ^ ord(char) return hash & 0xffff -# given a list of (name,regex) pairs, find the first one that matches the given line -def re_match(regexs, line): - for name, regex in regexs: - match = re.match(regex, line) +cpp_regexes = { + 'qstr': r'Q\((.+)\)$', + 'cdecl': r'(typedef|extern) [A-Za-z0-9_* ]+;$', +} + +# given a line, find which cpp regex it matches +def get_line_match(line): + for name in cpp_regexes: + match = re.match(cpp_regexes[name], line) if match: return name, match return None, None @@ -47,12 +52,12 @@ def do_work(infiles): continue # work out what kind of line it is - match_kind, match = re_match([('qstr', r'Q\((.+)\)$'), ('cdecl', r'(typedef|extern) [A-Za-z0-9_* ]+;$')], line) - if match_kind is None: + line_type, match = get_line_match(line) + if line_type is None: # unknown line format print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line), file=sys.stderr) return False - elif match_kind != 'qstr': + elif line_type != 'qstr': # not a line with a qstr continue From acb5b86583da6bde68e2e0cc8ea89219b99d1875 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 14 Apr 2014 22:14:12 +0100 Subject: [PATCH 2/2] Modified makeqstrdata.py to use OrderedDict so that regexes always get checked in correct order --- py/makeqstrdata.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index fd338ce59079a..fa92361715bb5 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -3,6 +3,7 @@ import argparse import re import sys +from collections import OrderedDict # codepoint2name is different in Python 2 to Python 3 import platform @@ -24,12 +25,11 @@ def compute_hash(qstr): hash = (hash * 33) ^ ord(char) return hash & 0xffff -cpp_regexes = { - 'qstr': r'Q\((.+)\)$', - 'cdecl': r'(typedef|extern) [A-Za-z0-9_* ]+;$', -} +cpp_regexes = OrderedDict() +cpp_regexes['qstr'] = r'Q\((.+)\)$' +cpp_regexes['cdecl'] = r'(typedef|extern) [A-Za-z0-9_* ]+;$' -# given a line, find which cpp regex it matches +# given a line, find the first cpp regex it matches def get_line_match(line): for name in cpp_regexes: match = re.match(cpp_regexes[name], line)