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/utilities.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/utilities.cpp')
-rw-r--r-- | src/qdoc/qdoc/utilities.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/qdoc/qdoc/utilities.cpp b/src/qdoc/qdoc/utilities.cpp index d12820d1f..d0f18338b 100644 --- a/src/qdoc/qdoc/utilities.cpp +++ b/src/qdoc/qdoc/utilities.cpp @@ -79,6 +79,45 @@ QString comma(qsizetype wordPosition, qsizetype numberOfWords) } /*! + \internal + Replace non-alphanum characters in \a str with hyphens + and convert all characters to lowercase. Returns the + result of the conversion with leading, trailing, and + consecutive hyphens removed. + + The implementation is equivalent to: + + \code + name.replace(QRegularExpression("[^A-Za-z0-9]+"), " "); + name = name.simplified(); + name.replace(QLatin1Char(' '), QLatin1Char('-')); + name = name.toLower(); + \endcode +*/ +QString canonicalizeFileName(const QString &name) +{ + QString result; + bool begun = false; + const auto *data{name.constData()}; + for (qsizetype i = 0; i < name.size(); ++i) { + char16_t u{data[i].unicode()}; + if (u >= 'A' && u <= 'Z') + u += 'a' - 'A'; + if ((u >= 'a' && u <= 'z') || (u >= '0' && u <= '9')) { + result += QLatin1Char(u); + begun = true; + } else if (begun) { + result += QLatin1Char('-'); + begun = false; + } + } + if (result.endsWith(QLatin1Char('-'))) + result.chop(1); + + return result; +} + +/*! \internal */ static bool runProcess(const QString &program, const QStringList &arguments, |