Remove QRegExp usage from the XBM handler
Change-Id: Ie7141c2a04c233bc1298195aebfc0437e43df0d1 Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
parent
de67bca44e
commit
a7fe1af609
@ -44,7 +44,6 @@
|
|||||||
|
|
||||||
#include <qimage.h>
|
#include <qimage.h>
|
||||||
#include <qiodevice.h>
|
#include <qiodevice.h>
|
||||||
#include <qregexp.h>
|
|
||||||
#include <qvariant.h>
|
#include <qvariant.h>
|
||||||
|
|
||||||
#include <stdio.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 buflen = 300;
|
||||||
const int maxlen = 4096;
|
const int maxlen = 4096;
|
||||||
char buf[buflen + 1];
|
char buf[buflen + 1];
|
||||||
QRegExp r1(QLatin1String("^#define[ \t]+[a-zA-Z0-9._]+[ \t]+"));
|
|
||||||
QRegExp r2(QLatin1String("[0-9]+"));
|
|
||||||
|
|
||||||
qint64 readBytes = 0;
|
qint64 readBytes = 0;
|
||||||
qint64 totalReadBytes = 0;
|
qint64 totalReadBytes = 0;
|
||||||
@ -90,30 +87,38 @@ static bool read_xbm_header(QIODevice *device, int& w, int& h)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf[readBytes - 1] = '\0';
|
auto parseDefine = [] (const char *buf, int len) -> int {
|
||||||
QString sbuf;
|
auto isAsciiLetterOrNumber = [] (char ch) -> bool {
|
||||||
sbuf = QString::fromLatin1(buf);
|
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>"
|
// "#define .._width <num>"
|
||||||
if (r1.indexIn(sbuf) == 0 &&
|
w = parseDefine(buf, readBytes - 1);
|
||||||
r2.indexIn(sbuf, r1.matchedLength()) == r1.matchedLength())
|
|
||||||
w = QByteArray(&buf[r1.matchedLength()]).trimmed().toInt();
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// "#define .._height <num>"
|
|
||||||
readBytes = device->readLine(buf, buflen);
|
readBytes = device->readLine(buf, buflen);
|
||||||
if (readBytes <= 0)
|
// "#define .._height <num>"
|
||||||
return false;
|
h = parseDefine(buf, readBytes - 1);
|
||||||
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;
|
|
||||||
|
|
||||||
// format error
|
// format error
|
||||||
if (w <= 0 || w > 32767 || h <= 0 || h > 32767)
|
if (w <= 0 || w > 32767 || h <= 0 || h > 32767)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user