QStringConverter: use QStaticByteArrayMatcher

The Boyer-Moore tables can be calculated at compile-time, and the
needles are long enough to make skipping worthwhile, even for small
haystacks.

Pick-to: 6.3
Change-Id: I3237812490367ed0491eb8d1667c6da67f38c517
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Marc Mutz 2022-01-21 00:56:05 +01:00
parent f091f371c4
commit e7115b7530
1 changed files with 5 additions and 2 deletions

View File

@ -1809,10 +1809,13 @@ std::optional<QStringConverter::Encoding> QStringConverter::encodingForHtml(QByt
// trust the initial BOM
return encoding;
static constexpr auto metaSearcher = qMakeStaticByteArrayMatcher("meta ");
static constexpr auto charsetSearcher = qMakeStaticByteArrayMatcher("charset=");
QByteArray header = data.first(qMin(data.size(), qsizetype(1024))).toByteArray().toLower();
qsizetype pos = header.indexOf("meta ");
qsizetype pos = metaSearcher.indexIn(header);
if (pos != -1) {
pos = header.indexOf("charset=", pos);
pos = charsetSearcher.indexIn(header, pos);
if (pos != -1) {
pos += int(qstrlen("charset="));
if (pos < header.size() && (header.at(pos) == '\"' || header.at(pos) == '\''))