Remove QRegExp usage from the XBM handler

Change-Id: Ie7141c2a04c233bc1298195aebfc0437e43df0d1
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
Lars Knoll 2020-03-15 18:45:43 +01:00
parent de67bca44e
commit a7fe1af609

View File

@ -44,7 +44,6 @@
#include <qimage.h>
#include <qiodevice.h>
#include <qregexp.h>
#include <qvariant.h>
#include <stdio.h>
@ -67,8 +66,6 @@ static bool read_xbm_header(QIODevice *device, int& w, int& h)
const int buflen = 300;
const int maxlen = 4096;
char buf[buflen + 1];
QRegExp r1(QLatin1String("^#define[ \t]+[a-zA-Z0-9._]+[ \t]+"));
QRegExp r2(QLatin1String("[0-9]+"));
qint64 readBytes = 0;
qint64 totalReadBytes = 0;
@ -90,30 +87,38 @@ static bool read_xbm_header(QIODevice *device, int& w, int& h)
return false;
}
buf[readBytes - 1] = '\0';
QString sbuf;
sbuf = QString::fromLatin1(buf);
auto parseDefine = [] (const char *buf, int len) -> int {
auto isAsciiLetterOrNumber = [] (char ch) -> bool {
return (ch >= '0' && ch <= '9') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= 'a' && ch <= 'z') ||
ch == '_' || ch == '.';
};
auto isAsciiSpace = [] (char ch) -> bool {
return ch == ' ' || ch == '\t';
};
const char define[] = "#define";
constexpr size_t defineLen = sizeof(define) - 1;
if (strncmp(buf, define, defineLen) != 0)
return 0;
int index = defineLen;
while (buf[index] && isAsciiSpace(buf[index]))
++index;
while (buf[index] && isAsciiLetterOrNumber(buf[index]))
++index;
while (buf[index] && isAsciiSpace(buf[index]))
++index;
return QByteArray(buf + index, len - index).toInt();
};
// "#define .._width <num>"
if (r1.indexIn(sbuf) == 0 &&
r2.indexIn(sbuf, r1.matchedLength()) == r1.matchedLength())
w = QByteArray(&buf[r1.matchedLength()]).trimmed().toInt();
else
return false;
w = parseDefine(buf, readBytes - 1);
// "#define .._height <num>"
readBytes = device->readLine(buf, buflen);
if (readBytes <= 0)
return false;
buf[readBytes - 1] = '\0';
sbuf = QString::fromLatin1(buf);
if (r1.indexIn(sbuf) == 0 &&
r2.indexIn(sbuf, r1.matchedLength()) == r1.matchedLength())
h = QByteArray(&buf[r1.matchedLength()]).trimmed().toInt();
else
return false;
// "#define .._height <num>"
h = parseDefine(buf, readBytes - 1);
// format error
if (w <= 0 || w > 32767 || h <= 0 || h > 32767)