Utils: Turn "Util.asciify" into a plain macro

This monves the asciify function to stringutils and makes it directly
available as "asciify:" prefix macro, so that the generation of a
default build path does not go through JavaScript.

"Util.asciify" remains available as core JavaScript extension for the
case that it is used by third party code/wizards.

This change also adds a test to tst_stringutils

Change-Id: Iba2f20c0415ee8fe757c2f0058a90629b3fbeff0
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Alessandro Portale
2023-10-24 18:12:09 +02:00
parent 0dbd951654
commit b2e96147cb
6 changed files with 41 additions and 9 deletions

View File

@@ -346,6 +346,18 @@ QString quoteAmpersands(const QString &text)
return result.replace("&", "&&");
}
QString asciify(const QString &input)
{
QString result;
for (const QChar &c : input) {
if (c.isPrint() && c.unicode() < 128)
result.append(c);
else
result.append(QString::fromLatin1("u%1").arg(c.unicode(), 4, 16, QChar('0')));
}
return result;
}
QString formatElapsedTime(qint64 elapsed)
{
elapsed += 500; // round up

View File

@@ -32,6 +32,8 @@ QTCREATOR_UTILS_EXPORT QString commonPrefix(const QStringList &strings);
QTCREATOR_UTILS_EXPORT QString stripAccelerator(const QString &text);
// Quotes all ampersands
QTCREATOR_UTILS_EXPORT QString quoteAmpersands(const QString &text);
// Convert non-ascii characters into foobar
QTCREATOR_UTILS_EXPORT QString asciify(const QString &input);
QTCREATOR_UTILS_EXPORT bool readMultiLineString(const QJsonValue &value, QString *out);

View File

@@ -139,14 +139,7 @@ QString UtilsJsExtension::mktemp(const QString &pattern) const
QString UtilsJsExtension::asciify(const QString &input) const
{
QString result;
for (const QChar &c : input) {
if (c.isPrint() && c.unicode() < 128)
result.append(c);
else
result.append(QString::fromLatin1("u%1").arg(c.unicode(), 4, 16, QChar('0')));
}
return result;
return Utils::asciify(input);
}
QString UtilsJsExtension::qtQuickVersion(const QString &filePath) const

View File

@@ -232,6 +232,9 @@ bool CorePlugin::initialize(const QStringList &arguments, QString *errorMessage)
[] { return QUuid::createUuid().toString(); });
expander->registerPrefix("#:", Tr::tr("A comment."), [](const QString &) { return QString(); });
expander->registerPrefix("asciify:", Tr::tr("Convert string into pure ascii."),
[expander] (const QString &s) {
return asciify(expander->expand(s)); });
Utils::PathChooser::setAboutToShowContextMenuHandler(&CorePlugin::addToPathChooserContextMenu);

View File

@@ -16,7 +16,7 @@ namespace ProjectExplorer {
static QString defaultBuildDirectoryTemplate()
{
return "../%{JS: Util.asciify(\"build-%{Project:Name}-%{Kit:FileSystemName}-%{BuildConfig:Name}\")}";
return "../%{asciify:build-%{Project:Name}-%{Kit:FileSystemName}-%{BuildConfig:Name}}";
}
BuildPropertiesSettings &buildPropertiesSettings()

View File

@@ -81,6 +81,8 @@ private slots:
void testWildcardToRegularExpression();
void testSplitAtFirst_data();
void testSplitAtFirst();
void testAsciify_data();
void testAsciify();
private:
TestMacroExpander mx;
@@ -438,6 +440,26 @@ void tst_StringUtils::testSplitAtFirst()
QCOMPARE(r, right);
}
void tst_StringUtils::testAsciify_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("expected");
QTest::newRow("Basic Latin") << QString("Basic text") << QString("Basic text");
QTest::newRow("Control character") << QString("\x07 text") << QString("u0007 text");
QTest::newRow("Miscellaneous Technical") << QString("\u23F0 text") << QString("u23f0 text");
}
void tst_StringUtils::testAsciify()
{
QFETCH(QString, input);
QFETCH(QString, expected);
const QString asciified = Utils::asciify(input);
QCOMPARE(asciified, expected);
}
QTEST_GUILESS_MAIN(tst_StringUtils)
#include "tst_stringutils.moc"