summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-02-03 00:18:05 +0200
committerAhmad Samir <a.samirh78@gmail.com>2023-02-07 20:04:11 +0200
commit27db859c6a36738d5074f59584ba7d2b289d1ae4 (patch)
tree4ba87eef451306062bd52d149d675eed96cfad7a
parent498f3452285aa44580e1d03baeec126d475f8401 (diff)
Use QtMiscUtils hex/oct-related helpers
Thanks to Thiago for pointing them out in review. Change-Id: I14d588a8bd5ba29d43a5daeacfd55d974eb65557 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/io/qsettings.cpp14
-rw-r--r--src/corelib/mimetypes/qmimemagicrule.cpp8
-rw-r--r--src/corelib/serialization/qjsonparser.cpp16
-rw-r--r--src/corelib/serialization/qtextstream.cpp9
4 files changed, 18 insertions, 29 deletions
diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp
index f9256e3124..184042593f 100644
--- a/src/corelib/io/qsettings.cpp
+++ b/src/corelib/io/qsettings.cpp
@@ -751,8 +751,8 @@ StNormal:
ch = str.at(i);
if (isHexDigit(ch))
goto StHexEscape;
- } else if (isOctalDigit(ch)) {
- escapeVal = ch - '0';
+ } else if (const int o = fromOct(ch); o != -1) {
+ escapeVal = o;
goto StOctEscape;
} else if (ch == '\n' || ch == '\r') {
if (i < str.size()) {
@@ -814,11 +814,9 @@ StHexEscape:
}
ch = str.at(i);
- if (ch >= 'a')
- ch -= 'a' - 'A';
- if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
+ if (const int h = fromHex(ch); h != -1) {
escapeVal <<= 4;
- escapeVal += QtMiscUtils::fromHex(ch);
+ escapeVal += h;
++i;
goto StHexEscape;
} else {
@@ -833,9 +831,9 @@ StOctEscape:
}
ch = str.at(i);
- if (ch >= '0' && ch <= '7') {
+ if (const int o = fromOct(ch); o != -1) {
escapeVal <<= 3;
- escapeVal += ch - '0';
+ escapeVal += o;
++i;
goto StOctEscape;
} else {
diff --git a/src/corelib/mimetypes/qmimemagicrule.cpp b/src/corelib/mimetypes/qmimemagicrule.cpp
index b6f2c9af20..b0846e375b 100644
--- a/src/corelib/mimetypes/qmimemagicrule.cpp
+++ b/src/corelib/mimetypes/qmimemagicrule.cpp
@@ -148,12 +148,8 @@ static inline QByteArray makePattern(const QByteArray &value)
char c = 0;
for (int i = 0; i < 2 && p + 1 < e; ++i) {
++p;
- if (*p >= '0' && *p <= '9')
- c = (c << 4) + *p - '0';
- else if (*p >= 'a' && *p <= 'f')
- c = (c << 4) + *p - 'a' + 10;
- else if (*p >= 'A' && *p <= 'F')
- c = (c << 4) + *p - 'A' + 10;
+ if (const int h = fromHex(*p); h != -1)
+ c = (c << 4) + h;
else
continue;
}
diff --git a/src/corelib/serialization/qjsonparser.cpp b/src/corelib/serialization/qjsonparser.cpp
index d39d766d75..dba603815f 100644
--- a/src/corelib/serialization/qjsonparser.cpp
+++ b/src/corelib/serialization/qjsonparser.cpp
@@ -780,15 +780,13 @@ bool Parser::parseNumber()
static inline bool addHexDigit(char digit, char32_t *result)
{
*result <<= 4;
- if (digit >= '0' && digit <= '9')
- *result |= (digit - '0');
- else if (digit >= 'a' && digit <= 'f')
- *result |= (digit - 'a') + 10;
- else if (digit >= 'A' && digit <= 'F')
- *result |= (digit - 'A') + 10;
- else
- return false;
- return true;
+ const int h = fromHex(digit);
+ if (h != -1) {
+ *result |= h;
+ return true;
+ }
+
+ return false;
}
static inline bool scanEscapeSequence(const char *&json, const char *end, char32_t *ch)
diff --git a/src/corelib/serialization/qtextstream.cpp b/src/corelib/serialization/qtextstream.cpp
index 806616aaf4..c02effb165 100644
--- a/src/corelib/serialization/qtextstream.cpp
+++ b/src/corelib/serialization/qtextstream.cpp
@@ -1752,13 +1752,10 @@ QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong
// Parse digits
int ndigits = 0;
while (getChar(&dig)) {
- int n = dig.toLower().unicode();
- if (n >= '0' && n <= '9') {
- val <<= 4;
- val += n - '0';
- } else if (n >= 'a' && n <= 'f') {
+ const int h = fromHex(dig.unicode());
+ if (h != -1) {
val <<= 4;
- val += 10 + (n - 'a');
+ val += h;
} else {
ungetChar(dig);
break;