diff --git a/DeThunk/src/header_preprocessor.py b/DeThunk/src/header_preprocessor.py index 3268ecb..8c173f2 100644 --- a/DeThunk/src/header_preprocessor.py +++ b/DeThunk/src/header_preprocessor.py @@ -105,7 +105,7 @@ def process(path_to_file: str): is_template, is_empty, ) - class_keyword_pos, _ = StrUtil.find_m(line, 'class ', 'struct ', 'union ') + class_keyword_pos, _ = StrUtil.find_mb(line, 'class ', 'struct ', 'union ') assert class_keyword_pos != -1, f"path = {path_to_file}, line = '{line}'" if not is_empty: diff --git a/DeThunk/src/header_processor.py b/DeThunk/src/header_processor.py index 2ade8ac..b80a37c 100644 --- a/DeThunk/src/header_processor.py +++ b/DeThunk/src/header_processor.py @@ -302,7 +302,7 @@ class reference_wrapper { if not in_forward_declaration_list: founded_cl = CppUtil.find_class_definition(line) if founded_cl: - class_keyword_pos, _ = StrUtil.find_m(line, 'class ', 'struct ', 'union ') + class_keyword_pos, _ = StrUtil.find_mb(line, 'class ', 'struct ', 'union ') assert class_keyword_pos != -1, f"path = {path_to_file}, line = '{line}'" if not stripped_line.endswith('{};'): diff --git a/DeThunk/src/util/cpp_language.py b/DeThunk/src/util/cpp_language.py index 2cab70b..879a253 100644 --- a/DeThunk/src/util/cpp_language.py +++ b/DeThunk/src/util/cpp_language.py @@ -18,7 +18,7 @@ def find_class_definition(line: str) -> str | None: # KEYWORD A { # KEYWORD A { ... }; (in single line) - keyword_pos, keyword = StrUtil.find_m(line, 'enum class ', 'class ', 'struct ', 'union ') + keyword_pos, keyword = StrUtil.find_mb(line, 'enum class ', 'class ', 'struct ', 'union ') if keyword_pos == -1 or keyword == 'enum class ': return None @@ -67,7 +67,7 @@ def find_class_forward_declaration(line: str) -> ForwardDeclaration | None: if namespace_pos != -1 and left_brace_pos != -1: namespace_decl = line[namespace_pos + len('namespace') : left_brace_pos].strip() - keyword_pos, keyword = StrUtil.find_m(line, 'class ', 'struct ', 'union ') + keyword_pos, keyword = StrUtil.find_mb(line, 'class ', 'struct ', 'union ') if keyword_pos == -1: return None diff --git a/DeThunk/src/util/string.py b/DeThunk/src/util/string.py index 793cbc2..6d1862f 100644 --- a/DeThunk/src/util/string.py +++ b/DeThunk/src/util/string.py @@ -19,6 +19,20 @@ def endswith_m(con: str, *args) -> bool: return False +def find_mb(con: str, *args) -> int: # bounded + r_pos = -1 + r_arg = None + for arg in args: + matched = re.search(rf'\b{re.escape(arg)}\b', con) + if not matched: + continue + pos = matched.start() + if pos != -1 and (r_pos == -1 or pos < r_pos): + r_pos = pos + r_arg = arg + return r_pos, r_arg + + def find_m(con: str, *args) -> int: r_pos = -1 r_arg = None