diff options
author | Topi Reinio <topi.reinio@qt.io> | 2023-04-18 20:23:15 +0000 |
---|---|---|
committer | Topi Reiniƶ <topi.reinio@qt.io> | 2023-04-24 18:39:29 +0000 |
commit | edf1e91454b1354bc3da718b2fe76930f894ff77 (patch) | |
tree | 64f1a329f7bcde0073e9a610be9019eeaa1e4742 /src/qdoc/qdoc/generator.cpp | |
parent | 3d4ada255ad02a102426e4c89a8d976129a00c63 (diff) | |
download | qttools-edf1e91454b1354bc3da718b2fe76930f894ff77.tar.gz |
qdoc: Rename transmogrify() and move it under Utilities namespace
The transmogrify() function was used by the Generator to produce
clean output file names by replacing all non-alphanum characters
with hyphens. This function is useful elsewhere as well,
especially in HTMLGenerator, as it produces valid strings for use
as HTML attribute values.
Rename the function to a more descriptive canonicalizeFileName(),
move it to the Utilities namespace, improve its documentation and
touch up the implementation a bit; make it return a QString
instead of modifying a string passed as an argument.
Pick-to: 6.5
Change-Id: I6088f4603802b86f96c4a5b668668ef229b302f3
Reviewed-by: Luca Di Sera <luca.disera@qt.io>
Diffstat (limited to 'src/qdoc/qdoc/generator.cpp')
-rw-r--r-- | src/qdoc/qdoc/generator.cpp | 49 |
1 files changed, 8 insertions, 41 deletions
diff --git a/src/qdoc/qdoc/generator.cpp b/src/qdoc/qdoc/generator.cpp index c4f6ff233..f5df13087 100644 --- a/src/qdoc/qdoc/generator.cpp +++ b/src/qdoc/qdoc/generator.cpp @@ -240,37 +240,6 @@ void Generator::endSubPage() delete outStreamStack.pop(); } -/* - the code below is effectively equivalent to: - input.replace(QRegularExpression("[^A-Za-z0-9]+"), " "); - input = input.trimmed(); - input.replace(QLatin1Char(' '), QLatin1Char('-')); - input = input.toLower(); - as this function accounted for ~8% of total running time - we optimize a bit... -*/ -static void transmogrify(QString &input, QString &output) -{ - // +5 prevents realloc in fileName() below - output.reserve(input.size() + 5); - bool begun = false; - for (int i = 0; i != input.size(); ++i) { - QChar c = input.at(i); - uint u = c.unicode(); - if (u >= 'A' && u <= 'Z') - u += 'a' - 'A'; - if ((u >= 'a' && u <= 'z') || (u >= '0' && u <= '9')) { - output += QLatin1Char(u); - begun = true; - } else if (begun) { - output += QLatin1Char('-'); - begun = false; - } - } - while (output.endsWith(QLatin1Char('-'))) - output.chop(1); -} - QString Generator::fileBase(const Node *node) const { if (!node->isPageNode() && !node->isCollectionNode()) @@ -337,11 +306,10 @@ QString Generator::fileBase(const Node *node) const } } - QString res; - transmogrify(base, res); + QString canonicalName{Utilities::canonicalizeFileName(base)}; Node *n = const_cast<Node *>(node); - n->setFileNameBase(res); - return res; + n->setFileNameBase(canonicalName); + return canonicalName; } /*! @@ -352,14 +320,13 @@ QString Generator::fileBase(const Node *node) const */ QString Generator::linkForExampleFile(const QString &path, const QString &fileExt) { - QString link = path; + QString link{path}; link.prepend(s_project.toLower() + QLatin1Char('-')); - QString res; - transmogrify(link, res); - res.append(QLatin1Char('.')); - res.append(fileExt.isEmpty() ? fileExtension() : fileExt); - return res; + QString canonicalName{Utilities::canonicalizeFileName(link)}; + canonicalName.append(QLatin1Char('.')); + canonicalName.append(fileExt.isEmpty() ? fileExtension() : fileExt); + return canonicalName; } /*! |