aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-09-01 15:34:23 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-09-02 16:07:29 +0000
commita0438c482354ca909efc1fd94e8810c2d5b185d3 (patch)
tree4d257d77e7660624bdbe84eecee552010dcaaab6
parent6b757b19c6b7c323e64ed3c5f2e8e2ee6c7456b0 (diff)
snippets_translate: Speed up regexp evaluation
Precompile the regular expressions and store the stripped string. Brings down user time from 2.3s to 2s, approximately. Change-Id: If929dc0c85cbe3e797bb3ec01eff9c434a8c5527 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> (cherry picked from commit c0387967f81dcabe3087a741e337d01ca177d0b1) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--tools/snippets_translate/converter.py101
-rw-r--r--tools/snippets_translate/handlers.py126
2 files changed, 134 insertions, 93 deletions
diff --git a/tools/snippets_translate/converter.py b/tools/snippets_translate/converter.py
index 77250a857..92c89bdf6 100644
--- a/tools/snippets_translate/converter.py
+++ b/tools/snippets_translate/converter.py
@@ -50,6 +50,31 @@ from handlers import (handle_array_declarations, handle_casts, handle_class,
from parse_utils import dstrip, get_indent, remove_ref
+VOID_METHOD_PATTERN = re.compile(r"^ *void *[\w\_]+(::)?[\w\d\_]+\(")
+QT_QUALIFIER_PATTERN = re.compile(r"Q[\w]+::")
+TERNARY_OPERATOR_PATTERN = re.compile(r"^.* \? .+ : .+$")
+COUT_PATTERN = re.compile("^ *(std::)?cout")
+FOR_PATTERN = re.compile(r"^ *for *\(")
+FOREACH_PATTERN = re.compile(r"^ *foreach *\(")
+ELSE_PATTERN = re.compile(r"^ *}? *else *{?")
+ELSE_REPLACEMENT_PATTERN = re.compile(r"}? *else *{?")
+CLASS_PATTERN = re.compile(r"^ *class ")
+STRUCT_PATTERN = re.compile(r"^ *struct ")
+DELETE_PATTERN = re.compile(r"^ *delete ")
+PUBLIC_PATTERN = re.compile(r"^public:$")
+PRIVATE_PATTERN = re.compile(r"^private:$")
+VAR1_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*\&]+(\(.*?\))? ?(?!.*=|:).*$")
+VAR2_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w]+::[\w\*\&]+\(.*\)$")
+VAR3_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+ *= *[\w\.\"\']*(\(.*?\))?")
+VAR4_PATTERN = re.compile(r"\w+ = [A-Z]{1}\w+")
+CONSTRUCTOR_PATTERN = re.compile(r"^ *\w+::\w+\(.*?\)")
+ARRAY_VAR_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+\[?\]? * =? *\{")
+RETURN_TYPE_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w]+::[\w\*\&]+\(.*\)$")
+FUNCTION_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*\&]+\(.*\)$")
+ITERATOR_PATTERN = re.compile(r"(std::)?[\w]+<[\w]+>::(const_)?iterator")
+SCOPE_PATTERN = re.compile(r"[\w]+::")
+
+
def snippet_translate(x):
## Cases which are not C++
@@ -64,15 +89,16 @@ def snippet_translate(x):
x = x[:-1]
# Remove lines with only '{' or '}'
- if x.strip() == "{" or x.strip() == "}":
+ xs = x.strip()
+ if xs == "{" or xs == "}":
return ""
# Skip lines with the snippet related identifier '//!'
- if x.strip().startswith("//!"):
+ if xs.startswith("//!"):
return x
# handle lines with only comments using '//'
- if x.lstrip().startswith("//"):
+ if xs.startswith("//"):
x = x.replace("//", "#", 1)
return x
@@ -144,11 +170,11 @@ def snippet_translate(x):
x = handle_keywords(x, "throw", "raise")
# handle 'void Class::method(...)' and 'void method(...)'
- if re.search(r"^ *void *[\w\_]+(::)?[\w\d\_]+\(", x):
+ if VOID_METHOD_PATTERN.search(x):
x = handle_void_functions(x)
# 'Q*::' -> 'Q*.'
- if re.search(r"Q[\w]+::", x):
+ if QT_QUALIFIER_PATTERN.search(x):
x = x.replace("::", ".")
# handle 'nullptr'
@@ -156,77 +182,79 @@ def snippet_translate(x):
x = x.replace("nullptr", "None")
## Special Cases Rules
-
+ xs = x.strip()
# Special case for 'main'
- if x.strip().startswith("int main("):
+ if xs.startswith("int main("):
return f'{get_indent(x)}if __name__ == "__main__":'
- if x.strip().startswith("QApplication app(argc, argv)"):
+ if xs.startswith("QApplication app(argc, argv)"):
return f"{get_indent(x)}app = QApplication([])"
# Special case for 'return app.exec()'
- if x.strip().startswith("return app.exec"):
+ if xs.startswith("return app.exec"):
return x.replace("return app.exec()", "sys.exit(app.exec())")
# Handle includes -> import
- if x.strip().startswith("#include"):
+ if xs.startswith("#include"):
x = handle_include(x)
return dstrip(x)
- if x.strip().startswith("emit "):
+ if xs.startswith("emit "):
x = handle_emit(x)
return dstrip(x)
# *_cast
if "_cast<" in x:
x = handle_casts(x)
+ xs = x.strip()
# Handle Qt classes that needs to be removed
x = handle_useless_qt_classes(x)
# Handling ternary operator
- if re.search(r"^.* \? .+ : .+$", x.strip()):
+ if TERNARY_OPERATOR_PATTERN.search(xs):
x = x.replace(" ? ", " if ")
x = x.replace(" : ", " else ")
+ xs = x.strip()
# Handle 'while', 'if', and 'else if'
# line might end in ')' or ") {"
- if x.strip().startswith(("while", "if", "else if", "} else if")):
+ if xs.startswith(("while", "if", "else if", "} else if")):
x = handle_conditions(x)
return dstrip(x)
- elif re.search("^ *}? *else *{?", x):
- x = re.sub(r"}? *else *{?", "else:", x)
+ elif ELSE_PATTERN.search(x):
+ x = ELSE_REPLACEMENT_PATTERN.sub("else:", x)
return dstrip(x)
# 'cout' and 'endl'
- if re.search("^ *(std::)?cout", x) or ("endl" in x) or x.lstrip().startswith("qDebug()"):
+ if COUT_PATTERN.search(x) or ("endl" in x) or xs.startswith("qDebug()"):
x = handle_cout_endl(x)
return dstrip(x)
# 'for' loops
- if re.search(r"^ *for *\(", x.strip()):
+ if FOR_PATTERN.search(xs):
return dstrip(handle_for(x))
# 'foreach' loops
- if re.search(r"^ *foreach *\(", x.strip()):
+ if FOREACH_PATTERN.search(xs):
return dstrip(handle_foreach(x))
# 'class' and 'structs'
- if re.search(r"^ *class ", x) or re.search(r"^ *struct ", x):
+ if CLASS_PATTERN.search(x) or STRUCT_PATTERN.search(x):
if "struct " in x:
x = x.replace("struct ", "class ")
return handle_class(x)
# 'delete'
- if re.search(r"^ *delete ", x):
+ if DELETE_PATTERN.search(x):
return x.replace("delete", "del")
# 'public:'
- if re.search(r"^public:$", x.strip()):
+ if PUBLIC_PATTERN.search(xs):
return x.replace("public:", "# public")
# 'private:'
- if re.search(r"^private:$", x.strip()):
+ if PRIVATE_PATTERN.search(xs):
return x.replace("private:", "# private")
# For expressions like: `Type var`
@@ -242,9 +270,9 @@ def snippet_translate(x):
# At the end we skip methods with the form:
# QStringView Message::body()
# to threat them as methods.
- if (re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*\&]+(\(.*?\))? ?(?!.*=|:).*$", x.strip())
- and x.strip().split()[0] not in ("def", "return", "and", "or")
- and not re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w]+::[\w\*\&]+\(.*\)$", x.strip())
+ if (VAR1_PATTERN.search(xs)
+ and xs.split()[0] not in ("def", "return", "and", "or")
+ and not VAR2_PATTERN.search(xs)
and ("{" not in x and "}" not in x)):
# FIXME: this 'if' is a hack for a function declaration with this form:
@@ -261,7 +289,7 @@ def snippet_translate(x):
# QSome thing = b(...)
# float v = 0.1
# QSome *thing = ...
- if (re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+ *= *[\w\.\"\']*(\(.*?\))?", x.strip())
+ if (VAR3_PATTERN.search(xs)
and ("{" not in x and "}" not in x)):
left, right = x.split("=", 1)
var_name = " ".join(left.strip().split()[1:])
@@ -272,23 +300,23 @@ def snippet_translate(x):
# layout = QVBoxLayout
# so we need to add '()' at the end if it's just a word
# with only alpha numeric content
- if re.search(r"\w+ = [A-Z]{1}\w+", x.strip()) and not x.strip().endswith(")"):
+ if VAR4_PATTERN.search(xs) and not xs.endswith(")"):
x = f"{x.rstrip()}()"
return dstrip(x)
# For constructors, that we now the shape is:
# ClassName::ClassName(...)
- if re.search(r"^ *\w+::\w+\(.*?\)", x.strip()):
+ if CONSTRUCTOR_PATTERN.search(xs):
x = handle_constructors(x)
return dstrip(x)
# For base object constructor:
# : QWidget(parent)
if (
- x.strip().startswith(": ")
+ xs.startswith(": ")
and ("<<" not in x)
and ("::" not in x)
- and not x.strip().endswith(";")
+ and not xs.endswith(";")
):
return handle_constructor_default_values(x)
@@ -297,27 +325,30 @@ def snippet_translate(x):
# type var_name[] = {...
# type var_name {...
# if re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+\[\] * = *\{", x.strip()):
- if re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+\[?\]? * =? *\{", x.strip()):
+ if ARRAY_VAR_PATTERN.search(xs):
x = handle_array_declarations(x)
+ xs = x.strip()
# Methods with return type
# int Class::method(...)
# QStringView Message::body()
- if re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w]+::[\w\*\&]+\(.*\)$", x.strip()):
+ if RETURN_TYPE_PATTERN.search(xs):
# We just need to capture the 'method name' and 'arguments'
x = handle_methods_return_type(x)
+ xs = x.strip()
# Handling functions
# By this section of the function, we cover all the other cases
# So we can safely assume it's not a variable declaration
- if re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*\&]+\(.*\)$", x.strip()):
+ if FUNCTION_PATTERN.search(xs):
x = handle_functions(x)
+ xs = x.strip()
# if it is a C++ iterator declaration, then ignore it due to dynamic typing in Python
# eg: std::vector<int> it;
# the case of iterator being used inside a for loop is already handed in handle_for(..)
# TODO: handle iterator initialization statement like it = container.begin();
- if re.search(r"(std::)?[\w]+<[\w]+>::(const_)?iterator", x):
+ if ITERATOR_PATTERN.search(x):
x = ""
return x
@@ -325,7 +356,7 @@ def snippet_translate(x):
# 'Namespace*::' -> 'Namespace*.'
# TODO: In the case where a C++ class function is defined outside the class, this would be wrong
# but we do not have such a code snippet yet
- if re.search(r"[\w]+::", x):
+ if SCOPE_PATTERN.search(x):
x = x.replace("::", ".")
# General return for no special cases
diff --git a/tools/snippets_translate/handlers.py b/tools/snippets_translate/handlers.py
index 47307de3b..26633fc50 100644
--- a/tools/snippets_translate/handlers.py
+++ b/tools/snippets_translate/handlers.py
@@ -44,9 +44,37 @@ from parse_utils import (dstrip, get_indent, get_qt_module_class,
parse_arguments, remove_ref, replace_main_commas)
IF_PATTERN = re.compile(r'^\s*if\s*\(')
+PARENTHESES_NONEMPTY_CONTENT_PATTERN = re.compile(r"\((.+)\)")
+LOCAL_INCLUDE_PATTERN = re.compile(r'"(.*)"')
+GLOBAL_INCLUDE_PATTERN = re.compile(r"<(.*)>")
+IF_CONDITION_PATTERN = PARENTHESES_NONEMPTY_CONTENT_PATTERN
ELSE_IF_PATTERN = re.compile(r'^\s*}?\s*else if\s*\(')
WHILE_PATTERN = re.compile(r'^\s*while\s*\(')
CAST_PATTERN = re.compile(r"[a-z]+_cast<(.*?)>\((.*?)\)") # Non greedy match of <>
+ITERATOR_LOOP_PATTERN = re.compile(r"= *(.*)egin\(")
+REMOVE_TEMPLATE_PARAMETER_PATTERN = re.compile("<.*>")
+PARENTHESES_CONTENT_PATTERN = re.compile(r"\((.*)\)")
+CONSTRUCTOR_BODY_PATTERN = re.compile(".*{ *}.*")
+CONSTRUCTOR_BODY_REPLACEMENT_PATTERN = re.compile("{ *}")
+CONSTRUCTOR_BASE_PATTERN = re.compile("^ *: *")
+NEGATE_PATTERN = re.compile(r"!(.)")
+CLASS_TEMPLATE_PATTERN = re.compile(r".*<.*>")
+EMPTY_CLASS_PATTERN = re.compile(r".*{.*}")
+EMPTY_CLASS_REPLACEMENT_PATTERN = re.compile(r"{.*}")
+FUNCTION_BODY_PATTERN = re.compile(r"\{(.*)\}")
+ARRAY_DECLARATION_PATTERN = re.compile(r"^[a-zA-Z0-9\<\>]+ ([\w\*]+) *\[?\]?")
+RETURN_TYPE_PATTERN = re.compile(r"^ *[a-zA-Z0-9]+ [\w]+::([\w\*\&]+\(.*\)$)")
+CAPTURE_PATTERN = re.compile(r"^ *([a-zA-Z0-9]+) ([\w\*\&]+\(.*\)$)")
+USELESS_QT_CLASSES_PATTERNS = [
+ re.compile(r"QLatin1String\((.*)\)"),
+ re.compile(r"QLatin1Char\((.*)\)")
+]
+COMMENT1_PATTERN = re.compile(r" *# *[\w\ ]+$")
+COMMENT2_PATTERN = re.compile(r" *# *(.*)$")
+COUT_ENDL_PATTERN = re.compile(r"cout *<<(.*)<< *.*endl")
+COUT1_PATTERN = re.compile(r" *<< *")
+COUT2_PATTERN = re.compile(r".*cout *<<")
+COUT_ENDL2_PATTERN = re.compile(r"<< +endl")
def handle_condition(x, name):
@@ -63,10 +91,9 @@ def handle_condition(x, name):
comment = f" #{comment_content[-1]}"
x = x.replace(f"//{comment_content[-1]}", "")
- re_par = re.compile(r"\((.+)\)")
- match = re_par.search(x)
+ match = IF_CONDITION_PATTERN.search(x)
if match:
- condition = re_par.search(x).group(1)
+ condition = match.group(1)
return f"{get_indent(x)}{name} {condition.strip()}:{comment}"
else:
print(f'snippets_translate: Warning "{x}" does not match condition pattern',
@@ -110,8 +137,7 @@ def handle_casts(x):
def handle_include(x):
if '"' in x:
- re_par = re.compile(r'"(.*)"')
- header = re_par.search(x)
+ header = LOCAL_INCLUDE_PATTERN.search(x)
if header:
header_name = header.group(1).replace(".h", "")
module_name = header_name.replace('/', '.')
@@ -121,8 +147,7 @@ def handle_include(x):
# besides '"something.h"'
x = ""
elif "<" in x and ">" in x:
- re_par = re.compile(r"<(.*)>")
- name = re_par.search(x).group(1)
+ name = GLOBAL_INCLUDE_PATTERN.search(x).group(1)
t = get_qt_module_class(name)
# if it's not a Qt module or class, we discard it.
if t is None:
@@ -150,8 +175,7 @@ def handle_conditions(x):
def handle_for(x):
- re_content = re.compile(r"\((.*)\)")
- content = re_content.search(x)
+ content = PARENTHESES_CONTENT_PATTERN.search(x)
new_x = x
if content:
@@ -166,7 +190,7 @@ def handle_for(x):
# iterators
if "begin(" in x.lower() and "end(" in x.lower():
- name = re.search(r"= *(.*)egin\(", start)
+ name = ITERATOR_LOOP_PATTERN.search(start)
iterable = None
iterator = None
if name:
@@ -247,23 +271,22 @@ def handle_for(x):
def handle_foreach(x):
- re_content = re.compile(r"\((.*)\)")
- content = re_content.search(x)
+ content = PARENTHESES_CONTENT_PATTERN.search(x)
if content:
parenthesis = content.group(1)
iterator, iterable = parenthesis.split(",", 1)
# remove iterator type
it = dstrip(iterator.split()[-1])
# remove <...> from iterable
- value = re.sub("<.*>", "", iterable)
+ value = REMOVE_TEMPLATE_PARAMETER_PATTERN.sub("", iterable)
return f"{get_indent(x)}for {it} in {value}:"
def handle_type_var_declaration(x):
# remove content between <...>
if "<" in x and ">" in x:
- x = " ".join(re.sub("<.*>", "", i) for i in x.split())
- content = re.search(r"\((.*)\)", x)
+ x = " ".join(REMOVE_TEMPLATE_PARAMETER_PATTERN.sub("", i) for i in x.split())
+ content = PARENTHESES_CONTENT_PATTERN.search(x)
if content:
# this means we have something like:
# QSome thing(...)
@@ -279,8 +302,7 @@ def handle_type_var_declaration(x):
def handle_constructors(x):
- re_content = re.compile(r"\((.*)\)")
- arguments = re_content.search(x).group(1)
+ arguments = PARENTHESES_CONTENT_PATTERN.search(x).group(1)
class_method = x.split("(")[0].split("::")
if len(class_method) == 2:
# Equal 'class name' and 'method name'
@@ -298,8 +320,8 @@ def handle_constructor_default_values(x):
# we discard that section completely, since even with a single
# value, we don't need to take care of it, for example:
# ' : a(1) { } -> self.a = 1
- if re.search(".*{ *}.*", x):
- x = re.sub("{ *}", "", x)
+ if CONSTRUCTOR_BODY_PATTERN.search(x):
+ x = CONSTRUCTOR_BODY_REPLACEMENT_PATTERN.sub("", x)
values = "".join(x.split(":", 1))
# Check the commas that are not inside round parenthesis
@@ -314,26 +336,24 @@ def handle_constructor_default_values(x):
if "@" in values:
return_values = ""
for arg in values.split("@"):
- arg = re.sub("^ *: *", "", arg).strip()
+ arg = CONSTRUCTOR_BASE_PATTERN.sub("", arg).strip()
if arg.startswith("Q"):
class_name = arg.split("(")[0]
content = arg.replace(class_name, "")[1:-1]
return_values += f" {class_name}.__init__(self, {content})\n"
elif arg:
var_name = arg.split("(")[0]
- re_par = re.compile(r"\((.+)\)")
- content = re_par.search(arg).group(1)
+ content = PARENTHESES_NONEMPTY_CONTENT_PATTERN.search(arg).group(1)
return_values += f" self.{var_name} = {content}\n"
else:
- arg = re.sub("^ *: *", "", values).strip()
+ arg = CONSTRUCTOR_BASE_PATTERN.sub("", values).strip()
if arg.startswith("Q"):
class_name = arg.split("(")[0]
content = arg.replace(class_name, "")[1:-1]
return f" {class_name}.__init__(self, {content})"
elif arg:
var_name = arg.split("(")[0]
- re_par = re.compile(r"\((.+)\)")
- match = re_par.search(arg)
+ match = PARENTHESES_NONEMPTY_CONTENT_PATTERN.search(arg)
if match:
content = match.group(1)
return f" self.{var_name} = {content}"
@@ -347,27 +367,27 @@ def handle_constructor_default_values(x):
def handle_cout_endl(x):
# if comment at the end
comment = ""
- if re.search(r" *# *[\w\ ]+$", x):
- comment = f' # {re.search(" *# *(.*)$", x).group(1)}'
+ if COMMENT1_PATTERN.search(x):
+ match = COMMENT2_PATTERN.search(x).group(1)
+ comment = f' # {match}'
x = x.split("#")[0]
if "qDebug()" in x:
x = x.replace("qDebug()", "cout")
if "cout" in x and "endl" in x:
- re_cout_endl = re.compile(r"cout *<<(.*)<< *.*endl")
- data = re_cout_endl.search(x)
+ data = COUT_ENDL_PATTERN.search(x)
if data:
data = data.group(1)
- data = re.sub(" *<< *", ", ", data)
+ data = COUT1_PATTERN.sub(", ", data)
x = f"{get_indent(x)}print({data}){comment}"
elif "cout" in x:
- data = re.sub(".*cout *<<", "", x)
- data = re.sub(" *<< *", ", ", data)
+ data = COUT2_PATTERN.sub("", x)
+ data = COUT1_PATTERN.sub(", ", data)
x = f"{get_indent(x)}print({data}){comment}"
elif "endl" in x:
- data = re.sub("<< +endl", "", x)
- data = re.sub(" *<< *", ", ", data)
+ data = COUT_ENDL2_PATTERN.sub("", x)
+ data = COUT1_PATTERN.sub(", ", data)
x = f"{get_indent(x)}print({data}){comment}"
x = x.replace("( ", "(").replace(" )", ")").replace(" ,", ",").replace("(, ", "(")
@@ -383,8 +403,7 @@ def handle_negate(x):
elif "/*" in x:
if x.index("/*") < x.index("!"):
return x
- re_negate = re.compile(r"!(.)")
- next_char = re_negate.search(x).group(1)
+ next_char = NEGATE_PATTERN.search(x).group(1)
if next_char not in ("=", '"'):
x = x.replace("!", "not ")
return x
@@ -392,8 +411,7 @@ def handle_negate(x):
def handle_emit(x):
function_call = x.replace("emit ", "").strip()
- re_content = re.compile(r"\((.*)\)")
- match = re_content.search(function_call)
+ match = PARENTHESES_CONTENT_PATTERN.search(function_call)
if not match:
stmt = x.strip()
print(f'snippets_translate: Warning "{stmt}" does not match function call',
@@ -416,15 +434,14 @@ def handle_void_functions(x):
# if the arguments are in the same line:
arguments = None
if ")" in x:
- re_content = re.compile(r"\((.*)\)")
- parenthesis = re_content.search(x).group(1)
+ parenthesis = PARENTHESES_CONTENT_PATTERN.search(x).group(1)
arguments = dstrip(parse_arguments(parenthesis))
elif "," in x:
arguments = dstrip(parse_arguments(x.split("(")[-1]))
# check if includes a '{ ... }' after the method signature
after_signature = x.split(")")[-1]
- re_decl = re.compile(r"\{(.*)\}").search(after_signature)
+ re_decl = FUNCTION_BODY_PATTERN.search(after_signature)
extra = ""
if re_decl:
extra = re_decl.group(1)
@@ -460,13 +477,13 @@ def handle_class(x):
bases_name = ""
# Check if the class_name is templated, then remove it
- if re.search(r".*<.*>", class_name):
+ if CLASS_TEMPLATE_PATTERN.search(class_name):
class_name = class_name.split("<")[0]
# Special case: invalid notation for an example:
# class B() {...} -> clas B(): pass
- if re.search(r".*{.*}", class_name):
- class_name = re.sub(r"{.*}", "", class_name).rstrip()
+ if EMPTY_CLASS_PATTERN.search(class_name):
+ class_name = EMPTY_CLASS_REPLACEMENT_PATTERN.sub("", class_name).rstrip()
return f"{class_name}(): pass"
# Special case: check if the line ends in ','
@@ -482,8 +499,7 @@ def handle_class(x):
def handle_array_declarations(x):
- re_varname = re.compile(r"^[a-zA-Z0-9\<\>]+ ([\w\*]+) *\[?\]?")
- content = re_varname.search(x.strip())
+ content = ARRAY_DECLARATION_PATTERN.search(x.strip())
if content:
var_name = content.group(1)
rest_line = "".join(x.split("{")[1:])
@@ -492,13 +508,11 @@ def handle_array_declarations(x):
def handle_methods_return_type(x):
- re_capture = re.compile(r"^ *[a-zA-Z0-9]+ [\w]+::([\w\*\&]+\(.*\)$)")
- capture = re_capture.search(x)
+ capture = RETURN_TYPE_PATTERN.search(x)
if capture:
content = capture.group(1)
method_name = content.split("(")[0]
- re_par = re.compile(r"\((.+)\)")
- par_capture = re_par.search(x)
+ par_capture = PARENTHESES_NONEMPTY_CONTENT_PATTERN.search(x)
arguments = "(self)"
if par_capture:
arguments = f"(self, {par_capture.group(1)})"
@@ -507,16 +521,14 @@ def handle_methods_return_type(x):
def handle_functions(x):
- re_capture = re.compile(r"^ *([a-zA-Z0-9]+) ([\w\*\&]+\(.*\)$)")
- capture = re_capture.search(x)
+ capture = CAPTURE_PATTERN.search(x)
if capture:
return_type = capture.group(1)
if return_type == "return": # "return QModelIndex();"
return x
content = capture.group(2)
function_name = content.split("(")[0]
- re_par = re.compile(r"\((.+)\)")
- par_capture = re_par.search(x)
+ par_capture = PARENTHESES_NONEMPTY_CONTENT_PATTERN.search(x)
arguments = ""
if par_capture:
for arg in par_capture.group(1).split(","):
@@ -529,10 +541,8 @@ def handle_functions(x):
def handle_useless_qt_classes(x):
- _classes = ("QLatin1String", "QLatin1Char")
- for i in _classes:
- re_content = re.compile(fr"{i}\((.*)\)")
- content = re_content.search(x)
+ for c in USELESS_QT_CLASSES_PATTERNS:
+ content = c.search(x)
if content:
x = x.replace(content.group(0), content.group(1))
return x