aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-12-13 16:42:17 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-12-14 15:08:49 +0000
commit04ebdcb6ec5266d154a7037bb4bea1d7c5ad291e (patch)
tree442f0ad268624148145537e29acd7fac3d31d3c6
parent9dcc16abfbd3bddbf3ab05f2c3125ef7deb2d7f4 (diff)
snippets_translate: Improve handling of string literals
Add QStringLiteral and others and handle multiple occurrences per line better by making the patterns more discriminative. Task-number: PYSIDE-2151 Task-number: PYSIDE-1106 Change-Id: I37589dfafe27d69480db665363d5900d163014da Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> (cherry picked from commit 4315cfb44eaa8abd57c685390c6b9d3aff09ba0f) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--tools/snippets_translate/handlers.py18
-rw-r--r--tools/snippets_translate/tests/test_converter.py5
2 files changed, 18 insertions, 5 deletions
diff --git a/tools/snippets_translate/handlers.py b/tools/snippets_translate/handlers.py
index 57b00e9da..daafb541b 100644
--- a/tools/snippets_translate/handlers.py
+++ b/tools/snippets_translate/handlers.py
@@ -30,8 +30,12 @@ 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\((.*)\)")
+ re.compile(r'QLatin1StringView\(("[^"]*")\)'),
+ re.compile(r'QLatin1String\(("[^"]*")\)'),
+ re.compile(r'QString\.fromLatin1\(("[^"]*")\)'),
+ re.compile(r"QLatin1Char\(('[^']*')\)"),
+ re.compile(r'QStringLiteral\(("[^"]*")\)'),
+ re.compile(r'QString\.fromUtf8\(("[^"]*")\)')
]
COMMENT1_PATTERN = re.compile(r" *# *[\w\ ]+$")
COMMENT2_PATTERN = re.compile(r" *# *(.*)$")
@@ -507,9 +511,13 @@ def handle_functions(x):
def handle_useless_qt_classes(x):
for c in USELESS_QT_CLASSES_PATTERNS:
- content = c.search(x)
- if content:
- x = x.replace(content.group(0), content.group(1))
+ while True:
+ match = c.search(x)
+ if match:
+ x = x[0:match.start()] + match.group(1) + x[match.end():]
+ else:
+ break
+ x = x.replace('"_s', '"') # New string literals
return x
diff --git a/tools/snippets_translate/tests/test_converter.py b/tools/snippets_translate/tests/test_converter.py
index a1f9fe834..be1bddc8a 100644
--- a/tools/snippets_translate/tests/test_converter.py
+++ b/tools/snippets_translate/tests/test_converter.py
@@ -368,7 +368,12 @@ def test_ternary_operator():
def test_useless_qt_classes():
assert st('result += QLatin1String("; ");') == 'result += "; "'
+ assert st('result += QString::fromLatin1("; ");') == 'result += "; "'
+ assert (
+ st('result = QStringLiteral("A") + QStringLiteral("B");')
+ == 'result = "A" + "B"')
assert st("<< QLatin1Char('\0') << endl;") == "print('\0')"
+ assert st('result = "A"_s;') == 'result = "A"'
def test_special_cases():