summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-01-17 20:54:52 +0200
committerAhmad Samir <a.samirh78@gmail.com>2023-02-07 20:04:11 +0200
commit498f3452285aa44580e1d03baeec126d475f8401 (patch)
tree6307934b58045f6b6761023f769844d87cdc63ca /src/corelib/text
parent9a8b9473d5f0fd4639193481ba9b344d91f3f00a (diff)
QtMiscUtils: add some more character helpers
isHexDigit, isOctalDigit, isAsciiDigit, isAsciiLower, isAsciiUpper, isAsciiLetterOrNumber. This de-duplicates some code through out. Rename two local lambdas that were called "isAsciiLetterOrNumber" to not conflict with the method in QtMiscUtils. Change-Id: I5b631f95b9f109136d19515f7e20b8e2fbca3d43 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qlocale.cpp13
-rw-r--r--src/corelib/text/qlocale_tools.cpp5
-rw-r--r--src/corelib/text/qstring.cpp42
-rw-r--r--src/corelib/text/qstringconverter.cpp13
4 files changed, 26 insertions, 47 deletions
diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp
index 7392c50d50..0714434d2d 100644
--- a/src/corelib/text/qlocale.cpp
+++ b/src/corelib/text/qlocale.cpp
@@ -22,6 +22,7 @@ QT_WARNING_DISABLE_GCC("-Wfree-nonheap-object") // false positive tracking
#include "qlocale.h"
#include "qlocale_p.h"
#include "qlocale_tools_p.h"
+#include <private/qtools_p.h>
#if QT_CONFIG(datetimeparser)
#include "private/qdatetimeparser_p.h"
#endif
@@ -60,6 +61,7 @@ QT_IMPL_METATYPE_EXTERN_TAGGED(QSystemLocale::CurrencyToStringArgument,
#endif
using namespace Qt::StringLiterals;
+using namespace QtMiscUtils;
#ifndef QT_NO_SYSTEMLOCALE
Q_CONSTINIT static QSystemLocale *_systemLocale = nullptr;
@@ -542,7 +544,7 @@ static bool validTag(QStringView tag)
// Is tag is a non-empty sequence of ASCII letters and/or digits ?
for (QChar uc : tag) {
const char16_t ch = uc.unicode();
- if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')))
+ if (!isAsciiLetterOrNumber(ch))
return false;
}
return tag.size() > 0;
@@ -2603,11 +2605,6 @@ QString QLocale::exponential() const
return d->m_data->exponentSeparator();
}
-static bool qIsUpper(char c)
-{
- return c >= 'A' && c <= 'Z';
-}
-
/*!
\overload
Returns a string representing the floating-point number \a f.
@@ -2645,7 +2642,7 @@ static bool qIsUpper(char c)
QString QLocale::toString(double f, char format, int precision) const
{
QLocaleData::DoubleForm form = QLocaleData::DFDecimal;
- uint flags = qIsUpper(format) ? QLocaleData::CapitalEorX : 0;
+ uint flags = isAsciiUpper(format) ? QLocaleData::CapitalEorX : 0;
switch (QtMiscUtils::toAsciiLower(format)) {
case 'f':
@@ -3983,7 +3980,7 @@ bool QLocaleData::validateChars(QStringView str, NumberMode numMode, QByteArray
const QStringView in = str.mid(i, str.at(i).isHighSurrogate() ? 2 : 1);
char c = numericToCLocale(in);
- if (c >= '0' && c <= '9') {
+ if (isAsciiDigit(c)) {
switch (state) {
case Whole:
// Nothing special to do (unless we want to check grouping sizes).
diff --git a/src/corelib/text/qlocale_tools.cpp b/src/corelib/text/qlocale_tools.cpp
index 53e70ac984..08846e0b89 100644
--- a/src/corelib/text/qlocale_tools.cpp
+++ b/src/corelib/text/qlocale_tools.cpp
@@ -7,6 +7,7 @@
#include "qlocale_p.h"
#include "qstring.h"
+#include <private/qtools_p.h>
#include <private/qnumeric_p.h>
#include <ctype.h>
@@ -37,6 +38,8 @@
QT_BEGIN_NAMESPACE
+using namespace QtMiscUtils;
+
QT_CLOCALE_HOLDER
void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision,
@@ -371,7 +374,7 @@ static auto scanPrefix(const char *p, const char *stop, int base)
const char *next;
int base;
};
- if (p < stop && *p >= '0' && *p <= '9') {
+ if (p < stop && isAsciiDigit(*p)) {
if (*p == '0') {
const char *x_or_b = p + 1;
if (x_or_b < stop) {
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index 12c3186a71..381ae8e6fd 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -75,6 +75,7 @@
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
+using namespace QtMiscUtils;
const char16_t QString::_empty = 0;
@@ -87,16 +88,6 @@ enum StringComparisonMode {
CompareStringsForOrdering
};
-inline bool qIsUpper(char ch)
-{
- return ch >= 'A' && ch <= 'Z';
-}
-
-inline bool qIsDigit(char ch)
-{
- return ch >= '0' && ch <= '9';
-}
-
template <typename Pointer>
char32_t foldCaseHelper(Pointer ch, Pointer start) = delete;
@@ -1361,13 +1352,6 @@ static int ucstrncmp(const char16_t *a, const char *b, size_t l)
return 0;
}
-constexpr int lencmp(qsizetype lhs, qsizetype rhs) noexcept
-{
- return lhs == rhs ? 0 :
- lhs > rhs ? 1 :
- /* else */ -1 ;
-}
-
// Unicode case-sensitive equality
template <typename Char2>
static bool ucstreq(const char16_t *a, size_t alen, const Char2 *b, size_t blen)
@@ -1391,7 +1375,7 @@ static int ucstrcmp(const char16_t *a, size_t alen, const Char2 *b, size_t blen)
}
const size_t l = qMin(alen, blen);
int cmp = ucstrncmp<CompareStringsForOrdering>(a, b, l);
- return cmp ? cmp : lencmp(alen, blen);
+ return cmp ? cmp : qt_lencmp(alen, blen);
}
using CaseInsensitiveL1 = QtPrivate::QCaseInsensitiveLatin1Hash;
@@ -1411,7 +1395,7 @@ static int latin1nicmp(const char *lhsChar, qsizetype lSize, const char *rhsChar
if (int res = CaseInsensitiveL1::difference(lhsChar[i], rhsChar[i]))
return res;
}
- return lencmp(lSize, rSize);
+ return qt_lencmp(lSize, rSize);
}
bool QtPrivate::equalStrings(QStringView lhs, QStringView rhs) noexcept
@@ -1556,12 +1540,12 @@ int QtPrivate::compareStrings(QLatin1StringView lhs, QStringView rhs, Qt::CaseSe
int QtPrivate::compareStrings(QLatin1StringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs) noexcept
{
if (lhs.isEmpty())
- return lencmp(qsizetype(0), rhs.size());
+ return qt_lencmp(qsizetype(0), rhs.size());
if (cs == Qt::CaseInsensitive)
return latin1nicmp(lhs.data(), lhs.size(), rhs.data(), rhs.size());
const auto l = std::min(lhs.size(), rhs.size());
int r = memcmp(lhs.data(), rhs.data(), l);
- return r ? r : lencmp(lhs.size(), rhs.size());
+ return r ? r : qt_lencmp(lhs.size(), rhs.size());
}
/*!
@@ -6850,7 +6834,7 @@ static uint parse_flag_characters(const char * &c) noexcept
static int parse_field_width(const char *&c, qsizetype size)
{
- Q_ASSERT(qIsDigit(*c));
+ Q_ASSERT(isAsciiDigit(*c));
const char *const stop = c + size;
// can't be negative - started with a digit
@@ -6860,7 +6844,7 @@ static int parse_field_width(const char *&c, qsizetype size)
if (used <= 0)
return false;
// preserve Qt 5.5 behavior of consuming all digits, no matter how many
- while (c < stop && qIsDigit(*c))
+ while (c < stop && isAsciiDigit(*c))
++c;
return result < qulonglong(std::numeric_limits<int>::max()) ? int(result) : 0;
}
@@ -6950,7 +6934,7 @@ QString QString::vasprintf(const char *cformat, va_list ap)
// Parse field width
int width = -1; // -1 means unspecified
- if (qIsDigit(*c)) {
+ if (isAsciiDigit(*c)) {
width = parse_field_width(c, formatEnd - c);
} else if (*c == '*') { // can't parse this in another function, not portably, at least
width = va_arg(ap, int);
@@ -6969,7 +6953,7 @@ QString QString::vasprintf(const char *cformat, va_list ap)
if (*c == '.') {
++c;
precision = 0;
- if (qIsDigit(*c)) {
+ if (isAsciiDigit(*c)) {
precision = parse_field_width(c, formatEnd - c);
} else if (*c == '*') { // can't parse this in another function, not portably, at least
precision = va_arg(ap, int);
@@ -7030,7 +7014,7 @@ QString QString::vasprintf(const char *cformat, va_list ap)
default: u = 0; break;
}
- if (qIsUpper(*c))
+ if (isAsciiUpper(*c))
flags |= QLocaleData::CapitalEorX;
int base = 10;
@@ -7061,7 +7045,7 @@ QString QString::vasprintf(const char *cformat, va_list ap)
else
d = va_arg(ap, double);
- if (qIsUpper(*c))
+ if (isAsciiUpper(*c))
flags |= QLocaleData::CapitalEorX;
QLocaleData::DoubleForm form = QLocaleData::DFDecimal;
@@ -7707,7 +7691,7 @@ QString QString::number(double n, char format, int precision)
break;
}
- return qdtoBasicLatin(n, form, precision, qIsUpper(format));
+ return qdtoBasicLatin(n, form, precision, isAsciiUpper(format));
}
namespace {
@@ -8574,7 +8558,7 @@ QString QString::arg(double a, int fieldWidth, char format, int precision, QChar
if (fillChar == u'0')
flags |= QLocaleData::ZeroPadded;
- if (qIsUpper(format))
+ if (isAsciiUpper(format))
flags |= QLocaleData::CapitalEorX;
QLocaleData::DoubleForm form = QLocaleData::DFDecimal;
diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp
index e25a2c7756..97072b427a 100644
--- a/src/corelib/text/qstringconverter.cpp
+++ b/src/corelib/text/qstringconverter.cpp
@@ -31,6 +31,8 @@
QT_BEGIN_NAMESPACE
+using namespace QtMiscUtils;
+
static_assert(std::is_nothrow_move_constructible_v<QStringEncoder>);
static_assert(std::is_nothrow_move_assignable_v<QStringEncoder>);
static_assert(std::is_nothrow_move_constructible_v<QStringDecoder>);
@@ -889,22 +891,15 @@ int QUtf8::compareUtf8(QByteArrayView utf8, QLatin1StringView s, Qt::CaseSensiti
return (end1 > src1) - (end2 > src2);
}
-static inline int lencmp(qsizetype lhs, qsizetype rhs) noexcept
-{
- return lhs == rhs ? 0 :
- lhs > rhs ? 1 :
- /* else */ -1 ;
-}
-
int QUtf8::compareUtf8(QByteArrayView lhs, QByteArrayView rhs, Qt::CaseSensitivity cs) noexcept
{
if (lhs.isEmpty())
- return lencmp(0, rhs.size());
+ return qt_lencmp(0, rhs.size());
if (cs == Qt::CaseSensitive) {
const auto l = std::min(lhs.size(), rhs.size());
int r = memcmp(lhs.data(), rhs.data(), l);
- return r ? r : lencmp(lhs.size(), rhs.size());
+ return r ? r : qt_lencmp(lhs.size(), rhs.size());
}
char32_t uc1 = QChar::Null;