我正在使用一些python代码来解析这个.h文件:
#include <limits.h>
// Offsets
// UINT_MAX == 4294967295 (Note: do not remove the comments, they are used by parser.py)
#define ID_OFFSET UINT_MAX - (1 << 6) // ID_OFFSET == 4294967231
#define CALL_NR_OFFSET ID_OFFSET - (1 << 20) // CALL_NR_OFFSET == 4293918655
// Range of lower 2^20 numbers is reserved for debug statements.
#define DEBUG_OFFSET (2 << 20) // DEBUG_OFFSET == 1048576
// IDs for jpeg library functions
#define INIT_HEADER_VLD_ID ID_OFFSET + 1
#define HEADER_VLD_ID ID_OFFSET + 2
#define IQZZ_ID ID_OFFSET + 3
#define IDCT_ID ID_OFFSET + 4
#define CC_ID ID_OFFSET + 5
#define RASTER_ID ID_OFFSET + 6
// Core frequencies
#define MB1_FREQ 2.5 // in Mhz
#define MB2_FREQ 2.5 // in Mhz
#define MB3_FREQ 3 // in Mhz
#define MB4_FREQ 3 // in Mhz
我有多个像这样的正则表达式:
uint_max_re = re.compile('UINT_MAX\s+=+\s+(\d+)\s+') # https://regexr.com/3pmk5
id_offset_re = re.compile('ID_OFFSET\s+=+\s+(\d+)') # https://regexr.com/3pmn8
call_nr_offset_re = re.compile('CALL_NR_OFFSET\s+=+\s+(\d+)') # https://regexr.com/3pmnh
debug_offset_re = re.compile('DEBUG_OFFSET\s+=+\s+(\d+)') # https://regexr.com/3pmnk
function_ids_re = re.compile('#define\s+(\w+)\s+ID_OFFSET\s+\+\s+(\d)') # https://regexr.com/3pmkt
core_freqs_re = re.compile('#define MB(\d+)_FREQ\s+(\d[.\d]*)') # https://regexr.com/3pml6
def parse_wrapper_params():
"This parses the wrapper_params.h file and returns a dictionary with function_id as key and function_name as value"
with open(wrapper_params_file, 'r') as f:
uint_max = int(uint_max_re.findall(f.read())[0])
with open(wrapper_params_file, 'r') as f:
id_offset = int(id_offset_re.findall(f.read())[0])
with open(wrapper_params_file, 'r') as f:
call_nr_offset = int(call_nr_offset_re.findall(f.read())[0])
with open(wrapper_params_file, 'r') as f:
debug_offset = int(debug_offset_re.findall(f.read())[0])
with open(wrapper_params_file, 'r') as f:
function_ids = function_ids_re.findall(f.read())
with open(wrapper_params_file, 'r') as f:
core_freqs = core_freqs_re.findall(f.read())
return {
'uint_max': uint_max,
'id_offset': id_offset,
'call_nr_offset': call_nr_offset,
'debug_offset': debug_offset,
'function_ids': {int(id_offset) + int(function_id): function_name.lower() for function_name, function_id in function_ids},
'core_freqs': {int(core): freq for core, freq in core_freqs}
}
现在这很慢,所以我只想用一个with open(wrapper_params_file, 'r') as f:
来寻找一种方法,但是如果我删除第二个,那么正则表达式什么也找不到。
感谢任何帮助。感谢。
答案 0 :(得分:3)
将内容读入变量一次。另外,请使用.search()
代替.findall()
,因为您只需要第一次匹配。
with open(wrapper_params_file, 'r') as f:
contents = f.read()
uint_max = uint_max_re.search(contents).group(1)
id_offset = id_offset_re.search(contents).group(1)