From 7f9398fd4d23469c77c6a53eac0819f92dfc3928 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 4 Feb 2014 16:02:06 -0800 Subject: [PATCH] Add ascii_isspace to replace the locale-dependent isspace(3) Change-Id: Icee42515179e6f3ddefe0692af69e90054449618 Reviewed-by: Lars Knoll --- src/corelib/tools/qbytearray.cpp | 14 +++++++------- src/corelib/tools/qlocale_p.h | 27 +++++++++++++++++++++++++++ src/corelib/tools/qlocale_tools.cpp | 6 +++--- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index d57eeaf188..10fc3dea2d 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -3113,7 +3113,7 @@ QDataStream &operator>>(QDataStream &in, QByteArray &ba) replaced with a single space. Whitespace means any character for which the standard C++ - isspace() function returns \c true. This includes the ASCII + isspace() function returns \c true in the C locale. This includes the ASCII characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '. Example: @@ -3131,9 +3131,9 @@ QByteArray QByteArray::simplified() const int outc=0; char *to = result.d->data(); for (;;) { - while (from!=fromend && isspace(uchar(*from))) + while (from!=fromend && ascii_isspace(uchar(*from))) from++; - while (from!=fromend && !isspace(uchar(*from))) + while (from!=fromend && !ascii_isspace(uchar(*from))) to[outc++] = *from++; if (from!=fromend) to[outc++] = ' '; @@ -3151,7 +3151,7 @@ QByteArray QByteArray::simplified() const and the end. Whitespace means any character for which the standard C++ - isspace() function returns \c true. This includes the ASCII + isspace() function returns \c true in the C locale. This includes the ASCII characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '. Example: @@ -3166,14 +3166,14 @@ QByteArray QByteArray::trimmed() const if (d->size == 0) return *this; const char *s = d->data(); - if (!isspace(uchar(*s)) && !isspace(uchar(s[d->size-1]))) + if (!ascii_isspace(uchar(*s)) && !ascii_isspace(uchar(s[d->size-1]))) return *this; int start = 0; int end = d->size - 1; - while (start<=end && isspace(uchar(s[start]))) // skip white space from start + while (start<=end && ascii_isspace(uchar(s[start]))) // skip white space from start start++; if (start <= end) { // only white space - while (end && isspace(uchar(s[end]))) // skip white space from end + while (end && ascii_isspace(uchar(s[end]))) // skip white space from end end--; } int l = end - start + 1; diff --git a/src/corelib/tools/qlocale_p.h b/src/corelib/tools/qlocale_p.h index 917e83834a..da8a820234 100644 --- a/src/corelib/tools/qlocale_p.h +++ b/src/corelib/tools/qlocale_p.h @@ -402,6 +402,33 @@ QString qt_readEscapedFormatString(const QString &format, int *idx); bool qt_splitLocaleName(const QString &name, QString &lang, QString &script, QString &cntry); int qt_repeatCount(const QString &s, int i); +enum { AsciiSpaceMask = (1 << (' ' - 1)) | + (1 << ('\t' - 1)) | // 9: HT - horizontal tab + (1 << ('\n' - 1)) | // 10: LF - line feed + (1 << ('\v' - 1)) | // 11: VT - vertical tab + (1 << ('\f' - 1)) | // 12: FF - form feed + (1 << ('\r' - 1)) }; // 13: CR - carriage return +Q_DECL_CONSTEXPR inline bool ascii_isspace(uchar c) +{ + return c >= 1U && c <= 32U && (uint(AsciiSpaceMask) >> uint(c - 1)) & 1U; +} + +#if defined(Q_COMPILER_CONSTEXPR) +Q_STATIC_ASSERT(ascii_isspace(' ')); +Q_STATIC_ASSERT(ascii_isspace('\t')); +Q_STATIC_ASSERT(ascii_isspace('\n')); +Q_STATIC_ASSERT(ascii_isspace('\v')); +Q_STATIC_ASSERT(ascii_isspace('\f')); +Q_STATIC_ASSERT(ascii_isspace('\r')); +Q_STATIC_ASSERT(!ascii_isspace('\0')); +Q_STATIC_ASSERT(!ascii_isspace('\a')); +Q_STATIC_ASSERT(!ascii_isspace('a')); +Q_STATIC_ASSERT(!ascii_isspace('\177')); +Q_STATIC_ASSERT(!ascii_isspace('\200')); +Q_STATIC_ASSERT(!ascii_isspace('\xA0')); +Q_STATIC_ASSERT(!ascii_isspace('\377')); +#endif + QT_END_NAMESPACE Q_DECLARE_METATYPE(QStringRef) diff --git a/src/corelib/tools/qlocale_tools.cpp b/src/corelib/tools/qlocale_tools.cpp index c2bae3df9d..f2d85b1985 100644 --- a/src/corelib/tools/qlocale_tools.cpp +++ b/src/corelib/tools/qlocale_tools.cpp @@ -243,7 +243,7 @@ qulonglong qstrtoull(const char *nptr, const char **endptr, int base, bool *ok) s = nptr; do { c = *s++; - } while (isspace(c)); + } while (ascii_isspace(c)); if (c == '-') { if (ok != 0) *ok = false; @@ -323,7 +323,7 @@ qlonglong qstrtoll(const char *nptr, const char **endptr, int base, bool *ok) s = nptr; do { c = *s++; - } while (isspace(c)); + } while (ascii_isspace(c)); if (c == '-') { neg = 1; c = *s++; @@ -1468,7 +1468,7 @@ Q_CORE_EXPORT double qstrtod(const char *s00, const char **se, bool *ok) rv = 0.; - for(s = s00; isspace(uchar(*s)); s++) + for(s = s00; ascii_isspace(uchar(*s)); s++) ; if (*s == '-') {