QString::toHtmlEscaped: avoid unconditional copy

Let's find replaceable chars and then do copy

Change-Id: I2d365626218b0daf2023144dee7e901e8e99adc6
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
bb10
Anton Kudryavtsev 2023-09-15 12:39:34 +03:00
parent bd5d313197
commit 0ed8db7800
1 changed files with 5 additions and 1 deletions

View File

@ -9926,10 +9926,14 @@ qsizetype QtPrivate::count(QStringView haystack, const QRegularExpression &re)
*/
QString QString::toHtmlEscaped() const
{
const auto pos = std::u16string_view(*this).find_first_of(u"<>&\"");
if (pos == std::u16string_view::npos)
return *this;
QString rich;
const qsizetype len = size();
rich.reserve(qsizetype(len * 1.1));
for (QChar ch : *this) {
rich += qToStringViewIgnoringNull(*this).first(pos);
for (auto ch : qToStringViewIgnoringNull(*this).sliced(pos)) {
if (ch == u'<')
rich += "&lt;"_L1;
else if (ch == u'>')