aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-12-13 16:42:17 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-12-14 14:09:36 +0100
commit4315cfb44eaa8abd57c685390c6b9d3aff09ba0f (patch)
tree2bad10449cc23dd4b01d6846c33ca280abac1477
parent4816f9e23b2ddb457fa4f110a391f4c217e628f2 (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. Pick-to: 6.4 6.2 Task-number: PYSIDE-2151 Task-number: PYSIDE-1106 Change-Id: I37589dfafe27d69480db665363d5900d163014da Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-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 1af97ff64..6b9614880 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" *# *(.*)$")
@@ -510,9 +514,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 4cf614d1e..1be9af553 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():