summaryrefslogtreecommitdiffstats
path: root/src/gui/image/qxbmhandler.cpp
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-03-15 18:45:43 +0100
committerLars Knoll <lars.knoll@qt.io>2020-03-25 09:34:32 +0100
commita7fe1af609e905f6e5fbf291ec43261a41d9541f (patch)
tree39f166f255c63876897655ffefd67227904e3afa /src/gui/image/qxbmhandler.cpp
parentde67bca44e91616733fb0434100d7cb46ae1d579 (diff)
Remove QRegExp usage from the XBM handler
Change-Id: Ie7141c2a04c233bc1298195aebfc0437e43df0d1 Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'src/gui/image/qxbmhandler.cpp')
-rw-r--r--src/gui/image/qxbmhandler.cpp51
1 files changed, 28 insertions, 23 deletions
diff --git a/src/gui/image/qxbmhandler.cpp b/src/gui/image/qxbmhandler.cpp
index f06561690c..c422826035 100644
--- a/src/gui/image/qxbmhandler.cpp
+++ b/src/gui/image/qxbmhandler.cpp
@@ -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)