diff --git a/dependencies.yaml b/dependencies.yaml index 6140b93..b4d2112 100644 --- a/dependencies.yaml +++ b/dependencies.yaml @@ -1,4 +1,4 @@ dependencies: ../qtbase: - ref: eb7d1cf098df56f8ebf62f02af611a627435a4a1 + ref: 4b1ffab8ad95eda737fde015c243442a28de8e23 required: true diff --git a/src/core5/CMakeLists.txt b/src/core5/CMakeLists.txt index 8aa48ae..d9c51e8 100644 --- a/src/core5/CMakeLists.txt +++ b/src/core5/CMakeLists.txt @@ -15,6 +15,10 @@ qt_add_module(Core5Compat serialization/qbinaryjsonarray.cpp serialization/qbinaryjsonarray_p.h serialization/qbinaryjsonobject.cpp serialization/qbinaryjsonobject_p.h serialization/qbinaryjsonvalue.cpp serialization/qbinaryjsonvalue_p.h + text/qstringref.cpp text/qstringref.h + DEFINES + QT_USE_FAST_OPERATOR_PLUS + QT_USE_QSTRINGBUILDER LIBRARIES Qt::CorePrivate PUBLIC_LIBRARIES diff --git a/src/core5/core5.pro b/src/core5/core5.pro index 4982511..5d52deb 100644 --- a/src/core5/core5.pro +++ b/src/core5/core5.pro @@ -9,6 +9,7 @@ QMAKE_DOCS = $$PWD/doc/qtcore5.qdocconf include(codecs/codecs.pri) include(serialization/serialization.pri) +include(text/text.pri) PUBLIC_HEADERS += \ qcore5global.h \ diff --git a/src/core5/text/qstringref.cpp b/src/core5/text/qstringref.cpp new file mode 100644 index 0000000..0e587d6 --- /dev/null +++ b/src/core5/text/qstringref.cpp @@ -0,0 +1,2090 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2018 Intel Corporation. +** Copyright (C) 2019 Mail.ru Group. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qstringref.h" + +#include +#include +#include + +#ifdef Q_OS_MACOS +#include +#endif + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#ifdef Q_OS_WIN +# include +#endif + +#ifdef truncate +# undef truncate +#endif + +QT_BEGIN_NAMESPACE + +// internal +static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept; + +static inline bool qt_starts_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); +static inline bool qt_starts_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs); +static inline bool qt_starts_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); +static inline bool qt_ends_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); +static inline bool qt_ends_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs); +static inline bool qt_ends_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); + +// Code imported from QUnicodeTables + +namespace QUnicodeTablesPrivate +{ + static const unsigned short specialCaseMap[] = { + 0x0, // placeholder + 0x1, 0x2c65, 0x1, 0x2c66, 0x1, 0x2c7e, 0x1, 0x2c7f, 0x1, 0x2c6f, 0x1, 0x2c6d, + 0x1, 0x2c70, 0x1, 0xa7ab, 0x1, 0xa7ac, 0x1, 0xa78d, 0x1, 0xa7aa, 0x1, 0xa7ae, + 0x1, 0x2c62, 0x1, 0xa7ad, 0x1, 0x2c6e, 0x1, 0x2c64, 0x1, 0xa7c5, 0x1, 0xa7b1, + 0x1, 0xa7b2, 0x1, 0xa7b0, 0x1, 0xab70, 0x1, 0xab71, 0x1, 0xab72, 0x1, 0xab73, + 0x1, 0xab74, 0x1, 0xab75, 0x1, 0xab76, 0x1, 0xab77, 0x1, 0xab78, 0x1, 0xab79, + 0x1, 0xab7a, 0x1, 0xab7b, 0x1, 0xab7c, 0x1, 0xab7d, 0x1, 0xab7e, 0x1, 0xab7f, + 0x1, 0xab80, 0x1, 0xab81, 0x1, 0xab82, 0x1, 0xab83, 0x1, 0xab84, 0x1, 0xab85, + 0x1, 0xab86, 0x1, 0xab87, 0x1, 0xab88, 0x1, 0xab89, 0x1, 0xab8a, 0x1, 0xab8b, + 0x1, 0xab8c, 0x1, 0xab8d, 0x1, 0xab8e, 0x1, 0xab8f, 0x1, 0xab90, 0x1, 0xab91, + 0x1, 0xab92, 0x1, 0xab93, 0x1, 0xab94, 0x1, 0xab95, 0x1, 0xab96, 0x1, 0xab97, + 0x1, 0xab98, 0x1, 0xab99, 0x1, 0xab9a, 0x1, 0xab9b, 0x1, 0xab9c, 0x1, 0xab9d, + 0x1, 0xab9e, 0x1, 0xab9f, 0x1, 0xaba0, 0x1, 0xaba1, 0x1, 0xaba2, 0x1, 0xaba3, + 0x1, 0xaba4, 0x1, 0xaba5, 0x1, 0xaba6, 0x1, 0xaba7, 0x1, 0xaba8, 0x1, 0xaba9, + 0x1, 0xabaa, 0x1, 0xabab, 0x1, 0xabac, 0x1, 0xabad, 0x1, 0xabae, 0x1, 0xabaf, + 0x1, 0xabb0, 0x1, 0xabb1, 0x1, 0xabb2, 0x1, 0xabb3, 0x1, 0xabb4, 0x1, 0xabb5, + 0x1, 0xabb6, 0x1, 0xabb7, 0x1, 0xabb8, 0x1, 0xabb9, 0x1, 0xabba, 0x1, 0xabbb, + 0x1, 0xabbc, 0x1, 0xabbd, 0x1, 0xabbe, 0x1, 0xabbf, 0x1, 0xa64a, 0x1, 0xa77d, + 0x1, 0xa7c6, 0x1, 0x6b, 0x1, 0xe5, 0x1, 0x26b, 0x1, 0x27d, 0x1, 0x23a, + 0x1, 0x23e, 0x1, 0x251, 0x1, 0x271, 0x1, 0x250, 0x1, 0x252, 0x1, 0x23f, + 0x1, 0x240, 0x1, 0x1d79, 0x1, 0x265, 0x1, 0x266, 0x1, 0x25c, 0x1, 0x261, + 0x1, 0x26c, 0x1, 0x26a, 0x1, 0x29e, 0x1, 0x287, 0x1, 0x29d, 0x1, 0x282, + 0x1, 0x1d8e, 0x1, 0x13a0, 0x1, 0x13a1, 0x1, 0x13a2, 0x1, 0x13a3, 0x1, 0x13a4, + 0x1, 0x13a5, 0x1, 0x13a6, 0x1, 0x13a7, 0x1, 0x13a8, 0x1, 0x13a9, 0x1, 0x13aa, + 0x1, 0x13ab, 0x1, 0x13ac, 0x1, 0x13ad, 0x1, 0x13ae, 0x1, 0x13af, 0x1, 0x13b0, + 0x1, 0x13b1, 0x1, 0x13b2, 0x1, 0x13b3, 0x1, 0x13b4, 0x1, 0x13b5, 0x1, 0x13b6, + 0x1, 0x13b7, 0x1, 0x13b8, 0x1, 0x13b9, 0x1, 0x13ba, 0x1, 0x13bb, 0x1, 0x13bc, + 0x1, 0x13bd, 0x1, 0x13be, 0x1, 0x13bf, 0x1, 0x13c0, 0x1, 0x13c1, 0x1, 0x13c2, + 0x1, 0x13c3, 0x1, 0x13c4, 0x1, 0x13c5, 0x1, 0x13c6, 0x1, 0x13c7, 0x1, 0x13c8, + 0x1, 0x13c9, 0x1, 0x13ca, 0x1, 0x13cb, 0x1, 0x13cc, 0x1, 0x13cd, 0x1, 0x13ce, + 0x1, 0x13cf, 0x1, 0x13d0, 0x1, 0x13d1, 0x1, 0x13d2, 0x1, 0x13d3, 0x1, 0x13d4, + 0x1, 0x13d5, 0x1, 0x13d6, 0x1, 0x13d7, 0x1, 0x13d8, 0x1, 0x13d9, 0x1, 0x13da, + 0x1, 0x13db, 0x1, 0x13dc, 0x1, 0x13dd, 0x1, 0x13de, 0x1, 0x13df, 0x1, 0x13e0, + 0x1, 0x13e1, 0x1, 0x13e2, 0x1, 0x13e3, 0x1, 0x13e4, 0x1, 0x13e5, 0x1, 0x13e6, + 0x1, 0x13e7, 0x1, 0x13e8, 0x1, 0x13e9, 0x1, 0x13ea, 0x1, 0x13eb, 0x1, 0x13ec, + 0x1, 0x13ed, 0x1, 0x13ee, 0x1, 0x13ef, 0x2, 0x53, 0x73, 0x2, 0x53, 0x53, + 0x2, 0x69, 0x307, 0x2, 0x46, 0x66, 0x2, 0x46, 0x46, 0x2, 0x46, 0x69, + 0x2, 0x46, 0x49, 0x2, 0x46, 0x6c, 0x2, 0x46, 0x4c, 0x3, 0x46, 0x66, + 0x69, 0x3, 0x46, 0x46, 0x49, 0x3, 0x46, 0x66, 0x6c, 0x3, 0x46, 0x46, + 0x4c, 0x2, 0x53, 0x74, 0x2, 0x53, 0x54, 0x2, 0x535, 0x582, 0x2, 0x535, + 0x552, 0x2, 0x544, 0x576, 0x2, 0x544, 0x546, 0x2, 0x544, 0x565, 0x2, 0x544, + 0x535, 0x2, 0x544, 0x56b, 0x2, 0x544, 0x53b, 0x2, 0x54e, 0x576, 0x2, 0x54e, + 0x546, 0x2, 0x544, 0x56d, 0x2, 0x544, 0x53d, 0x2, 0x2bc, 0x4e, 0x3, 0x399, + 0x308, 0x301, 0x3, 0x3a5, 0x308, 0x301, 0x2, 0x4a, 0x30c, 0x2, 0x48, 0x331, + 0x2, 0x54, 0x308, 0x2, 0x57, 0x30a, 0x2, 0x59, 0x30a, 0x2, 0x41, 0x2be, + 0x2, 0x3a5, 0x313, 0x3, 0x3a5, 0x313, 0x300, 0x3, 0x3a5, 0x313, 0x301, 0x3, + 0x3a5, 0x313, 0x342, 0x2, 0x391, 0x342, 0x2, 0x397, 0x342, 0x3, 0x399, 0x308, + 0x300, 0x2, 0x399, 0x342, 0x3, 0x399, 0x308, 0x342, 0x3, 0x3a5, 0x308, 0x300, + 0x2, 0x3a1, 0x313, 0x2, 0x3a5, 0x342, 0x3, 0x3a5, 0x308, 0x342, 0x2, 0x3a9, + 0x342, 0x2, 0x1f08, 0x399, 0x2, 0x1f09, 0x399, 0x2, 0x1f0a, 0x399, 0x2, 0x1f0b, + 0x399, 0x2, 0x1f0c, 0x399, 0x2, 0x1f0d, 0x399, 0x2, 0x1f0e, 0x399, 0x2, 0x1f0f, + 0x399, 0x2, 0x1f28, 0x399, 0x2, 0x1f29, 0x399, 0x2, 0x1f2a, 0x399, 0x2, 0x1f2b, + 0x399, 0x2, 0x1f2c, 0x399, 0x2, 0x1f2d, 0x399, 0x2, 0x1f2e, 0x399, 0x2, 0x1f2f, + 0x399, 0x2, 0x1f68, 0x399, 0x2, 0x1f69, 0x399, 0x2, 0x1f6a, 0x399, 0x2, 0x1f6b, + 0x399, 0x2, 0x1f6c, 0x399, 0x2, 0x1f6d, 0x399, 0x2, 0x1f6e, 0x399, 0x2, 0x1f6f, + 0x399, 0x2, 0x391, 0x399, 0x2, 0x397, 0x399, 0x2, 0x3a9, 0x399, 0x2, 0x1fba, + 0x345, 0x2, 0x1fba, 0x399, 0x2, 0x386, 0x345, 0x2, 0x386, 0x399, 0x2, 0x1fca, + 0x345, 0x2, 0x1fca, 0x399, 0x2, 0x389, 0x345, 0x2, 0x389, 0x399, 0x2, 0x1ffa, + 0x345, 0x2, 0x1ffa, 0x399, 0x2, 0x38f, 0x345, 0x2, 0x38f, 0x399, 0x3, 0x391, + 0x342, 0x345, 0x3, 0x391, 0x342, 0x399, 0x3, 0x397, 0x342, 0x345, 0x3, 0x397, + 0x342, 0x399, 0x3, 0x3a9, 0x342, 0x345, 0x3, 0x3a9, 0x342, 0x399, 0x1, 0xa64b + }; +} + +// Code imported from QChar + +namespace QCharPrivate +{ + template + Q_DECL_CONST_FUNCTION static inline T convertCase_helper(T uc, QUnicodeTables::Case which) noexcept + { + const auto fold = QUnicodeTables::properties(uc)->cases[which]; + + if (Q_UNLIKELY(fold.special)) { + const ushort *specialCase = QUnicodeTablesPrivate::specialCaseMap + fold.diff; + // so far, there are no special cases beyond BMP (guaranteed by the qunicodetables + // generator) + return *specialCase == 1 ? specialCase[1] : uc; + } + + return uc + fold.diff; + } + + static inline char32_t foldCase(char32_t ch, char32_t &last) noexcept + { + char32_t ucs4 = ch; + if (QChar::isLowSurrogate(ucs4) && QChar::isHighSurrogate(last)) + ucs4 = QChar::surrogateToUcs4(last, ucs4); + last = ch; + return convertCase_helper(ucs4, QUnicodeTables::CaseFold); + } + + static inline char16_t foldCase(char16_t ch) noexcept + { + return convertCase_helper(ch, QUnicodeTables::CaseFold); + } + + static inline QChar foldCase(QChar ch) noexcept + { + return QChar(foldCase(ch.unicode())); + } +} + +static int qt_compare_strings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept +{ + return lhs.compare(rhs, cs); +} + +static int qt_compare_strings(QStringView lhs, QLatin1String rhs, Qt::CaseSensitivity cs) noexcept +{ + return lhs.compare(rhs, cs); +} + +static QByteArray qt_convert_to_local_8bit(QStringView string) +{ + if (string.isNull()) + return QByteArray(); + QStringEncoder fromUtf16(QStringEncoder::System, QStringEncoder::Flag::Stateless); + return fromUtf16(string); +} + +static QByteArray qt_convert_to_utf8(QStringView str) +{ + if (str.isNull()) + return QByteArray(); + + return QUtf8::convertFromUnicode(str.data(), str.length()); +} + +static QList qt_convert_to_ucs4(QStringView string) +{ + QList v(string.length()); + uint *a = const_cast(v.constData()); + QStringIterator it(string); + while (it.hasNext()) + *a++ = it.next(); + v.resize(a - v.constData()); + return v; +} + +/*! + \internal + \since 4.5 +*/ +int QStringRef::compare_helper(const QChar *data1, qsizetype length1, const QChar *data2, qsizetype length2, + Qt::CaseSensitivity cs) noexcept +{ + Q_ASSERT(length1 >= 0); + Q_ASSERT(length2 >= 0); + Q_ASSERT(data1 || length1 == 0); + Q_ASSERT(data2 || length2 == 0); + return qt_compare_strings(QStringView(data1, length1), QStringView(data2, length2), cs); +} + +/*! + \internal + \since 5.0 +*/ +int QStringRef::compare_helper(const QChar *data1, qsizetype length1, const char *data2, qsizetype length2, + Qt::CaseSensitivity cs) +{ + Q_ASSERT(length1 >= 0); + Q_ASSERT(data1 || length1 == 0); + if (!data2) + return length1; + if (Q_UNLIKELY(length2 < 0)) + length2 = qsizetype(strlen(data2)); + // ### make me nothrow in all cases + QVarLengthArray s2(length2); + const auto beg = reinterpret_cast(s2.data()); + const auto end = QUtf8::convertToUnicode(beg, data2, length2); + return qt_compare_strings(QStringView(data1, length1), QStringView(beg, end - beg), cs); +} + +/*! + \internal + \since 4.5 +*/ +int QStringRef::compare_helper(const QChar *data1, qsizetype length1, QLatin1String s2, + Qt::CaseSensitivity cs) noexcept +{ + Q_ASSERT(length1 >= 0); + Q_ASSERT(data1 || length1 == 0); + return qt_compare_strings(QStringView(data1, length1), s2, cs); +} + +namespace { +template +static ResultList splitString(const StringSource &source, QStringView sep, + Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) +{ + ResultList list; + typename StringSource::size_type start = 0; + typename StringSource::size_type end; + typename StringSource::size_type extra = 0; + while ((end = QtPrivate::findString(QStringView(source.constData(), source.size()), start + extra, sep, cs)) != -1) { + if (start != end || behavior == Qt::KeepEmptyParts) + list.append(source.mid(start, end - start)); + start = end + sep.size(); + extra = (sep.size() == 0 ? 1 : 0); + } + if (start != source.size() || behavior == Qt::KeepEmptyParts) + list.append(source.mid(start)); + return list; +} + +} // namespace + +/*! + Splits the string into substrings references wherever \a sep occurs, and + returns the list of those strings. + + See QString::split() for how \a sep, \a behavior and \a cs interact to form + the result. + + \note All references are valid as long this string is alive. Destroying this + string will cause all references to be dangling pointers. + + \since 5.14 +*/ +QList QStringRef::split(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + return splitString>(*this, sep, behavior, cs); +} + +/*! + \overload + \since 5.14 +*/ +QList QStringRef::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + return splitString>(*this, QStringView(&sep, 1), behavior, cs); +} + +/*! + \class QStringRef + \inmodule QtCore5Compat + \since 4.3 + \brief The QStringRef class provides a thin wrapper around QString substrings. + \reentrant + \ingroup tools + \ingroup string-processing + + QStringRef provides a read-only subset of the QString API. + + A string reference explicitly references a portion of a string() + with a given size(), starting at a specific position(). Calling + toString() returns a copy of the data as a real QString instance. + + This class is designed to improve the performance of substring + handling when manipulating substrings obtained from existing QString + instances. QStringRef avoids the memory allocation and reference + counting overhead of a standard QString by simply referencing a + part of the original string. This can prove to be advantageous in + low level code, such as that used in a parser, at the expense of + potentially more complex code. + + For most users, there are no semantic benefits to using QStringRef + instead of QString since QStringRef requires attention to be paid + to memory management issues, potentially making code more complex + to write and maintain. + + \warning A QStringRef is only valid as long as the referenced + string exists. If the original string is deleted, the string + reference points to an invalid memory location. + + We suggest that you only use this class in stable code where profiling + has clearly identified that performance improvements can be made by + replacing standard string operations with the optimized substring + handling provided by this class. + + \sa {Implicitly Shared Classes} +*/ + +/*! + \typedef QStringRef::size_type + \internal +*/ + +/*! + \typedef QStringRef::value_type + \internal +*/ + +/*! + \typedef QStringRef::const_pointer + \internal +*/ + +/*! + \typedef QStringRef::const_reference + \internal +*/ + +/*! + \typedef QStringRef::const_iterator + \since 5.4 + + \sa QStringRef::const_reverse_iterator +*/ + +/*! + \typedef QStringRef::const_reverse_iterator + \since 5.7 + + \sa QStringRef::const_iterator +*/ + +/*! + \fn QStringRef::QStringRef() + + Constructs an empty string reference. +*/ + +/*! \fn QStringRef::QStringRef(const QString *string, int position, int length) + +Constructs a string reference to the range of characters in the given +\a string specified by the starting \a position and \a length in characters. + +\warning This function exists to improve performance as much as possible, +and performs no bounds checking. For program correctness, \a position and +\a length must describe a valid substring of \a string. + +This means that the starting \a position must be positive or 0 and smaller +than \a string's length, and \a length must be positive or 0 but smaller than +the string's length minus the starting \a position; +i.e, 0 <= position < string->length() and +0 <= length <= string->length() - position must both be satisfied. +*/ + +/*! \fn QStringRef::QStringRef(const QString *string) + +Constructs a string reference to the given \a string. +*/ + +/*! \fn QStringRef::QStringRef(const QStringRef &other) + +Constructs a copy of the \a other string reference. + */ +/*! +\fn QStringRef::~QStringRef() + +Destroys the string reference. + +Since this class is only used to refer to string data, and does not take +ownership of it, no memory is freed when instances are destroyed. +*/ + +/*! + \fn int QStringRef::position() const + + Returns the starting position in the referenced string that is referred to + by the string reference. + + \sa size(), string() +*/ + +/*! + \fn int QStringRef::size() const + + Returns the number of characters referred to by the string reference. + Equivalent to length() and count(). + + \sa position(), string() +*/ +/*! + \fn int QStringRef::count() const + Returns the number of characters referred to by the string reference. + Equivalent to size() and length(). + + \sa position(), string() +*/ +/*! + \fn int QStringRef::length() const + Returns the number of characters referred to by the string reference. + Equivalent to size() and count(). + + \sa position(), string() +*/ + + +/*! + \fn bool QStringRef::isEmpty() const + + Returns \c true if the string reference has no characters; otherwise returns + \c false. + + A string reference is empty if its size is zero. + + \sa size() +*/ + +/*! + \fn bool QStringRef::isNull() const + + Returns \c true if this string reference does not reference a string or if + the string it references is null (i.e. QString::isNull() is true). + + \sa size() +*/ + +/*! + \fn const QString *QStringRef::string() const + + Returns a pointer to the string referred to by the string reference, or + 0 if it does not reference a string. + + \sa unicode() +*/ + + +/*! + \fn const QChar *QStringRef::unicode() const + + Returns a Unicode representation of the string reference. Since + the data stems directly from the referenced string, it is not + \\0'-terminated unless the string reference includes the string's + null terminator. + + \sa string() +*/ + +/*! + \fn const QChar *QStringRef::data() const + + Same as unicode(). +*/ + +/*! + \fn const QChar *QStringRef::constData() const + + Same as unicode(). +*/ + +/*! + \fn QStringRef::const_iterator QStringRef::begin() const + \since 5.4 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in + the string. + + \sa cbegin(), constBegin(), end(), constEnd(), rbegin(), rend() +*/ + +/*! + \fn QStringRef::const_iterator QStringRef::cbegin() const + \since 5.4 + + Same as begin(). + + \sa begin(), constBegin(), cend(), constEnd(), rbegin(), rend() +*/ + +/*! + \fn QStringRef::const_iterator QStringRef::constBegin() const + \since 5.9 + + Same as begin(). + + \sa begin(), cend(), constEnd(), rbegin(), rend() +*/ + +/*! + \fn QStringRef::const_iterator QStringRef::end() const + \since 5.4 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary + character after the last character in the list. + + \sa cbegin(), constBegin(), end(), constEnd(), rbegin(), rend() +*/ + +/*! \fn QStringRef::const_iterator QStringRef::cend() const + \since 5.4 + + Same as end(). + + \sa end(), constEnd(), cbegin(), constBegin(), rbegin(), rend() +*/ + +/*! \fn QStringRef::const_iterator QStringRef::constEnd() const + \since 5.9 + + Same as end(). + + \sa end(), cend(), cbegin(), constBegin(), rbegin(), rend() +*/ + +/*! + \fn QStringRef::const_reverse_iterator QStringRef::rbegin() const + \since 5.7 + + Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first + character in the string, in reverse order. + + \sa begin(), crbegin(), rend() +*/ + +/*! + \fn QStringRef::const_reverse_iterator QStringRef::crbegin() const + \since 5.7 + + Same as rbegin(). + + \sa begin(), rbegin(), rend() +*/ + +/*! + \fn QStringRef::const_reverse_iterator QStringRef::rend() const + \since 5.7 + + Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past + the last character in the string, in reverse order. + + \sa end(), crend(), rbegin() +*/ + + +/*! + \fn QStringRef::const_reverse_iterator QStringRef::crend() const + \since 5.7 + + Same as rend(). + + \sa end(), rend(), rbegin() +*/ + +/*! + Returns a copy of the string reference as a QString object. + + If the string reference is not a complete reference of the string + (meaning that position() is 0 and size() equals string()->size()), + this function will allocate a new string to return. + + \sa string() +*/ + +QString QStringRef::toString() const { + if (isNull()) + return QString(); + if (m_size && m_position == 0 && m_size == m_string->size()) + return *m_string; + return QString(m_string->unicode() + m_position, m_size); +} + + +/*! \relates QStringRef + + Returns \c true if string reference \a s1 is lexically equal to string reference \a s2; otherwise + returns \c false. +*/ +bool operator==(const QStringRef &s1,const QStringRef &s2) noexcept +{ + return s1.size() == s2.size() && qt_compare_strings(s1, s2, Qt::CaseSensitive) == 0; +} + +/*! \relates QStringRef + + Returns \c true if string \a s1 is lexically equal to string reference \a s2; otherwise + returns \c false. +*/ +bool operator==(const QString &s1,const QStringRef &s2) noexcept +{ + return s1.size() == s2.size() && qt_compare_strings(s1, s2, Qt::CaseSensitive) == 0; +} + +/*! \relates QStringRef + + Returns \c true if string \a s1 is lexically equal to string reference \a s2; otherwise + returns \c false. +*/ +bool operator==(QLatin1String s1, const QStringRef &s2) noexcept +{ + if (s1.size() != s2.size()) + return false; + + return qt_compare_strings(s2, s1, Qt::CaseSensitive) == 0; +} + +/*! + \relates QStringRef + + Returns \c true if string reference \a s1 is lexically less than + string reference \a s2; otherwise returns \c false. + + \sa {Comparing Strings} +*/ +bool operator<(const QStringRef &s1,const QStringRef &s2) noexcept +{ + return qt_compare_strings(s1, s2, Qt::CaseSensitive) < 0; +} + +/*!\fn bool operator<=(const QStringRef &s1,const QStringRef &s2) + + \relates QStringRef + + Returns \c true if string reference \a s1 is lexically less than + or equal to string reference \a s2; otherwise returns \c false. + + \sa {Comparing Strings} +*/ + +/*!\fn bool operator>=(const QStringRef &s1,const QStringRef &s2) + + \relates QStringRef + + Returns \c true if string reference \a s1 is lexically greater than + or equal to string reference \a s2; otherwise returns \c false. + + \sa {Comparing Strings} +*/ + +/*!\fn bool operator>(const QStringRef &s1,const QStringRef &s2) + + \relates QStringRef + + Returns \c true if string reference \a s1 is lexically greater than + string reference \a s2; otherwise returns \c false. + + \sa {Comparing Strings} +*/ + + +/*! + \fn const QChar QStringRef::at(int position) const + + Returns the character at the given index \a position in the + string reference. + + The \a position must be a valid index position in the string + (i.e., 0 <= \a position < size()). +*/ + +/*! + \fn QChar QStringRef::operator[](int position) const + \since 5.7 + + Returns the character at the given index \a position in the + string reference. + + The \a position must be a valid index position in the string + reference (i.e., 0 <= \a position < size()). + + \sa at() +*/ + +/*! + \fn QChar QStringRef::front() const + \since 5.10 + + Returns the first character in the string. + Same as \c{at(0)}. + + This function is provided for STL compatibility. + + \warning Calling this function on an empty string constitutes + undefined behavior. + + \sa back(), at(), operator[]() +*/ + +/*! + \fn QChar QStringRef::back() const + \since 5.10 + + Returns the last character in the string. + Same as \c{at(size() - 1)}. + + This function is provided for STL compatibility. + + \warning Calling this function on an empty string constitutes + undefined behavior. + + \sa front(), at(), operator[]() +*/ + +/*! + \fn void QStringRef::clear() + + Clears the contents of the string reference by making it null and empty. + + \sa isEmpty(), isNull() +*/ + +/*! + \fn QStringRef &QStringRef::operator=(const QStringRef &other) + + Assigns the \a other string reference to this string reference, and + returns the result. +*/ + +/*! + \fn QStringRef &QStringRef::operator=(const QString *string) + + Constructs a string reference to the given \a string and assigns it to + this string reference, returning the result. +*/ + +/*! + \fn bool QStringRef::operator==(const char * s) const + + \overload operator==() + + The \a s byte array is converted to a QStringRef using the + fromUtf8() function. This function stops conversion at the + first NUL character found, or the end of the byte array. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. This + can be useful if you want to ensure that all user-visible strings + go through QObject::tr(), for example. + + Returns \c true if this string is lexically equal to the parameter + string \a s. Otherwise returns \c false. + + \sa QT_NO_CAST_FROM_ASCII +*/ + +/*! + \fn bool QStringRef::operator!=(const char * s) const + + \overload operator!=() + + The \a s const char pointer is converted to a QStringRef using + the fromUtf8() function. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. This + can be useful if you want to ensure that all user-visible strings + go through QObject::tr(), for example. + + Returns \c true if this string is not lexically equal to the parameter + string \a s. Otherwise returns \c false. + + \sa QT_NO_CAST_FROM_ASCII +*/ + +/*! + \fn bool QStringRef::operator<(const char * s) const + + \overload operator<() + + The \a s const char pointer is converted to a QStringRef using + the fromUtf8() function. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. This + can be useful if you want to ensure that all user-visible strings + go through QObject::tr(), for example. + + Returns \c true if this string is lexically smaller than the parameter + string \a s. Otherwise returns \c false. + + \sa QT_NO_CAST_FROM_ASCII +*/ + +/*! + \fn bool QStringRef::operator<=(const char * s) const + + \overload operator<=() + + The \a s const char pointer is converted to a QStringRef using + the fromUtf8() function. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. This + can be useful if you want to ensure that all user-visible strings + go through QObject::tr(), for example. + + Returns \c true if this string is lexically smaller than or equal to the parameter + string \a s. Otherwise returns \c false. + + \sa QT_NO_CAST_FROM_ASCII +*/ + +/*! + \fn bool QStringRef::operator>(const char * s) const + + + \overload operator>() + + The \a s const char pointer is converted to a QStringRef using + the fromUtf8() function. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. This + can be useful if you want to ensure that all user-visible strings + go through QObject::tr(), for example. + + Returns \c true if this string is lexically greater than the parameter + string \a s. Otherwise returns \c false. + + \sa QT_NO_CAST_FROM_ASCII +*/ + +/*! + \fn bool QStringRef::operator>= (const char * s) const + + \overload operator>=() + + The \a s const char pointer is converted to a QStringRef using + the fromUtf8() function. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. This + can be useful if you want to ensure that all user-visible strings + go through QObject::tr(), for example. + + Returns \c true if this string is lexically greater than or equal to the + parameter string \a s. Otherwise returns \c false. + + \sa QT_NO_CAST_FROM_ASCII +*/ + +/*! Appends the string reference to \a string, and returns a new +reference to the combined string data. + */ +QStringRef QStringRef::appendTo(QString *string) const +{ + if (!string) + return QStringRef(); + int pos = string->size(); + string->insert(pos, unicode(), size()); + return QStringRef(string, pos, size()); +} + +/*! + \fn int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) + \since 4.5 + + Compares the string \a s1 with the string \a s2 and returns an + integer less than, equal to, or greater than zero if \a s1 + is less than, equal to, or greater than \a s2. + + If \a cs is Qt::CaseSensitive, the comparison is case sensitive; + otherwise the comparison is case insensitive. +*/ + +/*! + \fn int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) + \since 4.5 + \overload + + Compares the string \a s1 with the string \a s2 and returns an + integer less than, equal to, or greater than zero if \a s1 + is less than, equal to, or greater than \a s2. + + If \a cs is Qt::CaseSensitive, the comparison is case sensitive; + otherwise the comparison is case insensitive. +*/ + +/*! + \fn int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) + \since 4.5 + \overload + + Compares the string \a s1 with the string \a s2 and returns an + integer less than, equal to, or greater than zero if \a s1 + is less than, equal to, or greater than \a s2. + + If \a cs is Qt::CaseSensitive, the comparison is case sensitive; + otherwise the comparison is case insensitive. +*/ + +/*! + \overload + \fn int QStringRef::compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 4.5 + + Compares this string with the \a other string and returns an + integer less than, equal to, or greater than zero if this string + is less than, equal to, or greater than the \a other string. + + If \a cs is Qt::CaseSensitive, the comparison is case sensitive; + otherwise the comparison is case insensitive. + + Equivalent to \c {compare(*this, other, cs)}. +*/ + +/*! + \overload + \fn int QStringRef::compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 4.5 + + Compares this string with the \a other string and returns an + integer less than, equal to, or greater than zero if this string + is less than, equal to, or greater than the \a other string. + + If \a cs is Qt::CaseSensitive, the comparison is case sensitive; + otherwise the comparison is case insensitive. + + Equivalent to \c {compare(*this, other, cs)}. +*/ + +/*! + \overload + \fn int QStringRef::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 5.14 + + Compares this string with \a ch and returns an + integer less than, equal to, or greater than zero if this string + is less than, equal to, or greater than \a ch, interpreted as a string of length one. + + If \a cs is Qt::CaseSensitive, the comparison is case sensitive; + otherwise the comparison is case insensitive. +*/ + +/*! + \overload + \fn int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 4.5 + + Compares this string with the \a other string and returns an + integer less than, equal to, or greater than zero if this string + is less than, equal to, or greater than the \a other string. + + If \a cs is Qt::CaseSensitive, the comparison is case sensitive; + otherwise the comparison is case insensitive. + + Equivalent to \c {compare(*this, other, cs)}. +*/ + +/*! + \overload + \fn int QStringRef::compare(const QByteArray &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 5.8 + + Compares this string with \a other and returns an + integer less than, equal to, or greater than zero if this string + is less than, equal to, or greater than the \a other byte array, + interpreted as a UTF-8 sequence. + + If \a cs is Qt::CaseSensitive, the comparison is case sensitive; + otherwise the comparison is case insensitive. + + Equivalent to \c {compare(*this, other, cs)}. +*/ + +/*! + \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QString & s2) + \since 4.5 + + Compares \a s1 with \a s2 and returns an integer less than, equal + to, or greater than zero if \a s1 is less than, equal to, or + greater than \a s2. + + The comparison is performed in a locale- and also + platform-dependent manner. Use this function to present sorted + lists of strings to the user. + + \sa compare(), QLocale, {Comparing Strings} +*/ + +/*! + \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef & s2) + \since 4.5 + \overload + + Compares \a s1 with \a s2 and returns an integer less than, equal + to, or greater than zero if \a s1 is less than, equal to, or + greater than \a s2. + + The comparison is performed in a locale- and also + platform-dependent manner. Use this function to present sorted + lists of strings to the user. + + \sa {Comparing Strings} +*/ + +/*! + \fn int QStringRef::localeAwareCompare(const QString &other) const + \since 4.5 + \overload + + Compares this string with the \a other string and returns an + integer less than, equal to, or greater than zero if this string + is less than, equal to, or greater than the \a other string. + + The comparison is performed in a locale- and also + platform-dependent manner. Use this function to present sorted + lists of strings to the user. + + \sa {Comparing Strings} +*/ + +/*! + \fn int QStringRef::localeAwareCompare(const QStringRef &other) const + \since 4.5 + \overload + + Compares this string with the \a other string and returns an + integer less than, equal to, or greater than zero if this string + is less than, equal to, or greater than the \a other string. + + The comparison is performed in a locale- and also + platform-dependent manner. Use this function to present sorted + lists of strings to the user. + + \sa {Comparing Strings} +*/ + +/*! + \fn QStringRef::left(int n) const + \since 5.2 + + Returns a substring reference to the \a n leftmost characters + of the string. + + If \a n is greater than or equal to size(), or less than zero, + a reference to the entire string is returned. + + \sa right(), mid(), startsWith(), chopped(), chop(), truncate() +*/ +QStringRef QStringRef::left(int n) const +{ + if (size_t(n) >= size_t(m_size)) + return *this; + return QStringRef(m_string, m_position, n); +} + +/*! + \fn QStringRef::right(int n) const + \since 5.2 + + Returns a substring reference to the \a n rightmost characters + of the string. + + If \a n is greater than or equal to size(), or less than zero, + a reference to the entire string is returned. + + \sa left(), mid(), endsWith(), chopped(), chop(), truncate() +*/ +QStringRef QStringRef::right(int n) const +{ + if (size_t(n) >= size_t(m_size)) + return *this; + return QStringRef(m_string, m_size - n + m_position, n); +} + +/*! + \fn QStringRef QStringRef::mid(int position, int n = -1) const + \since 5.2 + + Returns a substring reference to \a n characters of this string, + starting at the specified \a position. + + If the \a position exceeds the length of the string, a null + reference is returned. + + If there are less than \a n characters available in the string, + starting at the given \a position, or if \a n is -1 (default), the + function returns all characters from the specified \a position + onwards. + + \sa left(), right(), chopped(), chop(), truncate() +*/ +QStringRef QStringRef::mid(int pos, int n) const +{ + qsizetype p = pos; + qsizetype l = n; + using namespace QtPrivate; + switch (QContainerImplHelper::mid(m_size, &p, &l)) { + case QContainerImplHelper::Null: + return QStringRef(); + case QContainerImplHelper::Empty: + return QStringRef(m_string, 0, 0); + case QContainerImplHelper::Full: + return *this; + case QContainerImplHelper::Subset: + return QStringRef(m_string, p + m_position, l); + } + Q_UNREACHABLE(); + return QStringRef(); +} + +/*! + \fn QStringRef QStringRef::chopped(int len) const + \since 5.10 + + Returns a substring reference to the size() - \a len leftmost characters + of this string. + + \note The behavior is undefined if \a len is negative or greater than size(). + + \sa endsWith(), left(), right(), mid(), chop(), truncate() +*/ + +/*! + \fn void QStringRef::truncate(int position) + \since 5.6 + + Truncates the string at the given \a position index. + + If the specified \a position index is beyond the end of the + string, nothing happens. + + If \a position is negative, it is equivalent to passing zero. + + \sa QString::truncate() +*/ + +/*! + \fn void QStringRef::chop(int n) + \since 5.8 + + Removes \a n characters from the end of the string. + + If \a n is greater than or equal to size(), the result is an + empty string; if \a n is negative, it is equivalent to passing zero. + + \sa QString::chop(), truncate() +*/ + +#if QT_STRINGVIEW_LEVEL < 2 +/*! + \since 4.8 + + Returns the index position of the first occurrence of the string \a + str in this string reference, searching forward from index position + \a from. Returns -1 if \a str is not found. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + If \a from is -1, the search starts at the last character; if it is + -2, at the next to last character and so on. + + \sa QString::indexOf(), lastIndexOf(), contains(), count() +*/ +int QStringRef::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QtPrivate::findString(QStringView(unicode(), length()), from, QStringView(str.unicode(), str.length()), cs)); +} +#endif // QT_STRINGVIEW_LEVEL < 2 + +/*! + \fn int QStringRef::indexOf(QStringView str, int from, Qt::CaseSensitivity cs) const + \since 5.14 + \overload indexOf() + + Returns the index position of the first occurrence of the string view \a str + in this string reference, searching forward from index position \a from. + Returns -1 if \a str is not found. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + If \a from is -1, the search starts at the last character; if it is + -2, at the next to last character and so on. + + \sa QString::indexOf(), QStringView::indexOf(), lastIndexOf(), contains(), count() +*/ + +/*! + \since 4.8 + \overload indexOf() + + Returns the index position of the first occurrence of the + character \a ch in the string reference, searching forward from + index position \a from. Returns -1 if \a ch could not be found. + + \sa QString::indexOf(), lastIndexOf(), contains(), count() +*/ +int QStringRef::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(qFindChar(QStringView(unicode(), length()), ch, from, cs)); +} + +/*! + \since 4.8 + + Returns the index position of the first occurrence of the string \a + str in this string reference, searching forward from index position + \a from. Returns -1 if \a str is not found. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + If \a from is -1, the search starts at the last character; if it is + -2, at the next to last character and so on. + + \sa QString::indexOf(), lastIndexOf(), contains(), count() +*/ +int QStringRef::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QtPrivate::findString(QStringView(unicode(), size()), from, str, cs)); +} + +#if QT_STRINGVIEW_LEVEL < 2 +/*! + \since 4.8 + + \overload indexOf() + + Returns the index position of the first occurrence of the string + reference \a str in this string reference, searching forward from + index position \a from. Returns -1 if \a str is not found. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + \sa QString::indexOf(), lastIndexOf(), contains(), count() +*/ +int QStringRef::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QtPrivate::findString(QStringView(unicode(), size()), from, QStringView(str.unicode(), str.size()), cs)); +} +#endif // QT_STRINGVIEW_LEVEL < 2 + +/*! + \since 4.8 + + Returns the index position of the last occurrence of the string \a + str in this string reference, searching backward from index position + \a from. If \a from is -1 (default), the search starts at the last + character; if \a from is -2, at the next to last character and so + on. Returns -1 if \a str is not found. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + \sa QString::lastIndexOf(), indexOf(), contains(), count() +*/ +int QStringRef::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QtPrivate::lastIndexOf(*this, from, str, cs)); +} + +/*! + \since 4.8 + \overload lastIndexOf() + + Returns the index position of the last occurrence of the character + \a ch, searching backward from position \a from. + + \sa QString::lastIndexOf(), indexOf(), contains(), count() +*/ +int QStringRef::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QStringView{ *this }.lastIndexOf(ch, from, cs)); +} + +/*! + \since 4.8 + \overload lastIndexOf() + + Returns the index position of the last occurrence of the string \a + str in this string reference, searching backward from index position + \a from. If \a from is -1 (default), the search starts at the last + character; if \a from is -2, at the next to last character and so + on. Returns -1 if \a str is not found. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + \sa QString::lastIndexOf(), indexOf(), contains(), count() +*/ +int QStringRef::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QtPrivate::lastIndexOf(*this, from, str, cs)); +} + +/*! + \since 4.8 + \overload lastIndexOf() + + Returns the index position of the last occurrence of the string + reference \a str in this string reference, searching backward from + index position \a from. If \a from is -1 (default), the search + starts at the last character; if \a from is -2, at the next to last + character and so on. Returns -1 if \a str is not found. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + \sa QString::lastIndexOf(), indexOf(), contains(), count() +*/ +int QStringRef::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QtPrivate::lastIndexOf(*this, from, str, cs)); +} + +/*! + \fn int QStringRef::lastIndexOf(QStringView str, int from, Qt::CaseSensitivity cs) const + \since 5.14 + \overload lastIndexOf() + + Returns the index position of the last occurrence of the string view \a + str in this string, searching backward from index position \a + from. If \a from is -1 (default), the search starts at the last + character; if \a from is -2, at the next to last character and so + on. Returns -1 if \a str is not found. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + \sa indexOf(), contains(), count() +*/ + +/*! + \since 4.8 + Returns the number of (potentially overlapping) occurrences of + the string \a str in this string reference. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa QString::count(), contains(), indexOf() +*/ +int QStringRef::count(const QString &str, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QtPrivate::count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); +} + +/*! + \since 4.8 + \overload count() + + Returns the number of occurrences of the character \a ch in the + string reference. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa QString::count(), contains(), indexOf() +*/ +int QStringRef::count(QChar ch, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QtPrivate::count(QStringView(unicode(), size()), ch, cs)); +} + +/*! + \since 4.8 + \overload count() + + Returns the number of (potentially overlapping) occurrences of the + string reference \a str in this string reference. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa QString::count(), contains(), indexOf() +*/ +int QStringRef::count(const QStringRef &str, Qt::CaseSensitivity cs) const +{ + // ### Qt6: qsizetype + return int(QtPrivate::count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); +} + +/*! + \since 5.9 + + Returns \c true if the string is read right to left. + + \sa QString::isRightToLeft() +*/ +bool QStringRef::isRightToLeft() const +{ + return QtPrivate::isRightToLeft(QStringView(unicode(), size())); +} + +/*! + \since 4.8 + + Returns \c true if the string reference starts with \a str; otherwise + returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa QString::startsWith(), endsWith() +*/ +bool QStringRef::startsWith(const QString &str, Qt::CaseSensitivity cs) const +{ + return qt_starts_with(*this, str, cs); +} + +/*! + \since 4.8 + \overload startsWith() + \sa QString::startsWith(), endsWith() +*/ +bool QStringRef::startsWith(QLatin1String str, Qt::CaseSensitivity cs) const +{ + return qt_starts_with(*this, str, cs); +} + +/*! + \fn bool QStringRef::startsWith(QStringView str, Qt::CaseSensitivity cs) const + \since 5.10 + \overload startsWith() + \sa QString::startsWith(), endsWith() +*/ + +/*! + \since 4.8 + \overload startsWith() + \sa QString::startsWith(), endsWith() +*/ +bool QStringRef::startsWith(const QStringRef &str, Qt::CaseSensitivity cs) const +{ + return qt_starts_with(*this, str, cs); +} + +/*! + \since 4.8 + \overload startsWith() + + Returns \c true if the string reference starts with \a ch; otherwise + returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + \sa QString::startsWith(), endsWith() +*/ +bool QStringRef::startsWith(QChar ch, Qt::CaseSensitivity cs) const +{ + return qt_starts_with(*this, ch, cs); +} + +/*! + \since 4.8 + Returns \c true if the string reference ends with \a str; otherwise + returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + \sa QString::endsWith(), startsWith() +*/ +bool QStringRef::endsWith(const QString &str, Qt::CaseSensitivity cs) const +{ + return qt_ends_with(*this, str, cs); +} + +/*! + \since 4.8 + \overload endsWith() + + Returns \c true if the string reference ends with \a ch; otherwise + returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is case + sensitive; otherwise the search is case insensitive. + + \sa QString::endsWith(), endsWith() +*/ +bool QStringRef::endsWith(QChar ch, Qt::CaseSensitivity cs) const +{ + return qt_ends_with(*this, ch, cs); +} + +/*! + \since 4.8 + \overload endsWith() + \sa QString::endsWith(), endsWith() +*/ +bool QStringRef::endsWith(QLatin1String str, Qt::CaseSensitivity cs) const +{ + return qt_ends_with(*this, str, cs); +} + +/*! + \fn bool QStringRef::endsWith(QStringView str, Qt::CaseSensitivity cs) const + \since 5.10 + \overload endsWith() + \sa QString::endsWith(), startsWith() +*/ + +/*! + \since 4.8 + \overload endsWith() + \sa QString::endsWith(), endsWith() +*/ +bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const +{ + return qt_ends_with(*this, str, cs); +} + +#if QT_STRINGVIEW_LEVEL < 2 +/*! \fn bool QStringRef::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + + \since 4.8 + Returns \c true if this string reference contains an occurrence of + the string \a str; otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa indexOf(), count() +*/ +#endif // QT_STRINGVIEW_LEVEL < 2 + +/*! \fn bool QStringRef::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + + \overload contains() + \since 4.8 + + Returns \c true if this string contains an occurrence of the + character \a ch; otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + +*/ + +#if QT_STRINGVIEW_LEVEL < 2 +/*! \fn bool QStringRef::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \overload contains() + \since 4.8 + + Returns \c true if this string reference contains an occurrence of + the string reference \a str; otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa indexOf(), count() +*/ +#endif // QT_STRINGVIEW_LEVEL < 2 + +/*! \fn bool QStringRef::contains(QLatin1String str, Qt::CaseSensitivity cs) const + \since 4.8 + \overload contains() + + Returns \c true if this string reference contains an occurrence of + the string \a str; otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa indexOf(), count() +*/ + +/*! \fn bool QStringRef::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 5.14 + \overload contains() + + Returns \c true if this string reference contains an occurrence of + the string view \a str; otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa indexOf(), count() +*/ + +template +bool qt_starts_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept +{ + if (haystack.isNull()) + return needle.isNull(); // historical behavior, consider changing in ### Qt 6. + const auto haystackLen = haystack.size(); + const auto needleLen = needle.size(); + if (haystackLen == 0) + return needleLen == 0; + if (needleLen > haystackLen) + return false; + + return qt_compare_strings(haystack.left(needleLen), needle, cs) == 0; +} + +static inline bool qt_starts_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) +{ + return qt_starts_with_impl(haystack, needle, cs); +} + +static inline bool qt_starts_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) +{ + return qt_starts_with_impl(haystack, needle, cs); +} + +static inline bool qt_starts_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs) +{ + return haystack.size() + && (cs == Qt::CaseSensitive ? haystack.front() == needle + : QCharPrivate::foldCase(haystack.front()) == QCharPrivate::foldCase(needle)); +} + +template +bool qt_ends_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept +{ + if (haystack.isNull()) + return needle.isNull(); // historical behavior, consider changing in ### Qt 6. + const auto haystackLen = haystack.size(); + const auto needleLen = needle.size(); + if (haystackLen == 0) + return needleLen == 0; + if (haystackLen < needleLen) + return false; + + return qt_compare_strings(haystack.right(needleLen), needle, cs) == 0; +} + +static inline bool qt_ends_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) +{ + return qt_ends_with_impl(haystack, needle, cs); +} + +static inline bool qt_ends_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) +{ + return qt_ends_with_impl(haystack, needle, cs); +} + +static inline bool qt_ends_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs) +{ + return haystack.size() + && (cs == Qt::CaseSensitive ? haystack.back() == needle + : QCharPrivate::foldCase(haystack.back()) == QCharPrivate::foldCase(needle)); +} + +/*! + \internal + + Returns the index position of the first occurrence of the + character \a ch in the string given by \a str and \a len, + searching forward from index + position \a from. Returns -1 if \a ch could not be found. +*/ + +static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept +{ + if (from < 0) + from = qMax(from + str.size(), qsizetype(0)); + if (from < str.size()) { + const char16_t *s = str.utf16(); + char16_t c = ch.unicode(); + const char16_t *n = s + from; + const char16_t *e = s + str.size(); + if (cs == Qt::CaseSensitive) { + n = QtPrivate::qustrchr(QStringView(n, e), c); + if (n != e) + return n - s; + } else { + c = QCharPrivate::foldCase(c); + --n; + while (++n != e) + if (QCharPrivate::foldCase(*n) == c) + return n - s; + } + } + return -1; +} + +/*! + \since 4.8 + + Returns a Latin-1 representation of the string as a QByteArray. + + The returned byte array is undefined if the string contains non-Latin1 + characters. Those characters may be suppressed or replaced with a + question mark. + + \sa toUtf8(), toLocal8Bit(), QStringEncoder +*/ +QByteArray QStringRef::toLatin1() const +{ + return QStringView{ *this }.toLatin1(); +} + +/*! + \since 4.8 + + Returns the local 8-bit representation of the string as a + QByteArray. The returned byte array is undefined if the string + contains characters not supported by the local 8-bit encoding. + + On Unix systems this is equivalent to toUtf8(), on Windows the systems + current code page is being used. + + If this string contains any characters that cannot be encoded in the + locale, the returned byte array is undefined. Those characters may be + suppressed or replaced by another. + + \sa toLatin1(), toUtf8(), QStringEncoder +*/ +QByteArray QStringRef::toLocal8Bit() const +{ + return qt_convert_to_local_8bit(*this); +} + +/*! + \since 4.8 + + Returns a UTF-8 representation of the string as a QByteArray. + + UTF-8 is a Unicode codec and can represent all characters in a Unicode + string like QString. + + \sa toLatin1(), toLocal8Bit(), QStringEncoder +*/ +QByteArray QStringRef::toUtf8() const +{ + return qt_convert_to_utf8(*this); +} + +/*! + \since 4.8 + + Returns a UCS-4/UTF-32 representation of the string as a QList. + + UCS-4 is a Unicode codec and therefore it is lossless. All characters from + this string will be encoded in UCS-4. Any invalid sequence of code units in + this string is replaced by the Unicode's replacement character + (QChar::ReplacementCharacter, which corresponds to \c{U+FFFD}). + + The returned list is not \\0'-terminated. + + \sa toUtf8(), toLatin1(), toLocal8Bit(), QStringEncoder +*/ +QList QStringRef::toUcs4() const +{ + return qt_convert_to_ucs4(*this); +} + +/*! + Returns a string that has whitespace removed from the start and + the end. + + Whitespace means any character for which QChar::isSpace() returns + \c true. This includes the ASCII characters '\\t', '\\n', '\\v', + '\\f', '\\r', and ' '. + + Unlike QString::simplified(), trimmed() leaves internal whitespace alone. + + \since 5.1 + + \sa QString::trimmed() +*/ +QStringRef QStringRef::trimmed() const +{ + const QChar *begin = cbegin(); + const QChar *end = cend(); + QStringAlgorithms::trimmed_helper_positions(begin, end); + if (begin == cbegin() && end == cend()) + return *this; + int position = m_position + (begin - cbegin()); + return QStringRef(m_string, position, end - begin); +} + +/*! + Returns the string converted to a \c{long long} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toLongLong() + + \sa QString::toLongLong() + + \since 5.1 +*/ +qint64 QStringRef::toLongLong(bool *ok, int base) const +{ + return QStringView{ *this }.toLongLong(ok, base); +} + +/*! + Returns the string converted to an \c{unsigned long long} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toULongLong() + + \sa QString::toULongLong() + + \since 5.1 +*/ +quint64 QStringRef::toULongLong(bool *ok, int base) const +{ + return QStringView{ *this }.toULongLong(ok, base); +} + +/*! + \fn long QStringRef::toLong(bool *ok, int base) const + + Returns the string converted to a \c long using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toLong() + + \sa QString::toLong() + + \since 5.1 +*/ +long QStringRef::toLong(bool *ok, int base) const +{ + return QStringView{ *this }.toLong(ok, base); +} + +/*! + \fn ulong QStringRef::toULong(bool *ok, int base) const + + Returns the string converted to an \c{unsigned long} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toULongLong() + + \sa QString::toULong() + + \since 5.1 +*/ +ulong QStringRef::toULong(bool *ok, int base) const +{ + return QStringView{ *this }.toULong(ok, base); +} + +/*! + Returns the string converted to an \c int using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toInt() + + \sa QString::toInt() + + \since 5.1 +*/ +int QStringRef::toInt(bool *ok, int base) const +{ + return QStringView{ *this }.toInt(ok, base); +} + +/*! + Returns the string converted to an \c{unsigned int} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toUInt() + + \sa QString::toUInt() + + \since 5.1 +*/ +uint QStringRef::toUInt(bool *ok, int base) const +{ + return QStringView{ *this }.toUInt(ok, base); +} + +/*! + Returns the string converted to a \c short using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toShort() + + \sa QString::toShort() + + \since 5.1 +*/ +short QStringRef::toShort(bool *ok, int base) const +{ + return QStringView{ *this }.toShort(ok, base); +} + +/*! + Returns the string converted to an \c{unsigned short} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toUShort() + + \sa QString::toUShort() + + \since 5.1 +*/ +ushort QStringRef::toUShort(bool *ok, int base) const +{ + return QStringView{ *this }.toUShort(ok, base); +} + +/*! + Returns the string converted to a \c double value. + + Returns an infinity if the conversion overflows or 0.0 if the + conversion fails for other reasons (e.g. underflow). + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toDouble() + + For historic reasons, this function does not handle + thousands group separators. If you need to convert such numbers, + use QLocale::toDouble(). + + \sa QString::toDouble() + + \since 5.1 +*/ +double QStringRef::toDouble(bool *ok) const +{ + return QStringView{ *this }.toDouble(ok); +} + +/*! + Returns the string converted to a \c float value. + + Returns an infinity if the conversion overflows or 0.0 if the + conversion fails for other reasons (e.g. underflow). + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toFloat() + + \sa QString::toFloat() + + \since 5.1 +*/ +float QStringRef::toFloat(bool *ok) const +{ + return QStringView{ *this }.toFloat(ok); +} + +/*! \fn size_t qHash(const QStringRef &key, size_t seed = 0) + \relates QHash + \since 5.0 + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + +QT_END_NAMESPACE diff --git a/src/core5/text/qstringref.h b/src/core5/text/qstringref.h new file mode 100644 index 0000000..b99ab16 --- /dev/null +++ b/src/core5/text/qstringref.h @@ -0,0 +1,487 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 Intel Corporation. +** Copyright (C) 2019 Mail.ru Group. +** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSTRINGREF_H +#define QSTRINGREF_H + +#if defined(QT_NO_CAST_FROM_ASCII) && defined(QT_RESTRICTED_CAST_FROM_ASCII) +#error QT_NO_CAST_FROM_ASCII and QT_RESTRICTED_CAST_FROM_ASCII must not be defined at the same time +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#ifdef truncate +#error qstringref.h must be included before any header file that defines truncate +#endif + +QT_BEGIN_NAMESPACE + +class Q_CORE5COMPAT_EXPORT QStringRef +{ + const QString *m_string; + int m_position; + int m_size; +public: + typedef QString::size_type size_type; + typedef QString::value_type value_type; + typedef const QChar *const_iterator; + typedef std::reverse_iterator const_reverse_iterator; + typedef QString::const_pointer const_pointer; + typedef QString::const_reference const_reference; + + constexpr QStringRef() noexcept + : m_string(nullptr), m_position(0), m_size(0) { } + inline QStringRef(const QString *string, int position, int size); + inline QStringRef(const QString *string); + + inline const QString *string() const { return m_string; } + inline int position() const { return m_position; } + inline int size() const { return m_size; } + inline int count() const { return m_size; } + inline int length() const { return m_size; } + +#if QT_STRINGVIEW_LEVEL < 2 + int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + int indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#endif + Q_REQUIRED_RESULT int indexOf(QStringView s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return int(QtPrivate::findString(*this, from, s, cs)); } // ### Qt6: qsizetype + int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + int indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#if QT_STRINGVIEW_LEVEL < 2 + int lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#endif + int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + int lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT int lastIndexOf(QStringView s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return int(QtPrivate::lastIndexOf(*this, from, s, cs)); } // ### Qt6: qsizetype + +#if QT_STRINGVIEW_LEVEL < 2 + inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + inline bool contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#endif + inline bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + inline bool contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + + int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + + Q_REQUIRED_RESULT + QList split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT + QList split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + + Q_REQUIRED_RESULT QStringRef left(int n) const; + Q_REQUIRED_RESULT QStringRef right(int n) const; + Q_REQUIRED_RESULT QStringRef mid(int pos, int n = -1) const; + Q_REQUIRED_RESULT QStringRef chopped(int n) const + { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return left(size() - n); } + + void truncate(int pos) noexcept { m_size = qBound(0, pos, m_size); } + void chop(int n) noexcept + { + if (n >= m_size) + m_size = 0; + else if (n > 0) + m_size -= n; + } + + bool isRightToLeft() const; + + Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::startsWith(*this, s, cs); } + bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#if QT_STRINGVIEW_LEVEL < 2 + bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + bool startsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#endif + + Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::endsWith(*this, s, cs); } + bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#if QT_STRINGVIEW_LEVEL < 2 + bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + bool endsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#endif + + inline operator QStringView() const { + if (!m_string) + return {}; + return QStringView(m_string->data() + m_position, m_size); + } + inline QStringRef &operator=(const QString *string); + + inline const QChar *unicode() const + { + static const char16_t _empty = 0; + if (!m_string) + return reinterpret_cast(&_empty); + return m_string->unicode() + m_position; + } + inline const QChar *data() const { return unicode(); } + inline const QChar *constData() const { return unicode(); } + + inline const_iterator begin() const { return unicode(); } + inline const_iterator cbegin() const { return unicode(); } + inline const_iterator constBegin() const { return unicode(); } + inline const_iterator end() const { return unicode() + size(); } + inline const_iterator cend() const { return unicode() + size(); } + inline const_iterator constEnd() const { return unicode() + size(); } + inline const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } + inline const_reverse_iterator crbegin() const { return rbegin(); } + inline const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } + inline const_reverse_iterator crend() const { return rend(); } + + Q_REQUIRED_RESULT QByteArray toLatin1() const; + Q_REQUIRED_RESULT QByteArray toUtf8() const; + Q_REQUIRED_RESULT QByteArray toLocal8Bit() const; + Q_REQUIRED_RESULT QList toUcs4() const; + + inline void clear() { m_string = nullptr; m_position = m_size = 0; } + QString toString() const; + inline bool isEmpty() const { return m_size == 0; } + inline bool isNull() const { return m_string == nullptr || m_string->isNull(); } + + QStringRef appendTo(QString *string) const; + + inline const QChar at(int i) const + { Q_ASSERT(uint(i) < uint(size())); return m_string->at(i + m_position); } + QChar operator[](int i) const { return at(i); } + Q_REQUIRED_RESULT QChar front() const { return at(0); } + Q_REQUIRED_RESULT QChar back() const { return at(size() - 1); } + +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + // ASCII compatibility + inline QT_ASCII_CAST_WARN bool operator==(const char *s) const; + inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const; + inline QT_ASCII_CAST_WARN bool operator<(const char *s) const; + inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const; + inline QT_ASCII_CAST_WARN bool operator>(const char *s) const; + inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const; +#endif + + int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + int compare(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); } + int compare(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + int compare(const QByteArray &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + { return QStringRef::compare_helper(unicode(), size(), s.data(), qstrnlen(s.data(), s.size()), cs); } +#endif + static int compare(const QStringRef &s1, const QString &s2, + Qt::CaseSensitivity = Qt::CaseSensitive) noexcept; + static int compare(const QStringRef &s1, const QStringRef &s2, + Qt::CaseSensitivity = Qt::CaseSensitive) noexcept; + static int compare(const QStringRef &s1, QLatin1String s2, + Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; + + int localeAwareCompare(const QString &s) const; + int localeAwareCompare(const QStringRef &s) const; + static int localeAwareCompare(const QStringRef &s1, const QString &s2); + static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2); + + Q_REQUIRED_RESULT QStringRef trimmed() const; + short toShort(bool *ok = nullptr, int base = 10) const; + ushort toUShort(bool *ok = nullptr, int base = 10) const; + int toInt(bool *ok = nullptr, int base = 10) const; + uint toUInt(bool *ok = nullptr, int base = 10) const; + long toLong(bool *ok = nullptr, int base = 10) const; + ulong toULong(bool *ok = nullptr, int base = 10) const; + qlonglong toLongLong(bool *ok = nullptr, int base = 10) const; + qulonglong toULongLong(bool *ok = nullptr, int base = 10) const; + float toFloat(bool *ok = nullptr) const; + double toDouble(bool *ok = nullptr) const; + + friend inline bool operator==(QChar, const QStringRef &) noexcept; + friend inline bool operator<(QChar, const QStringRef &) noexcept; + friend inline bool operator>(QChar, const QStringRef &) noexcept; + +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + friend inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2); + friend inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2); + friend inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QStringRef &s2); + friend inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QStringRef &s2); + friend inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QStringRef &s2); + friend inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QStringRef &s2); +#endif + +private: + static int compare_helper(const QChar *data1, qsizetype length1, + const QChar *data2, qsizetype length2, + Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; + static int compare_helper(const QChar *data1, qsizetype length1, + const char *data2, qsizetype length2, + Qt::CaseSensitivity cs = Qt::CaseSensitive); + static int compare_helper(const QChar *data1, qsizetype length1, + QLatin1String s2, + Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; +}; +Q_DECLARE_TYPEINFO(QStringRef, Q_PRIMITIVE_TYPE); + +namespace QtPrivate { +namespace Tok { + template <> struct ViewForImpl : ViewForImpl {}; + } // namespace Tok +} // namespace QtPrivate + +#if QT_STRINGVIEW_LEVEL < 2 +inline Q_DECL_PURE_FUNCTION size_t qHash(const QStringRef &key, size_t seed = 0) noexcept +{ + return qHash(QStringView { key }, seed); +} +#endif +QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QStringRef) + +inline QStringRef &QStringRef::operator=(const QString *aString) +{ m_string = aString; m_position = 0; m_size = aString?aString->size():0; return *this; } + +inline QStringRef::QStringRef(const QString *aString, int aPosition, int aSize) + :m_string(aString), m_position(aPosition), m_size(aSize){} + +inline QStringRef::QStringRef(const QString *aString) + :m_string(aString), m_position(0), m_size(aString?aString->size() : 0){} + +// QStringRef <> QStringRef +Q_CORE5COMPAT_EXPORT bool operator==(const QStringRef &s1, const QStringRef &s2) noexcept; +inline bool operator!=(const QStringRef &s1, const QStringRef &s2) noexcept +{ return !(s1 == s2); } +Q_CORE5COMPAT_EXPORT bool operator<(const QStringRef &s1, const QStringRef &s2) noexcept; +inline bool operator>(const QStringRef &s1, const QStringRef &s2) noexcept +{ return s2 < s1; } +inline bool operator<=(const QStringRef &s1, const QStringRef &s2) noexcept +{ return !(s1 > s2); } +inline bool operator>=(const QStringRef &s1, const QStringRef &s2) noexcept +{ return !(s1 < s2); } + +// QString <> QStringRef +Q_CORE5COMPAT_EXPORT bool operator==(const QString &lhs, const QStringRef &rhs) noexcept; +inline bool operator!=(const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) != 0; } +inline bool operator< (const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) < 0; } +inline bool operator> (const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) > 0; } +inline bool operator<=(const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) <= 0; } +inline bool operator>=(const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) >= 0; } + +inline bool operator==(const QStringRef &lhs, const QString &rhs) noexcept { return rhs == lhs; } +inline bool operator!=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs != lhs; } +inline bool operator< (const QStringRef &lhs, const QString &rhs) noexcept { return rhs > lhs; } +inline bool operator> (const QStringRef &lhs, const QString &rhs) noexcept { return rhs < lhs; } +inline bool operator<=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs >= lhs; } +inline bool operator>=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs <= lhs; } + +inline int QStringRef::compare(const QString &s, Qt::CaseSensitivity cs) const noexcept +{ return QStringRef::compare_helper(constData(), length(), s.constData(), s.length(), cs); } +inline int QStringRef::compare(const QStringRef &s, Qt::CaseSensitivity cs) const noexcept +{ return QStringRef::compare_helper(constData(), length(), s.constData(), s.length(), cs); } +inline int QStringRef::compare(QLatin1String s, Qt::CaseSensitivity cs) const noexcept +{ return QStringRef::compare_helper(constData(), length(), s, cs); } +inline int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs) noexcept +{ return QStringRef::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); } +inline int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs) noexcept +{ return QStringRef::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); } +inline int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs) noexcept +{ return QStringRef::compare_helper(s1.constData(), s1.length(), s2, cs); } + +// QLatin1String <> QStringRef +Q_CORE5COMPAT_EXPORT bool operator==(QLatin1String lhs, const QStringRef &rhs) noexcept; +inline bool operator!=(QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) != 0; } +inline bool operator< (QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) > 0; } +inline bool operator> (QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) < 0; } +inline bool operator<=(QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) >= 0; } +inline bool operator>=(QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) <= 0; } + +inline bool operator==(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs == lhs; } +inline bool operator!=(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs != lhs; } +inline bool operator< (const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs > lhs; } +inline bool operator> (const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs < lhs; } +inline bool operator<=(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs >= lhs; } +inline bool operator>=(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs <= lhs; } + +// QChar <> QStringRef +inline bool operator==(QChar lhs, const QStringRef &rhs) noexcept +{ return rhs.size() == 1 && lhs == rhs.front(); } +inline bool operator< (QChar lhs, const QStringRef &rhs) noexcept +{ return QStringRef::compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; } +inline bool operator> (QChar lhs, const QStringRef &rhs) noexcept +{ return QStringRef::compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; } + +inline bool operator!=(QChar lhs, const QStringRef &rhs) noexcept { return !(lhs == rhs); } +inline bool operator<=(QChar lhs, const QStringRef &rhs) noexcept { return !(lhs > rhs); } +inline bool operator>=(QChar lhs, const QStringRef &rhs) noexcept { return !(lhs < rhs); } + +inline bool operator==(const QStringRef &lhs, QChar rhs) noexcept { return rhs == lhs; } +inline bool operator!=(const QStringRef &lhs, QChar rhs) noexcept { return !(rhs == lhs); } +inline bool operator< (const QStringRef &lhs, QChar rhs) noexcept { return rhs > lhs; } +inline bool operator> (const QStringRef &lhs, QChar rhs) noexcept { return rhs < lhs; } +inline bool operator<=(const QStringRef &lhs, QChar rhs) noexcept { return !(rhs < lhs); } +inline bool operator>=(const QStringRef &lhs, QChar rhs) noexcept { return !(rhs > lhs); } + +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) +// QStringRef <> QByteArray +inline QT_ASCII_CAST_WARN bool operator==(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) == 0; } +inline QT_ASCII_CAST_WARN bool operator!=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) != 0; } +inline QT_ASCII_CAST_WARN bool operator< (const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) < 0; } +inline QT_ASCII_CAST_WARN bool operator> (const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) > 0; } +inline QT_ASCII_CAST_WARN bool operator<=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) <= 0; } +inline QT_ASCII_CAST_WARN bool operator>=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) >= 0; } + +inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) == 0; } +inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) != 0; } +inline QT_ASCII_CAST_WARN bool operator< (const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) > 0; } +inline QT_ASCII_CAST_WARN bool operator> (const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) < 0; } +inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) >= 0; } +inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) <= 0; } + +// QStringRef <> const char * +inline QT_ASCII_CAST_WARN bool QStringRef::operator==(const char *s) const +{ return QStringRef::compare_helper(constData(), size(), s, -1) == 0; } +inline QT_ASCII_CAST_WARN bool QStringRef::operator!=(const char *s) const +{ return QStringRef::compare_helper(constData(), size(), s, -1) != 0; } +inline QT_ASCII_CAST_WARN bool QStringRef::operator<(const char *s) const +{ return QStringRef::compare_helper(constData(), size(), s, -1) < 0; } +inline QT_ASCII_CAST_WARN bool QStringRef::operator<=(const char *s) const +{ return QStringRef::compare_helper(constData(), size(), s, -1) <= 0; } +inline QT_ASCII_CAST_WARN bool QStringRef::operator>(const char *s) const +{ return QStringRef::compare_helper(constData(), size(), s, -1) > 0; } +inline QT_ASCII_CAST_WARN bool QStringRef::operator>=(const char *s) const +{ return QStringRef::compare_helper(constData(), size(), s, -1) >= 0; } + +inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2) +{ return QStringRef::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; } +inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2) +{ return QStringRef::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; } +inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QStringRef &s2) +{ return QStringRef::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; } +inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QStringRef &s2) +{ return QStringRef::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; } +inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QStringRef &s2) +{ return QStringRef::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; } +inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QStringRef &s2) +{ return QStringRef::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; } +#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + +inline int QStringRef::localeAwareCompare(const QString &s) const +{ return QString::localeAwareCompare(QStringView{ *this }, QStringView{ s }); } +inline int QStringRef::localeAwareCompare(const QStringRef &s) const +{ return QString::localeAwareCompare(QStringView{ *this }, QStringView{ s }); } +inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QString &s2) +{ return QString::localeAwareCompare(QStringView{ s1 }, QStringView{ s2 }); } +inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef &s2) +{ return QString::localeAwareCompare(QStringView{ s1 }, QStringView{ s2 }); } + +#if QT_STRINGVIEW_LEVEL < 2 +inline bool QStringRef::contains(const QString &s, Qt::CaseSensitivity cs) const +{ return indexOf(s, 0, cs) != -1; } +inline bool QStringRef::contains(const QStringRef &s, Qt::CaseSensitivity cs) const +{ return indexOf(s, 0, cs) != -1; } +#endif +inline bool QStringRef::contains(QLatin1String s, Qt::CaseSensitivity cs) const +{ return indexOf(s, 0, cs) != -1; } +inline bool QStringRef::contains(QChar c, Qt::CaseSensitivity cs) const +{ return indexOf(c, 0, cs) != -1; } +inline bool QStringRef::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept +{ return indexOf(s, 0, cs) != -1; } + +#if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER) +inline QString operator+(const QString &s1, const QStringRef &s2) +{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } +inline QString operator+(const QStringRef &s1, const QString &s2) +{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } +inline QString operator+(const QStringRef &s1, QLatin1String s2) +{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } +inline QString operator+(QLatin1String s1, const QStringRef &s2) +{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } +inline QString operator+(const QStringRef &s1, const QStringRef &s2) +{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } +inline QString operator+(const QStringRef &s1, QChar s2) +{ QString t; t.reserve(s1.size() + 1); t += s1; t += s2; return t; } +inline QString operator+(QChar s1, const QStringRef &s2) +{ QString t; t.reserve(1 + s2.size()); t += s1; t += s2; return t; } +#endif // !(QT_USE_FAST_OPERATOR_PLUS || QT_USE_QSTRINGBUILDER) + +QT_END_NAMESPACE + +#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER) + +#include + +QT_BEGIN_NAMESPACE + +template <> struct QConcatenable : private QAbstractConcatenable +{ + typedef QStringRef type; + typedef QString ConvertTo; + enum { ExactSize = true }; + static int size(const QStringRef &a) { return a.size(); } + static inline void appendTo(const QStringRef &a, QChar *&out) + { + const int n = a.size(); + if (n) + memcpy(out, reinterpret_cast(a.constData()), sizeof(QChar) * n); + out += n; + } +}; + +QT_END_NAMESPACE + +#endif + +#endif // QSTRINGREF_H diff --git a/src/core5/text/text.pri b/src/core5/text/text.pri new file mode 100644 index 0000000..527810a --- /dev/null +++ b/src/core5/text/text.pri @@ -0,0 +1,4 @@ +QT += core-private +HEADERS += text/qstringref.h +SOURCES += text/qstringref.cpp +DEFINES += QT_USE_FAST_OPERATOR_PLUS QT_USE_QSTRINGBUILDER diff --git a/tests/auto/core5/CMakeLists.txt b/tests/auto/core5/CMakeLists.txt index 48478fb..73859b5 100644 --- a/tests/auto/core5/CMakeLists.txt +++ b/tests/auto/core5/CMakeLists.txt @@ -4,3 +4,5 @@ add_subdirectory(qlinkedlist) add_subdirectory(qregexp) add_subdirectory(codecs) add_subdirectory(serialization) +add_subdirectory(text) +add_subdirectory(io) diff --git a/tests/auto/core5/codecs/utf8/CMakeLists.txt b/tests/auto/core5/codecs/utf8/CMakeLists.txt index 5d22471..add63a5 100644 --- a/tests/auto/core5/codecs/utf8/CMakeLists.txt +++ b/tests/auto/core5/codecs/utf8/CMakeLists.txt @@ -6,8 +6,8 @@ add_qt_test(tst_utf8 SOURCES - utf8data.h tst_utf8.cpp + utf8data.h PUBLIC_LIBRARIES Qt::Core5Compat ) diff --git a/tests/auto/core5/core5.pro b/tests/auto/core5/core5.pro index 09e903c..e80b4d9 100644 --- a/tests/auto/core5/core5.pro +++ b/tests/auto/core5/core5.pro @@ -3,4 +3,6 @@ SUBDIRS += \ qlinkedlist \ qregexp \ codecs \ - serialization + serialization \ + text \ + io diff --git a/tests/auto/core5/io/CMakeLists.txt b/tests/auto/core5/io/CMakeLists.txt new file mode 100644 index 0000000..2f826b0 --- /dev/null +++ b/tests/auto/core5/io/CMakeLists.txt @@ -0,0 +1,3 @@ +# Generated from io.pro. + +add_subdirectory(qdebug) diff --git a/tests/auto/core5/io/io.pro b/tests/auto/core5/io/io.pro new file mode 100644 index 0000000..825d341 --- /dev/null +++ b/tests/auto/core5/io/io.pro @@ -0,0 +1,4 @@ +TEMPLATE = subdirs + +SUBDIRS = \ + qdebug diff --git a/tests/auto/core5/io/qdebug/CMakeLists.txt b/tests/auto/core5/io/qdebug/CMakeLists.txt new file mode 100644 index 0000000..b5cc5db --- /dev/null +++ b/tests/auto/core5/io/qdebug/CMakeLists.txt @@ -0,0 +1,12 @@ +# Generated from qdebug.pro. + +##################################################################### +## tst_qdebug Test: +##################################################################### + +qt_add_test(tst_qdebug + SOURCES + tst_qdebug.cpp + PUBLIC_LIBRARIES + Qt::Core5Compat +) diff --git a/tests/auto/core5/io/qdebug/qdebug.pro b/tests/auto/core5/io/qdebug/qdebug.pro new file mode 100644 index 0000000..f7ffb89 --- /dev/null +++ b/tests/auto/core5/io/qdebug/qdebug.pro @@ -0,0 +1,4 @@ +CONFIG += testcase +TARGET = tst_qdebug +QT = core testlib core5compat +SOURCES = tst_qdebug.cpp diff --git a/tests/auto/core5/io/qdebug/tst_qdebug.cpp b/tests/auto/core5/io/qdebug/tst_qdebug.cpp new file mode 100644 index 0000000..82653f7 --- /dev/null +++ b/tests/auto/core5/io/qdebug/tst_qdebug.cpp @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2016 Intel Corporation. +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +class tst_QDebug: public QObject +{ + Q_OBJECT + +private slots: + void qDebugQStringRef() const; +}; + +static QtMsgType s_msgType; +static QString s_msg; +static QByteArray s_file; +static int s_line; +static QByteArray s_function; + +static void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + s_msg = msg; + s_msgType = type; + s_file = context.file; + s_line = context.line; + s_function = context.function; +} + +// Helper class to ensure that the testlib message handler gets +// restored at the end of each test function, even if the test +// fails or throws an exception. +class MessageHandlerSetter +{ +public: + MessageHandlerSetter(QtMessageHandler newMessageHandler) + : oldMessageHandler(qInstallMessageHandler(newMessageHandler)) + { } + + ~MessageHandlerSetter() + { + qInstallMessageHandler(oldMessageHandler); + } + +private: + QtMessageHandler oldMessageHandler; +}; + +void tst_QDebug::qDebugQStringRef() const +{ + /* Use a basic string. */ + { + QString file, function; + int line = 0; + const QString in(QLatin1String("input")); + const QStringRef inRef(&in); + + MessageHandlerSetter mhs(myMessageHandler); + { qDebug() << inRef; } +#ifndef QT_NO_MESSAGELOGCONTEXT + file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO; +#endif + QCOMPARE(s_msgType, QtDebugMsg); + QCOMPARE(s_msg, QString::fromLatin1("\"input\"")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); + } + + /* Use a null QStringRef. */ + { + QString file, function; + int line = 0; + + const QStringRef inRef; + + MessageHandlerSetter mhs(myMessageHandler); + { qDebug() << inRef; } +#ifndef QT_NO_MESSAGELOGCONTEXT + file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO; +#endif + QCOMPARE(s_msgType, QtDebugMsg); + QCOMPARE(s_msg, QString::fromLatin1("\"\"")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); + } +} + +QTEST_MAIN(tst_QDebug); +#include "tst_qdebug.moc" diff --git a/tests/auto/core5/serialization/CMakeLists.txt b/tests/auto/core5/serialization/CMakeLists.txt index 873e834..97c0e44 100644 --- a/tests/auto/core5/serialization/CMakeLists.txt +++ b/tests/auto/core5/serialization/CMakeLists.txt @@ -1,3 +1,4 @@ # Generated from serialization.pro. add_subdirectory(json) +add_subdirectory(qtextstream) diff --git a/tests/auto/core5/serialization/qtextstream/CMakeLists.txt b/tests/auto/core5/serialization/qtextstream/CMakeLists.txt new file mode 100644 index 0000000..81c1382 --- /dev/null +++ b/tests/auto/core5/serialization/qtextstream/CMakeLists.txt @@ -0,0 +1,12 @@ +# Generated from qtextstream.pro. + +##################################################################### +## tst_qtextstream Test: +##################################################################### + +qt_add_test(tst_qtextstream + SOURCES + tst_qtextstream.cpp + PUBLIC_LIBRARIES + Qt::Core5Compat +) diff --git a/tests/auto/core5/serialization/qtextstream/qtextstream.pro b/tests/auto/core5/serialization/qtextstream/qtextstream.pro new file mode 100644 index 0000000..0ad52c6 --- /dev/null +++ b/tests/auto/core5/serialization/qtextstream/qtextstream.pro @@ -0,0 +1,4 @@ +CONFIG += testcase +TARGET = tst_qtextstream +QT = core testlib core5compat +SOURCES = tst_qtextstream.cpp diff --git a/tests/auto/core5/serialization/qtextstream/tst_qtextstream.cpp b/tests/auto/core5/serialization/qtextstream/tst_qtextstream.cpp new file mode 100644 index 0000000..24ea46b --- /dev/null +++ b/tests/auto/core5/serialization/qtextstream/tst_qtextstream.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include +#include +#include +#include + +class tst_QTextStream : public QObject +{ + Q_OBJECT + +private slots: + // text write operators + void stringref_write_operator_ToDevice(); +}; + +void tst_QTextStream::stringref_write_operator_ToDevice() +{ + QBuffer buf; + buf.open(QBuffer::WriteOnly); + QTextStream stream(&buf); + stream.setEncoding(QStringConverter::Latin1); + stream.setAutoDetectUnicode(true); + + const QString expected = "No explicit lengthExplicit length"; + + stream << QStringRef(&expected).left(18); + stream << QStringRef(&expected).mid(18); + stream.flush(); + QCOMPARE(buf.buffer().constData(), "No explicit lengthExplicit length"); +} + +QTEST_MAIN(tst_QTextStream) +#include "tst_qtextstream.moc" diff --git a/tests/auto/core5/serialization/serialization.pro b/tests/auto/core5/serialization/serialization.pro index b26ade7..d00dc97 100644 --- a/tests/auto/core5/serialization/serialization.pro +++ b/tests/auto/core5/serialization/serialization.pro @@ -1,2 +1,3 @@ TEMPLATE = subdirs -SUBDIRS = json +SUBDIRS = json \ + qtextstream diff --git a/tests/auto/core5/text/CMakeLists.txt b/tests/auto/core5/text/CMakeLists.txt new file mode 100644 index 0000000..c1fa7c7 --- /dev/null +++ b/tests/auto/core5/text/CMakeLists.txt @@ -0,0 +1,4 @@ +# Generated from text.pro. + +add_subdirectory(qstringref) +add_subdirectory(qstringbuilder) diff --git a/tests/auto/core5/text/qstringbuilder/CMakeLists.txt b/tests/auto/core5/text/qstringbuilder/CMakeLists.txt new file mode 100644 index 0000000..b7a4ef1 --- /dev/null +++ b/tests/auto/core5/text/qstringbuilder/CMakeLists.txt @@ -0,0 +1,3 @@ +# Generated from qstringbuilder.pro. + +add_subdirectory(qstringbuilder1) diff --git a/tests/auto/core5/text/qstringbuilder/qstringbuilder.pro b/tests/auto/core5/text/qstringbuilder/qstringbuilder.pro new file mode 100644 index 0000000..7ebaacc --- /dev/null +++ b/tests/auto/core5/text/qstringbuilder/qstringbuilder.pro @@ -0,0 +1,2 @@ +TEMPLATE=subdirs +SUBDIRS= qstringbuilder1 diff --git a/tests/auto/core5/text/qstringbuilder/qstringbuilder1/CMakeLists.txt b/tests/auto/core5/text/qstringbuilder/qstringbuilder1/CMakeLists.txt new file mode 100644 index 0000000..f4ca45e --- /dev/null +++ b/tests/auto/core5/text/qstringbuilder/qstringbuilder1/CMakeLists.txt @@ -0,0 +1,15 @@ +# Generated from qstringbuilder1.pro. + +##################################################################### +## tst_qstringbuilder1 Test: +##################################################################### + +qt_add_test(tst_qstringbuilder1 + SOURCES + tst_qstringbuilder1.cpp + DEFINES + QT_USE_FAST_OPERATOR_PLUS + QT_USE_QSTRINGBUILDER + PUBLIC_LIBRARIES + Qt::Core5Compat +) diff --git a/tests/auto/core5/text/qstringbuilder/qstringbuilder1/qstringbuilder1.pro b/tests/auto/core5/text/qstringbuilder/qstringbuilder1/qstringbuilder1.pro new file mode 100644 index 0000000..9394925 --- /dev/null +++ b/tests/auto/core5/text/qstringbuilder/qstringbuilder1/qstringbuilder1.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +TARGET = tst_qstringbuilder1 +QT = core testlib core5compat +SOURCES = tst_qstringbuilder1.cpp +DEFINES += QT_USE_FAST_OPERATOR_PLUS QT_USE_QSTRINGBUILDER diff --git a/tests/auto/core5/text/qstringbuilder/qstringbuilder1/stringbuilder.cpp b/tests/auto/core5/text/qstringbuilder/qstringbuilder1/stringbuilder.cpp new file mode 100644 index 0000000..30fd0df --- /dev/null +++ b/tests/auto/core5/text/qstringbuilder/qstringbuilder1/stringbuilder.cpp @@ -0,0 +1,363 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// Do not include anything in this file. We are being #included with +// a bunch of defines that may break other legitimate code. + +#define LITERAL "some literal" +#define LITERAL_LEN (sizeof(LITERAL)-1) +#define LITERAL_EXTRA "some literal" "EXTRA" + +// "some literal", but replacing all vowels by their umlauted UTF-8 string :) +#define UTF8_LITERAL "s\xc3\xb6m\xc3\xab l\xc3\xaft\xc3\xabr\xc3\xa4l" +#define UTF8_LITERAL_LEN (sizeof(UTF8_LITERAL)-1) +#define UTF8_LITERAL_EXTRA "s\xc3\xb6m\xc3\xab l\xc3\xaft\xc3\xabr\xc3\xa4l" "EXTRA" + +#ifdef Q_COMPILER_UNICODE_STRINGS +// "some literal", but replacing all vocals by their umlauted UTF-8 string :) +#define UNICODE_LITERAL u"s\u00f6m\u00eb l\u00eft\u00ebr\u00e4l" +#define UNICODE_LITERAL_LEN ((sizeof(UNICODE_LITERAL) - 1) / 2) +#define UNICODE_LITERAL_EXTRA u"s\u00f6m\u00eb l\u00eft\u00ebr\u00e4l" "EXTRA" +#endif + +#ifndef P +# error You need to define P +#endif + +//fix for gcc4.0: if the operator+ does not exist without QT_USE_FAST_OPERATOR_PLUS +#ifndef QT_USE_FAST_CONCATENATION +#define Q % +#else +#define Q P +#endif + +template QString toQString(const T &t); + +template <> QString toQString(const QString &s) { return s; } +template <> QString toQString(const QStringRef &r) { return r.toString(); } +template <> QString toQString(const QStringView &v) { return v.toString(); } +template <> QString toQString(const QLatin1String &l) { return l; } +template <> QString toQString(const QLatin1Char &l) { return QChar(l); } +template <> QString toQString(const QChar &c) { return c; } +template <> QString toQString(const QChar::SpecialCharacter &c) { return QChar(c); } +#ifdef Q_COMPILER_UNICODE_STRINGS +template <> QString toQString(char16_t * const &p) { return QStringView(p).toString(); } +template QString toQString(const char16_t (&a)[N]) { return QStringView(a).toString(); } +template <> QString toQString(const char16_t &c) { return QChar(c); } +#endif + +template QByteArray toQByteArray(const T &t); + +template <> QByteArray toQByteArray(const QByteArray &b) { return b; } +template <> QByteArray toQByteArray(char * const &p) { return p; } +template QByteArray toQByteArray(const char (&a)[N]) { return a; } +template <> QByteArray toQByteArray(const char &c) { return QByteArray(&c, 1); } + +void runScenario() +{ + // this code is latin1. TODO: replace it with the utf8 block below, once + // strings default to utf8. + QLatin1String l1string(LITERAL); + QString string(l1string); + QStringRef stringref(&string, 2, 10); + QStringView stringview(stringref); + QLatin1Char lchar('c'); + QChar qchar(lchar); + QChar::SpecialCharacter special(QChar::Nbsp); +#ifdef Q_COMPILER_UNICODE_STRINGS + char16_t u16char = UNICODE_LITERAL[0]; + char16_t u16chararray[] = { u's', 0xF6, u'm', 0xEB, u' ', u'l', 0xEF, u't', 0xEB, u'r', 0xE4, u'l', 0x00 }; + QCOMPARE(QStringView(u16chararray), QStringView(UNICODE_LITERAL)); + char16_t *u16charstar = u16chararray; +#endif + +#define CHECK(QorP, a1, a2) \ + do { \ + DO(QorP, a1, a2); \ + DO(QorP, a2, a1); \ + } while (0) + +#define DO(QorP, a1, a2) \ + QCOMPARE(QString(a1 QorP a2), \ + toQString(a1).append(toQString(a2))) \ + /* end */ + + CHECK(P, l1string, l1string); + CHECK(P, l1string, string); + CHECK(P, l1string, stringref); + CHECK(Q, l1string, stringview); + CHECK(P, l1string, lchar); + CHECK(P, l1string, qchar); + CHECK(P, l1string, special); + CHECK(P, l1string, QStringLiteral(LITERAL)); + CHECK(Q, l1string, u16char); + CHECK(Q, l1string, u16chararray); + CHECK(Q, l1string, u16charstar); + + CHECK(P, string, string); + CHECK(P, string, stringref); + CHECK(Q, string, stringview); + CHECK(P, string, lchar); + CHECK(P, string, qchar); + CHECK(P, string, special); + CHECK(P, string, QStringLiteral(LITERAL)); + CHECK(Q, string, u16char); + CHECK(Q, string, u16chararray); + CHECK(Q, string, u16charstar); + + CHECK(P, stringref, stringref); + CHECK(Q, stringref, stringview); + CHECK(P, stringref, lchar); + CHECK(P, stringref, qchar); + CHECK(P, stringref, special); + CHECK(P, stringref, QStringLiteral(LITERAL)); + CHECK(Q, stringref, u16char); + CHECK(Q, stringref, u16chararray); + CHECK(Q, stringref, u16charstar); + + CHECK(Q, stringview, stringview); + CHECK(Q, stringview, lchar); + CHECK(Q, stringview, qchar); + CHECK(Q, stringview, special); + CHECK(P, stringview, QStringLiteral(LITERAL)); + CHECK(Q, stringview, u16char); + CHECK(Q, stringview, u16chararray); + CHECK(Q, stringview, u16charstar); + + CHECK(P, lchar, lchar); + CHECK(P, lchar, qchar); + CHECK(P, lchar, special); + CHECK(P, lchar, QStringLiteral(LITERAL)); + CHECK(Q, lchar, u16char); + CHECK(Q, lchar, u16chararray); + CHECK(Q, lchar, u16charstar); + + CHECK(P, qchar, qchar); + CHECK(P, qchar, special); + CHECK(P, qchar, QStringLiteral(LITERAL)); + CHECK(Q, qchar, u16char); + CHECK(Q, qchar, u16chararray); + CHECK(Q, qchar, u16charstar); + + CHECK(P, special, special); + CHECK(P, special, QStringLiteral(LITERAL)); + CHECK(Q, special, u16char); + CHECK(Q, special, u16chararray); + CHECK(Q, special, u16charstar); + + CHECK(P, QStringLiteral(LITERAL), QStringLiteral(LITERAL)); + CHECK(Q, QStringLiteral(LITERAL), u16char); + CHECK(Q, QStringLiteral(LITERAL), u16chararray); + CHECK(Q, QStringLiteral(LITERAL), u16charstar); + + // CHECK(Q, u16char, u16char); // BUILTIN <-> BUILTIN cat't be overloaded + // CHECK(Q, u16char, u16chararray); + // CHECK(Q, u16char, u16charstar); + + // CHECK(Q, u16chararray, u16chararray); // BUILTIN <-> BUILTIN cat't be overloaded + // CHECK(Q, u16chararray, u16charstar); + + // CHECK(Q, u16charstar, u16charstar); // BUILTIN <-> BUILTIN cat't be overloaded + +#undef DO + +#define DO(QorP, a1, a2) \ + QCOMPARE(QByteArray(a1 QorP a2), \ + toQByteArray(a1).append(toQByteArray(a2))) \ + /* end */ + + QByteArray bytearray = stringref.toUtf8(); + char *charstar = bytearray.data(); + char chararray[3] = { 'H', 'i', '\0' }; + const char constchararray[3] = { 'H', 'i', '\0' }; + char achar = 'a'; + + CHECK(P, bytearray, bytearray); + CHECK(P, bytearray, charstar); +#ifndef Q_CC_MSVC // see QTBUG-65359 + CHECK(P, bytearray, chararray); +#else + Q_UNUSED(chararray); +#endif + CHECK(P, bytearray, constchararray); + CHECK(P, bytearray, achar); + + //CHECK(Q, charstar, charstar); // BUILTIN <-> BUILTIN cat't be overloaded + //CHECK(Q, charstar, chararray); + //CHECK(Q, charstar, achar); + + //CHECK(Q, chararray, chararray); // BUILTIN <-> BUILTIN cat't be overloaded + //CHECK(Q, chararray, achar); + + //CHECK(Q, achar, achar); // BUILTIN <-> BUILTIN cat't be overloaded + +#undef DO +#undef CHECK + + QString r2(QLatin1String(LITERAL LITERAL)); + QString r3 = QString::fromUtf8(UTF8_LITERAL UTF8_LITERAL); + QString r; + + // self-assignment: + r = stringref.toString(); + r = lchar + r; + QCOMPARE(r, QString(lchar P stringref)); + +#ifdef Q_COMPILER_UNICODE_STRINGS + r = QStringLiteral(UNICODE_LITERAL); + r = r Q QStringLiteral(UNICODE_LITERAL); + QCOMPARE(r, r3); +#endif + +#ifndef QT_NO_CAST_FROM_ASCII + r = string P LITERAL; + QCOMPARE(r, r2); + r = LITERAL P string; + QCOMPARE(r, r2); + + QByteArray ba = QByteArray(LITERAL); + r = ba P string; + QCOMPARE(r, r2); + r = string P ba; + QCOMPARE(r, r2); + + r = string P QByteArrayLiteral(LITERAL); + QCOMPARE(r, r2); + r = QByteArrayLiteral(LITERAL) P string; + QCOMPARE(r, r2); + + static const char badata[] = LITERAL_EXTRA; + ba = QByteArray::fromRawData(badata, LITERAL_LEN); + r = ba P string; + QCOMPARE(r, r2); + r = string P ba; + QCOMPARE(r, r2); + + string = QString::fromUtf8(UTF8_LITERAL); + ba = UTF8_LITERAL; + + r = string P UTF8_LITERAL; + QCOMPARE(r.size(), r3.size()); + QCOMPARE(r, r3); + r = UTF8_LITERAL P string; + QCOMPARE(r, r3); + r = ba P string; + QCOMPARE(r, r3); + r = string P ba; + QCOMPARE(r, r3); + + ba = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); + r = ba P string; + QCOMPARE(r, r3); + r = string P ba; + QCOMPARE(r, r3); + + ba = QByteArray(); // empty + r = ba P string; + QCOMPARE(r, string); + r = string P ba; + QCOMPARE(r, string); + + const char *zero = 0; + r = string P zero; + QCOMPARE(r, string); + r = zero P string; + QCOMPARE(r, string); +#endif + + string = QString::fromLatin1(LITERAL); + QCOMPARE(QByteArray(qPrintable(string P string)), QByteArray(string.toLatin1() + string.toLatin1())); + + + + //QByteArray + { + QByteArray ba = LITERAL; + QByteArray superba = ba P ba P LITERAL; + QCOMPARE(superba, QByteArray(LITERAL LITERAL LITERAL)); + + ba = QByteArrayLiteral(LITERAL); + QCOMPARE(ba, QByteArray(LITERAL)); + superba = ba P QByteArrayLiteral(LITERAL) P LITERAL; + QCOMPARE(superba, QByteArray(LITERAL LITERAL LITERAL)); + + QByteArray testWith0 = ba P "test\0with\0zero" P ba; + QCOMPARE(testWith0, QByteArray(LITERAL "test" LITERAL)); + + QByteArray ba2 = ba P '\0' + LITERAL; + QCOMPARE(ba2, QByteArray(LITERAL "\0" LITERAL, ba.size()*2+1)); + + const char *mmh = "test\0foo"; + QCOMPARE(QByteArray(ba P mmh P ba), testWith0); + + QByteArray raw = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); + QByteArray r = "hello" P raw; + QByteArray r2 = "hello" UTF8_LITERAL; + QCOMPARE(r, r2); + r2 = QByteArray("hello\0") P UTF8_LITERAL; + QCOMPARE(r, r2); + + const char *zero = 0; + r = ba P zero; + QCOMPARE(r, ba); + r = zero P ba; + QCOMPARE(r, ba); + } + + //operator QString += + { + QString str = QString::fromUtf8(UTF8_LITERAL); + str += QLatin1String(LITERAL) P str; + QCOMPARE(str, QString::fromUtf8(UTF8_LITERAL LITERAL UTF8_LITERAL)); +#ifndef QT_NO_CAST_FROM_ASCII + str = (QString::fromUtf8(UTF8_LITERAL) += QLatin1String(LITERAL) P UTF8_LITERAL); + QCOMPARE(str, QString::fromUtf8(UTF8_LITERAL LITERAL UTF8_LITERAL)); +#endif + + QString str2 = QString::fromUtf8(UTF8_LITERAL); + QString str2_e = QString::fromUtf8(UTF8_LITERAL); + const char * nullData = 0; + str2 += QLatin1String(nullData) P str2; + str2_e += QLatin1String("") P str2_e; + QCOMPARE(str2, str2_e); + } + + //operator QByteArray += + { + QByteArray ba = UTF8_LITERAL; + ba += QByteArray(LITERAL) P UTF8_LITERAL; + QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL)); + ba += LITERAL P QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); + QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL LITERAL UTF8_LITERAL)); + QByteArray withZero = QByteArray(LITERAL "\0" LITERAL, LITERAL_LEN*2+1); + QByteArray ba2 = withZero; + ba2 += ba2 P withZero; + QCOMPARE(ba2, QByteArray(withZero + withZero + withZero)); + } + +} diff --git a/tests/auto/core5/text/qstringbuilder/qstringbuilder1/tst_qstringbuilder1.cpp b/tests/auto/core5/text/qstringbuilder/qstringbuilder1/tst_qstringbuilder1.cpp new file mode 100644 index 0000000..9140e57 --- /dev/null +++ b/tests/auto/core5/text/qstringbuilder/qstringbuilder1/tst_qstringbuilder1.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +// SCENARIO 1 +// this is the "no harm done" version. Only operator% is active, +// with NO_CAST * defined +#undef QT_USE_QSTRINGBUILDER +#define QT_NO_CAST_FROM_ASCII +#define QT_NO_CAST_TO_ASCII + +#include +#include +#include +#include +#include + +#define LITERAL "some literal" + +void runScenario(); // Defined in stringbuilder.cpp #included below. + +class tst_QStringBuilder1 : public QObject +{ + Q_OBJECT + +private slots: + void scenario() { runScenario(); } +}; + +#define P % +#include "stringbuilder.cpp" +#undef P + +#include "tst_qstringbuilder1.moc" + +QTEST_APPLESS_MAIN(tst_QStringBuilder1) diff --git a/tests/auto/core5/text/qstringref/CMakeLists.txt b/tests/auto/core5/text/qstringref/CMakeLists.txt new file mode 100644 index 0000000..03c4b40 --- /dev/null +++ b/tests/auto/core5/text/qstringref/CMakeLists.txt @@ -0,0 +1,12 @@ +# Generated from qstringref.pro. + +##################################################################### +## tst_qstringref Test: +##################################################################### + +qt_add_test(tst_qstringref + SOURCES + tst_qstringref.cpp + PUBLIC_LIBRARIES + Qt::Core5Compat +) diff --git a/tests/auto/core5/text/qstringref/qstringref.pro b/tests/auto/core5/text/qstringref/qstringref.pro new file mode 100644 index 0000000..342b509 --- /dev/null +++ b/tests/auto/core5/text/qstringref/qstringref.pro @@ -0,0 +1,4 @@ +CONFIG += testcase +TARGET = tst_qstringref +QT = core core5compat testlib +SOURCES = tst_qstringref.cpp diff --git a/tests/auto/core5/text/qstringref/tst_qstringref.cpp b/tests/auto/core5/text/qstringref/tst_qstringref.cpp new file mode 100644 index 0000000..59b5db79 --- /dev/null +++ b/tests/auto/core5/text/qstringref/tst_qstringref.cpp @@ -0,0 +1,2186 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2016 Intel Corporation. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +#include +#include +#include + +class tst_QStringRef : public QObject +{ + Q_OBJECT +public slots: + void cleanup(); +private slots: + void at(); + void endsWith(); + void startsWith(); + void contains(); + void count(); + void lastIndexOf_data(); + void lastIndexOf(); + void indexOf_data(); + void indexOf(); + void indexOf2_data(); + void indexOf2(); + void iteration(); + void length_data(); + void length(); + void isEmpty(); + void compare_data(); + void compare(); + void compare2_data(); + void compare2(); + void operator_eqeq_nullstring(); + void toNum(); + void toDouble_data(); + void toDouble(); + void toFloat(); + void toLong_data(); + void toLong(); + void toULong_data(); + void toULong(); + void toLongLong(); + void toULongLong(); + void toUInt(); + void toInt(); + void toShort(); + void toUShort(); + void double_conversion_data(); + void double_conversion(); + void integer_conversion_data(); + void integer_conversion(); + void trimmed(); + void truncate(); + void chop(); + void left(); + void right(); + void mid(); + void split_data(); + void split(); + void nullToString(); + void qHashFunction(); +}; + +static QStringRef emptyRef() +{ + static const QString empty(""); + return QStringRef(&empty); +} + +#define CREATE_REF(string) \ + const QString padded = QLatin1Char(' ') + string + QLatin1Char(' '); \ + QStringRef ref = QStringRef(&padded, 1, padded.size() - 2); + +typedef QList IntList; + +// This next bit is needed for the NAN and INF in string -> number conversion tests +#include +#include +#include +#if defined(Q_OS_WIN) +# include +// mingw defines NAN and INFINITY to 0/0 and x/0 +# if defined(Q_CC_GNU) +# undef NAN +# undef INFINITY +# else +# define isnan(d) _isnan(d) +# endif +#endif +#if defined(Q_OS_MACOS) && !defined isnan +#define isnan(d) __isnand(d) +#endif +#if defined(Q_OS_SOLARIS) +# include +#endif + +enum { + LittleEndian, + BigEndian +#ifdef Q_BYTE_ORDER +# if Q_BYTE_ORDER == Q_BIG_ENDIAN + , ByteOrder = BigEndian +# elif Q_BYTE_ORDER == Q_LITTLE_ENDIAN + , ByteOrder = LittleEndian +# else +# error "undefined byte order" +# endif +}; +#else +}; +static const unsigned int one = 1; +static const bool ByteOrder = ((*((unsigned char *) &one) == 0) ? BigEndian : LittleEndian); +#endif +#if !defined(INFINITY) +static const unsigned char be_inf_bytes[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0,0 }; +static const unsigned char le_inf_bytes[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f }; +static inline double inf() +{ + if (ByteOrder == BigEndian) + return *reinterpret_cast(be_inf_bytes); + return *reinterpret_cast(le_inf_bytes); +} +# define INFINITY (::inf()) +#endif +#if !defined(NAN) +static const unsigned char be_nan_bytes[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0,0 }; +static const unsigned char le_nan_bytes[] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f }; +static inline double nan() +{ + if (ByteOrder == BigEndian) + return *reinterpret_cast(be_nan_bytes); + return *reinterpret_cast(le_nan_bytes); +} +# define NAN (::nan()) +#endif + +void tst_QStringRef::cleanup() +{ + QLocale::setDefault(QLocale(QString(QLatin1Char('C')))); +} + +void tst_QStringRef::at() +{ + const QString hw = QStringLiteral("Hello World"); + const QStringRef ref = QStringRef(&hw).mid(6); + QCOMPARE(ref.at(0), QChar('W')); + QCOMPARE(ref.at(4), QChar('d')); + QCOMPARE(ref[0], QChar('W')); + QCOMPARE(ref[4], QChar('d')); +} + +void tst_QStringRef::length_data() +{ + QTest::addColumn("s1"); + QTest::addColumn("res"); + + QTest::newRow("data0") << QString("Test") << 4; + QTest::newRow("data1") << QString("The quick brown fox jumps over the lazy dog") << 43; + QTest::newRow("data2") << QString() << 0; + QTest::newRow("data3") << QString("A") << 1; + QTest::newRow("data4") << QString("AB") << 2; + QTest::newRow("data5") << QString("AB\n") << 3; + QTest::newRow("data6") << QString("AB\nC") << 4; + QTest::newRow("data7") << QString("\n") << 1; + QTest::newRow("data8") << QString("\nA") << 2; + QTest::newRow("data9") << QString("\nAB") << 3; + QTest::newRow("data10") << QString("\nAB\nCDE") << 7; + QTest::newRow("data11") << QString("shdnftrheid fhgnt gjvnfmd chfugkh bnfhg thgjf vnghturkf chfnguh bjgnfhvygh hnbhgutjfv dhdnjds dcjs d") << 100; + QTest::newRow("data12") << QString("") << 0; +} + + +void tst_QStringRef::length() +{ + QFETCH(QString, s1); + CREATE_REF(s1); + QTEST(ref.length(), "res"); +} + + +void tst_QStringRef::isEmpty() +{ + QStringRef a; + QVERIFY(a.isEmpty()); + QVERIFY(emptyRef().isEmpty()); + CREATE_REF("Not empty"); + QVERIFY(!ref.isEmpty()); +} + +void tst_QStringRef::indexOf_data() +{ + QTest::addColumn("haystack"); + QTest::addColumn("needle"); + QTest::addColumn("startpos"); + QTest::addColumn("bcs"); + QTest::addColumn("resultpos"); + + QTest::newRow("data0") << QString("abc") << QString("a") << 0 << true << 0; + QTest::newRow("data1") << QString("abc") << QString("a") << 0 << false << 0; + QTest::newRow("data2") << QString("abc") << QString("A") << 0 << true << -1; + QTest::newRow("data3") << QString("abc") << QString("A") << 0 << false << 0; + QTest::newRow("data4") << QString("abc") << QString("a") << 1 << true << -1; + QTest::newRow("data5") << QString("abc") << QString("a") << 1 << false << -1; + QTest::newRow("data6") << QString("abc") << QString("A") << 1 << true << -1; + QTest::newRow("data7") << QString("abc") << QString("A") << 1 << false << -1; + QTest::newRow("data8") << QString("abc") << QString("b") << 0 << true << 1; + QTest::newRow("data9") << QString("abc") << QString("b") << 0 << false << 1; + QTest::newRow("data10") << QString("abc") << QString("B") << 0 << true << -1; + QTest::newRow("data11") << QString("abc") << QString("B") << 0 << false << 1; + QTest::newRow("data12") << QString("abc") << QString("b") << 1 << true << 1; + QTest::newRow("data13") << QString("abc") << QString("b") << 1 << false << 1; + QTest::newRow("data14") << QString("abc") << QString("B") << 1 << true << -1; + QTest::newRow("data15") << QString("abc") << QString("B") << 1 << false << 1; + QTest::newRow("data16") << QString("abc") << QString("b") << 2 << true << -1; + QTest::newRow("data17") << QString("abc") << QString("b") << 2 << false << -1; + + QTest::newRow("data20") << QString("ABC") << QString("A") << 0 << true << 0; + QTest::newRow("data21") << QString("ABC") << QString("A") << 0 << false << 0; + QTest::newRow("data22") << QString("ABC") << QString("a") << 0 << true << -1; + QTest::newRow("data23") << QString("ABC") << QString("a") << 0 << false << 0; + QTest::newRow("data24") << QString("ABC") << QString("A") << 1 << true << -1; + QTest::newRow("data25") << QString("ABC") << QString("A") << 1 << false << -1; + QTest::newRow("data26") << QString("ABC") << QString("a") << 1 << true << -1; + QTest::newRow("data27") << QString("ABC") << QString("a") << 1 << false << -1; + QTest::newRow("data28") << QString("ABC") << QString("B") << 0 << true << 1; + QTest::newRow("data29") << QString("ABC") << QString("B") << 0 << false << 1; + QTest::newRow("data30") << QString("ABC") << QString("b") << 0 << true << -1; + QTest::newRow("data31") << QString("ABC") << QString("b") << 0 << false << 1; + QTest::newRow("data32") << QString("ABC") << QString("B") << 1 << true << 1; + QTest::newRow("data33") << QString("ABC") << QString("B") << 1 << false << 1; + QTest::newRow("data34") << QString("ABC") << QString("b") << 1 << true << -1; + QTest::newRow("data35") << QString("ABC") << QString("b") << 1 << false << 1; + QTest::newRow("data36") << QString("ABC") << QString("B") << 2 << true << -1; + QTest::newRow("data37") << QString("ABC") << QString("B") << 2 << false << -1; + + QTest::newRow("data40") << QString("aBc") << QString("bc") << 0 << true << -1; + QTest::newRow("data41") << QString("aBc") << QString("Bc") << 0 << true << 1; + QTest::newRow("data42") << QString("aBc") << QString("bC") << 0 << true << -1; + QTest::newRow("data43") << QString("aBc") << QString("BC") << 0 << true << -1; + QTest::newRow("data44") << QString("aBc") << QString("bc") << 0 << false << 1; + QTest::newRow("data45") << QString("aBc") << QString("Bc") << 0 << false << 1; + QTest::newRow("data46") << QString("aBc") << QString("bC") << 0 << false << 1; + QTest::newRow("data47") << QString("aBc") << QString("BC") << 0 << false << 1; + QTest::newRow("data48") << QString("AbC") << QString("bc") << 0 << true << -1; + QTest::newRow("data49") << QString("AbC") << QString("Bc") << 0 << true << -1; + QTest::newRow("data50") << QString("AbC") << QString("bC") << 0 << true << 1; + QTest::newRow("data51") << QString("AbC") << QString("BC") << 0 << true << -1; + QTest::newRow("data52") << QString("AbC") << QString("bc") << 0 << false << 1; + QTest::newRow("data53") << QString("AbC") << QString("Bc") << 0 << false << 1; + + QTest::newRow("data54") << QString("AbC") << QString("bC") << 0 << false << 1; + QTest::newRow("data55") << QString("AbC") << QString("BC") << 0 << false << 1; + QTest::newRow("data56") << QString("AbC") << QString("BC") << 1 << false << 1; + QTest::newRow("data57") << QString("AbC") << QString("BC") << 2 << false << -1; +#if 0 + QTest::newRow("null-in-null") << QString() << QString() << 0 << false << 0; + QTest::newRow("empty-in-null") << QString() << QString("") << 0 << false << 0; + QTest::newRow("null-in-empty") << QString("") << QString() << 0 << false << 0; + QTest::newRow("empty-in-empty") << QString("") << QString("") << 0 << false << 0; +#endif + + + QString s1 = "abc"; + s1 += QChar(0xb5); + QString s2; + s2 += QChar(0x3bc); + QTest::newRow("data58") << QString(s1) << QString(s2) << 0 << false << 3; + s2.prepend(QLatin1Char('C')); + QTest::newRow("data59") << QString(s1) << QString(s2) << 0 << false << 2; + + QString veryBigHaystack(500, 'a'); + veryBigHaystack += 'B'; + QTest::newRow("BoyerMooreStressTest") << veryBigHaystack << veryBigHaystack << 0 << true << 0; + QTest::newRow("BoyerMooreStressTest2") << veryBigHaystack + 'c' << veryBigHaystack << 0 << true << 0; + QTest::newRow("BoyerMooreStressTest3") << 'c' + veryBigHaystack << veryBigHaystack << 0 << true << 1; + QTest::newRow("BoyerMooreStressTest4") << veryBigHaystack << veryBigHaystack + 'c' << 0 << true << -1; + QTest::newRow("BoyerMooreStressTest5") << veryBigHaystack << 'c' + veryBigHaystack << 0 << true << -1; + QTest::newRow("BoyerMooreStressTest6") << 'd' + veryBigHaystack << 'c' + veryBigHaystack << 0 << true << -1; + QTest::newRow("BoyerMooreStressTest7") << veryBigHaystack + 'c' << 'c' + veryBigHaystack << 0 << true << -1; + + QTest::newRow("BoyerMooreInsensitiveStressTest") << veryBigHaystack << veryBigHaystack << 0 << false << 0; + +} + +void tst_QStringRef::indexOf() +{ + QFETCH(QString, haystack); + QFETCH(QString, needle); + QFETCH(int, startpos); + QFETCH(bool, bcs); + QFETCH(int, resultpos); + + const QString haystackPadded = QLatin1Char(' ') + haystack + QLatin1Char(' '); + const QString needlePadded = QLatin1Char(' ') + needle + QLatin1Char(' '); + const QStringRef haystackRef(&haystackPadded, 1, haystack.size()); + const QStringRef needleRef(&needlePadded, 1, needle.size()); + + Qt::CaseSensitivity cs = bcs ? Qt::CaseSensitive : Qt::CaseInsensitive; + + QCOMPARE(haystack.indexOf(needle, startpos, cs), resultpos); + QCOMPARE(haystackRef.indexOf(needle, startpos, cs), resultpos); + QCOMPARE(haystackRef.indexOf(needleRef, startpos, cs), resultpos); + QCOMPARE(haystack.indexOf(needleRef, startpos, cs), resultpos); + + if (cs == Qt::CaseSensitive) { + QCOMPARE(haystack.indexOf(needle, startpos), resultpos); + QCOMPARE(haystackRef.indexOf(needle, startpos), resultpos); + QCOMPARE(haystackRef.indexOf(needleRef, startpos), resultpos); + QCOMPARE(haystack.indexOf(needleRef, startpos), resultpos); + if (startpos == 0) { + QCOMPARE(haystack.indexOf(needle), resultpos); + QCOMPARE(haystackRef.indexOf(needle), resultpos); + QCOMPARE(haystackRef.indexOf(needleRef), resultpos); + QCOMPARE(haystack.indexOf(needleRef), resultpos); + } + } + if (needle.size() == 1) { + QCOMPARE(needle.at(0), needleRef.at(0)); + QCOMPARE(haystack.indexOf(needleRef.at(0), startpos, cs), resultpos); + QCOMPARE(haystackRef.indexOf(needle.at(0), startpos, cs), resultpos); + QCOMPARE(haystackRef.indexOf(needleRef.at(0), startpos, cs), resultpos); + QCOMPARE(haystack.indexOf(needleRef.at(0), startpos ,cs), resultpos); + } +} + +void tst_QStringRef::indexOf2_data() +{ + QTest::addColumn("haystack"); + QTest::addColumn("needle"); + QTest::addColumn("resultpos"); + + QTest::newRow("data0") << QString() << QString() << 0; + QTest::newRow("data1") << QString() << QString("") << 0; + QTest::newRow("data2") << QString("") << QString() << 0; + QTest::newRow("data3") << QString("") << QString("") << 0; + QTest::newRow("data4") << QString() << QString("a") << -1; + QTest::newRow("data5") << QString() << QString("abcdefg") << -1; + QTest::newRow("data6") << QString("") << QString("a") << -1; + QTest::newRow("data7") << QString("") << QString("abcdefg") << -1; + + QTest::newRow("data8") << QString("a") << QString() << 0; + QTest::newRow("data9") << QString("a") << QString("") << 0; + QTest::newRow("data10") << QString("a") << QString("a") << 0; + QTest::newRow("data11") << QString("a") << QString("b") << -1; + QTest::newRow("data12") << QString("a") << QString("abcdefg") << -1; + QTest::newRow("data13") << QString("ab") << QString() << 0; + QTest::newRow("data14") << QString("ab") << QString("") << 0; + QTest::newRow("data15") << QString("ab") << QString("a") << 0; + QTest::newRow("data16") << QString("ab") << QString("b") << 1; + QTest::newRow("data17") << QString("ab") << QString("ab") << 0; + QTest::newRow("data18") << QString("ab") << QString("bc") << -1; + QTest::newRow("data19") << QString("ab") << QString("abcdefg") << -1; + + QTest::newRow("data30") << QString("abc") << QString("a") << 0; + QTest::newRow("data31") << QString("abc") << QString("b") << 1; + QTest::newRow("data32") << QString("abc") << QString("c") << 2; + QTest::newRow("data33") << QString("abc") << QString("d") << -1; + QTest::newRow("data34") << QString("abc") << QString("ab") << 0; + QTest::newRow("data35") << QString("abc") << QString("bc") << 1; + QTest::newRow("data36") << QString("abc") << QString("cd") << -1; + QTest::newRow("data37") << QString("abc") << QString("ac") << -1; + + // sizeof(whale) > 32 + QString whale = "a5zby6cx7dw8evf9ug0th1si2rj3qkp4lomn"; + QString minnow = "zby"; + QTest::newRow("data40") << whale << minnow << 2; + QTest::newRow("data41") << (whale + whale) << minnow << 2; + QTest::newRow("data42") << (minnow + whale) << minnow << 0; + QTest::newRow("data43") << whale << whale << 0; + QTest::newRow("data44") << (whale + whale) << whale << 0; + QTest::newRow("data45") << whale << (whale + whale) << -1; + QTest::newRow("data46") << (whale + whale) << (whale + whale) << 0; + QTest::newRow("data47") << (whale + whale) << (whale + minnow) << -1; + QTest::newRow("data48") << (minnow + whale) << whale << (int)minnow.length(); +} + +void tst_QStringRef::indexOf2() +{ + QFETCH(QString, haystack); + QFETCH(QString, needle); + QFETCH(int, resultpos); + + const QString haystackPadded = QLatin1Char(' ') + haystack + QLatin1Char(' '); + const QString needlePadded = QLatin1Char(' ') + needle + QLatin1Char(' '); + const QStringRef haystackRef(&haystackPadded, 1, haystack.size()); + const QStringRef needleRef(&needlePadded, 1, needle.size()); + + + int got; + + QCOMPARE(haystack.indexOf(needleRef, 0, Qt::CaseSensitive), resultpos); + QCOMPARE(haystackRef.indexOf(needle, 0, Qt::CaseSensitive), resultpos); + QCOMPARE(haystackRef.indexOf(needleRef, 0, Qt::CaseSensitive), resultpos); + QCOMPARE(haystack.indexOf(needleRef, 0, Qt::CaseInsensitive), resultpos); + QCOMPARE(haystackRef.indexOf(needle, 0, Qt::CaseInsensitive), resultpos); + QCOMPARE(haystackRef.indexOf(needleRef, 0, Qt::CaseInsensitive), resultpos); + if (needle.length() > 0) { + got = haystackRef.lastIndexOf(needle, -1, Qt::CaseSensitive); + QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); + got = haystackRef.lastIndexOf(needle, -1, Qt::CaseInsensitive); + QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); + + got = haystack.lastIndexOf(needleRef, -1, Qt::CaseSensitive); + QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); + got = haystack.lastIndexOf(needleRef, -1, Qt::CaseInsensitive); + QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); + + got = haystackRef.lastIndexOf(needleRef, -1, Qt::CaseSensitive); + QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); + got = haystackRef.lastIndexOf(needleRef, -1, Qt::CaseInsensitive); + QVERIFY(got == resultpos || (resultpos >= 0 && got >= resultpos)); + } +} + +void tst_QStringRef::iteration() +{ + QString hello = "Hello"; + QString olleh = "olleH"; + + QStringRef ref(&hello); + QStringRef rref(&olleh); + + const QStringRef &cref = ref; + const QStringRef &crref = rref; + + QVERIFY(std::equal( ref.begin(), ref.end(), hello.begin())); + QVERIFY(std::equal( rref.begin(), rref.end(), olleh.begin())); + QVERIFY(std::equal( cref.begin(), cref.end(), hello.begin())); + QVERIFY(std::equal(crref.begin(), crref.end(), olleh.begin())); + + QVERIFY(std::equal( ref.cbegin(), ref.cend(), hello.begin())); + QVERIFY(std::equal( rref.cbegin(), rref.cend(), olleh.begin())); + QVERIFY(std::equal( cref.cbegin(), cref.cend(), hello.begin())); + QVERIFY(std::equal(crref.cbegin(), crref.cend(), olleh.begin())); + + QVERIFY(std::equal( ref.rbegin(), ref.rend(), hello.rbegin())); + QVERIFY(std::equal( rref.rbegin(), rref.rend(), olleh.rbegin())); + QVERIFY(std::equal( cref.rbegin(), cref.rend(), hello.rbegin())); + QVERIFY(std::equal(crref.rbegin(), crref.rend(), olleh.rbegin())); + + QVERIFY(std::equal( ref.crbegin(), ref.crend(), hello.rbegin())); + QVERIFY(std::equal( rref.crbegin(), rref.crend(), olleh.rbegin())); + QVERIFY(std::equal( cref.crbegin(), cref.crend(), hello.rbegin())); + QVERIFY(std::equal(crref.crbegin(), crref.crend(), olleh.rbegin())); +} + +void tst_QStringRef::lastIndexOf_data() +{ + QTest::addColumn("haystack"); + QTest::addColumn("needle"); + QTest::addColumn("from"); + QTest::addColumn("expected"); + QTest::addColumn("caseSensitive"); + + QString a = "ABCDEFGHIEfGEFG"; + + QTest::newRow("-1") << a << "G" << int(a.size()) - 1 << 14 << true; + QTest::newRow("1") << a << "G" << - 1 << 14 << true; + QTest::newRow("2") << a << "G" << -3 << 11 << true; + QTest::newRow("3") << a << "G" << -5 << 6 << true; + QTest::newRow("4") << a << "G" << 14 << 14 << true; + QTest::newRow("5") << a << "G" << 13 << 11 << true; + QTest::newRow("6") << a << "B" << int(a.size()) - 1 << 1 << true; + QTest::newRow("7") << a << "B" << - 1 << 1 << true; + QTest::newRow("8") << a << "B" << 1 << 1 << true; + QTest::newRow("9") << a << "B" << 0 << -1 << true; + + QTest::newRow("10") << a << "G" << -1 << int(a.size())-1 << true; + QTest::newRow("11") << a << "G" << int(a.size())-1 << int(a.size())-1 << true; + QTest::newRow("12") << a << "G" << int(a.size()) << -1 << true; + QTest::newRow("13") << a << "A" << 0 << 0 << true; + QTest::newRow("14") << a << "A" << -1*int(a.size()) << 0 << true; + + QTest::newRow("15") << a << "efg" << 0 << -1 << false; + QTest::newRow("16") << a << "efg" << int(a.size()) << -1 << false; + QTest::newRow("17") << a << "efg" << -1 * int(a.size()) << -1 << false; + QTest::newRow("19") << a << "efg" << int(a.size()) - 1 << 12 << false; + QTest::newRow("20") << a << "efg" << 12 << 12 << false; + QTest::newRow("21") << a << "efg" << -12 << -1 << false; + QTest::newRow("22") << a << "efg" << 11 << 9 << false; + + QTest::newRow("24") << "" << "asdf" << -1 << -1 << false; + QTest::newRow("25") << "asd" << "asdf" << -1 << -1 << false; + QTest::newRow("26") << "" << QString() << -1 << -1 << false; + + QTest::newRow("27") << a << "" << int(a.size()) << int(a.size()) << false; + QTest::newRow("28") << a << "" << int(a.size()) + 10 << -1 << false; +} + +void tst_QStringRef::lastIndexOf() +{ + QFETCH(QString, haystack); + QFETCH(QString, needle); + QFETCH(int, from); + QFETCH(int, expected); + QFETCH(bool, caseSensitive); + + const QString haystackPadded = QLatin1Char(' ') + haystack + QLatin1Char(' '); + const QString needlePadded = QLatin1Char(' ') + needle + QLatin1Char(' '); + const QStringRef haystackRef(&haystackPadded, 1, haystack.size()); + const QStringRef needleRef(&needlePadded, 1, needle.size()); + + Qt::CaseSensitivity cs = (caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); + + QCOMPARE(haystack.lastIndexOf(needleRef, from, cs), expected); + QCOMPARE(haystackRef.lastIndexOf(needle, from, cs), expected); + QCOMPARE(haystackRef.lastIndexOf(needleRef, from, cs), expected); + + + if (cs == Qt::CaseSensitive) { + QCOMPARE(haystack.lastIndexOf(needleRef, from), expected); + QCOMPARE(haystackRef.lastIndexOf(needle, from), expected); + QCOMPARE(haystackRef.lastIndexOf(needleRef, from), expected); + + if (from == -1) { + QCOMPARE(haystack.lastIndexOf(needleRef), expected); + QCOMPARE(haystackRef.lastIndexOf(needle), expected); + QCOMPARE(haystackRef.lastIndexOf(needleRef), expected); + + } + } + if (needle.size() == 1) { + QCOMPARE(haystack.lastIndexOf(needleRef.at(0), from), expected); + QCOMPARE(haystackRef.lastIndexOf(needle.at(0), from), expected); + QCOMPARE(haystackRef.lastIndexOf(needleRef.at(0), from), expected); + } +} + +void tst_QStringRef::count() +{ + const QString a = QString::fromLatin1("ABCDEFGHIEfGEFG"); // 15 chars + CREATE_REF(a); + QCOMPARE(ref.count('A'),1); + QCOMPARE(ref.count('Z'),0); + QCOMPARE(ref.count('E'),3); + QCOMPARE(ref.count('F'),2); + QCOMPARE(ref.count('F',Qt::CaseInsensitive),3); + QCOMPARE(ref.count("FG"),2); + QCOMPARE(ref.count("FG",Qt::CaseInsensitive),3); + QCOMPARE(ref.count(QString(), Qt::CaseInsensitive), 16); + QCOMPARE(ref.count("", Qt::CaseInsensitive), 16); +} + +void tst_QStringRef::contains() +{ + const QString a = QString::fromLatin1("ABCDEFGHIEfGEFG"); // 15 chars + CREATE_REF(a); + QVERIFY(ref.contains('A')); + QVERIFY(!ref.contains('Z')); + QVERIFY(ref.contains('E')); + QVERIFY(ref.contains('F')); + QVERIFY(ref.contains('F',Qt::CaseInsensitive)); + QVERIFY(ref.contains("FG")); + const QString fg("FG"); + QVERIFY(ref.contains(QStringRef(&fg))); + const QString ref2 = QString::fromLatin1(" FG "); + QVERIFY(ref.contains(QStringRef(&ref2, 1, 2),Qt::CaseInsensitive)); + QVERIFY(ref.contains(QString(), Qt::CaseInsensitive)); + QVERIFY(ref.contains("", Qt::CaseInsensitive)); // apparently +} + +void tst_QStringRef::startsWith() +{ + { + const QString a = QString::fromLatin1("AB"); + CREATE_REF(a); + QVERIFY(ref.startsWith("A")); + QVERIFY(ref.startsWith("AB")); + QVERIFY(!ref.startsWith("C")); + QVERIFY(!ref.startsWith("ABCDEF")); + QVERIFY(ref.startsWith("")); + QVERIFY(ref.startsWith(QString())); + QVERIFY(ref.startsWith('A')); + QVERIFY(ref.startsWith(QLatin1Char('A'))); + QVERIFY(ref.startsWith(QChar('A'))); + QVERIFY(!ref.startsWith('C')); + QVERIFY(!ref.startsWith(QChar())); + QVERIFY(!ref.startsWith(QLatin1Char(0))); + + QVERIFY(ref.startsWith(QLatin1String("A"))); + QVERIFY(ref.startsWith(QLatin1String("AB"))); + QVERIFY(!ref.startsWith(QLatin1String("C"))); + QVERIFY(!ref.startsWith(QLatin1String("ABCDEF"))); + QVERIFY(ref.startsWith(QLatin1String(""))); + QVERIFY(ref.startsWith(QLatin1String(0))); + + QVERIFY(ref.startsWith("A", Qt::CaseSensitive)); + QVERIFY(ref.startsWith("A", Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith("a", Qt::CaseSensitive)); + QVERIFY(ref.startsWith("a", Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith("aB", Qt::CaseSensitive)); + QVERIFY(ref.startsWith("aB", Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith("C", Qt::CaseSensitive)); + QVERIFY(!ref.startsWith("C", Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith("c", Qt::CaseSensitive)); + QVERIFY(!ref.startsWith("c", Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith("abcdef", Qt::CaseInsensitive)); + QVERIFY(ref.startsWith("", Qt::CaseInsensitive)); + QVERIFY(ref.startsWith(QString(), Qt::CaseInsensitive)); + QVERIFY(ref.startsWith('a', Qt::CaseInsensitive)); + QVERIFY(ref.startsWith('A', Qt::CaseInsensitive)); + QVERIFY(ref.startsWith(QLatin1Char('a'), Qt::CaseInsensitive)); + QVERIFY(ref.startsWith(QChar('a'), Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith('c', Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith(QChar(), Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith(QLatin1Char(0), Qt::CaseInsensitive)); + + QVERIFY(ref.startsWith(QLatin1String("A"), Qt::CaseSensitive)); + QVERIFY(ref.startsWith(QLatin1String("A"), Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith(QLatin1String("a"), Qt::CaseSensitive)); + QVERIFY(ref.startsWith(QLatin1String("a"), Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith(QLatin1String("aB"), Qt::CaseSensitive)); + QVERIFY(ref.startsWith(QLatin1String("aB"), Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith(QLatin1String("C"), Qt::CaseSensitive)); + QVERIFY(!ref.startsWith(QLatin1String("C"), Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith(QLatin1String("c"), Qt::CaseSensitive)); + QVERIFY(!ref.startsWith(QLatin1String("c"), Qt::CaseInsensitive)); + QVERIFY(!ref.startsWith(QLatin1String("abcdef"), Qt::CaseInsensitive)); + QVERIFY(ref.startsWith(QLatin1String(""), Qt::CaseInsensitive)); + QVERIFY(ref.startsWith(QLatin1String(0), Qt::CaseInsensitive)); + QVERIFY(ref.startsWith('A', Qt::CaseSensitive)); + QVERIFY(ref.startsWith(QLatin1Char('A'), Qt::CaseSensitive)); + QVERIFY(ref.startsWith(QChar('A'), Qt::CaseSensitive)); + QVERIFY(!ref.startsWith('a', Qt::CaseSensitive)); + QVERIFY(!ref.startsWith(QChar(), Qt::CaseSensitive)); + QVERIFY(!ref.startsWith(QLatin1Char(0), Qt::CaseSensitive)); + } + { + const QString a = QString::fromLatin1(""); + CREATE_REF(a); + QVERIFY(ref.startsWith("")); + QVERIFY(ref.startsWith(QString())); + QVERIFY(!ref.startsWith("ABC")); + + QVERIFY(ref.startsWith(QLatin1String(""))); + QVERIFY(ref.startsWith(QLatin1String(0))); + QVERIFY(!ref.startsWith(QLatin1String("ABC"))); + + QVERIFY(!ref.startsWith(QLatin1Char(0))); + QVERIFY(!ref.startsWith(QLatin1Char('x'))); + QVERIFY(!ref.startsWith(QChar())); + } + { + const QStringRef ref; + QVERIFY(!ref.startsWith("")); + QVERIFY(ref.startsWith(QString())); + QVERIFY(!ref.startsWith("ABC")); + + QVERIFY(!ref.startsWith(QLatin1String(""))); + QVERIFY(ref.startsWith(QLatin1String(0))); + QVERIFY(!ref.startsWith(QLatin1String("ABC"))); + + QVERIFY(!ref.startsWith(QLatin1Char(0))); + QVERIFY(!ref.startsWith(QLatin1Char('x'))); + QVERIFY(!ref.startsWith(QChar())); + } +} + +void tst_QStringRef::endsWith() +{ + { + const QString a = QString::fromLatin1("AB"); + CREATE_REF(a); + QVERIFY(ref.endsWith("B")); + QVERIFY(ref.endsWith("AB")); + QVERIFY(!ref.endsWith("C")); + QVERIFY(!ref.endsWith("ABCDEF")); + QVERIFY(ref.endsWith("")); + QVERIFY(ref.endsWith(QString())); + QVERIFY(ref.endsWith('B')); + QVERIFY(ref.endsWith(QLatin1Char('B'))); + QVERIFY(ref.endsWith(QChar('B'))); + QVERIFY(!ref.endsWith('C')); + QVERIFY(!ref.endsWith(QChar())); + QVERIFY(!ref.endsWith(QLatin1Char(0))); + + QVERIFY(ref.endsWith(QLatin1String("B"))); + QVERIFY(ref.endsWith(QLatin1String("AB"))); + QVERIFY(!ref.endsWith(QLatin1String("C"))); + QVERIFY(!ref.endsWith(QLatin1String("ABCDEF"))); + QVERIFY(ref.endsWith(QLatin1String(""))); + QVERIFY(ref.endsWith(QLatin1String(0))); + + QVERIFY(ref.endsWith("B", Qt::CaseSensitive)); + QVERIFY(ref.endsWith("B", Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith("b", Qt::CaseSensitive)); + QVERIFY(ref.endsWith("b", Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith("aB", Qt::CaseSensitive)); + QVERIFY(ref.endsWith("aB", Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith("C", Qt::CaseSensitive)); + QVERIFY(!ref.endsWith("C", Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith("c", Qt::CaseSensitive)); + QVERIFY(!ref.endsWith("c", Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith("abcdef", Qt::CaseInsensitive)); + QVERIFY(ref.endsWith("", Qt::CaseInsensitive)); + QVERIFY(ref.endsWith(QString(), Qt::CaseInsensitive)); + QVERIFY(ref.endsWith('b', Qt::CaseInsensitive)); + QVERIFY(ref.endsWith('B', Qt::CaseInsensitive)); + QVERIFY(ref.endsWith(QLatin1Char('b'), Qt::CaseInsensitive)); + QVERIFY(ref.endsWith(QChar('b'), Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith('c', Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith(QChar(), Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith(QLatin1Char(0), Qt::CaseInsensitive)); + + QVERIFY(ref.endsWith(QLatin1String("B"), Qt::CaseSensitive)); + QVERIFY(ref.endsWith(QLatin1String("B"), Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith(QLatin1String("b"), Qt::CaseSensitive)); + QVERIFY(ref.endsWith(QLatin1String("b"), Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith(QLatin1String("aB"), Qt::CaseSensitive)); + QVERIFY(ref.endsWith(QLatin1String("aB"), Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith(QLatin1String("C"), Qt::CaseSensitive)); + QVERIFY(!ref.endsWith(QLatin1String("C"), Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith(QLatin1String("c"), Qt::CaseSensitive)); + QVERIFY(!ref.endsWith(QLatin1String("c"), Qt::CaseInsensitive)); + QVERIFY(!ref.endsWith(QLatin1String("abcdef"), Qt::CaseInsensitive)); + QVERIFY(ref.endsWith(QLatin1String(""), Qt::CaseInsensitive)); + QVERIFY(ref.endsWith(QLatin1String(0), Qt::CaseInsensitive)); + QVERIFY(ref.endsWith('B', Qt::CaseSensitive)); + QVERIFY(ref.endsWith(QLatin1Char('B'), Qt::CaseSensitive)); + QVERIFY(ref.endsWith(QChar('B'), Qt::CaseSensitive)); + QVERIFY(!ref.endsWith('b', Qt::CaseSensitive)); + QVERIFY(!ref.endsWith(QChar(), Qt::CaseSensitive)); + QVERIFY(!ref.endsWith(QLatin1Char(0), Qt::CaseSensitive)); + + } + { + const QString a = QString::fromLatin1(""); + CREATE_REF(a); + QVERIFY(ref.endsWith("")); + QVERIFY(ref.endsWith(QString())); + QVERIFY(!ref.endsWith("ABC")); + QVERIFY(!ref.endsWith(QLatin1Char(0))); + QVERIFY(!ref.endsWith(QLatin1Char('x'))); + QVERIFY(!ref.endsWith(QChar())); + + QVERIFY(ref.endsWith(QLatin1String(""))); + QVERIFY(ref.endsWith(QLatin1String(0))); + QVERIFY(!ref.endsWith(QLatin1String("ABC"))); + } + + { + QStringRef ref; + QVERIFY(!ref.endsWith("")); + QVERIFY(ref.endsWith(QString())); + QVERIFY(!ref.endsWith("ABC")); + + QVERIFY(!ref.endsWith(QLatin1String(""))); + QVERIFY(ref.endsWith(QLatin1String(0))); + QVERIFY(!ref.endsWith(QLatin1String("ABC"))); + + QVERIFY(!ref.endsWith(QLatin1Char(0))); + QVERIFY(!ref.endsWith(QLatin1Char('x'))); + QVERIFY(!ref.endsWith(QChar())); + } +} + +void tst_QStringRef::operator_eqeq_nullstring() +{ + /* Some of these might not be all that logical but it's the behavior we've had since 3.0.0 + so we should probably stick with it. */ + + QVERIFY(QStringRef() == ""); + QVERIFY("" == QStringRef()); + + QVERIFY(QString("") == ""); + QVERIFY("" == QString("")); + + QVERIFY(QStringRef().size() == 0); + + QVERIFY(QString("").size() == 0); + + QVERIFY(QStringRef() == QString("")); + QVERIFY(QString("") == QString()); +} + +static inline int sign(int x) +{ + return x == 0 ? 0 : (x < 0 ? -1 : 1); +} + +void tst_QStringRef::compare_data() +{ + QTest::addColumn("s1"); + QTest::addColumn("s2"); + QTest::addColumn("csr"); // case sensitive result + QTest::addColumn("cir"); // case insensitive result + + + // null strings + QTest::newRow("data0") << QString("") << QString("") << 0 << 0; + QTest::newRow("data1") << QString("a") << QString("") << 1 << 1; + QTest::newRow("data2") << QString("") << QString("a") << -1 << -1; + + // equal length + QTest::newRow("data3") << QString("abc") << QString("abc") << 0 << 0; + QTest::newRow("data4") << QString("abC") << QString("abc") << -1 << 0; + QTest::newRow("data5") << QString("abc") << QString("abC") << 1 << 0; + QTest::newRow("data10") << QString("abcdefgh") << QString("abcdefgh") << 0 << 0; + QTest::newRow("data11") << QString("abcdefgh") << QString("abCdefgh") << 1 << 0; + QTest::newRow("data12") << QString("0123456789012345") << QString("0123456789012345") << 0 << 0; + QTest::newRow("data13") << QString("0123556789012345") << QString("0123456789012345") << 1 << 1; + + // different length + QTest::newRow("data6") << QString("abcdef") << QString("abc") << 1 << 1; + QTest::newRow("data7") << QString("abCdef") << QString("abc") << -1 << 1; + QTest::newRow("data8") << QString("abc") << QString("abcdef") << -1 << -1; + QTest::newRow("data14") << QString("abcdefgh") << QString("abcdefghi") << -1 << -1; + QTest::newRow("data15") << QString("01234567890123456") << QString("0123456789012345") << 1 << 1; + + QString upper; + upper += QChar(QChar::highSurrogate(0x10400)); + upper += QChar(QChar::lowSurrogate(0x10400)); + QString lower; + lower += QChar(QChar::highSurrogate(0x10428)); + lower += QChar(QChar::lowSurrogate(0x10428)); + QTest::newRow("data9") << upper << lower << -1 << 0; +} + +static bool isLatin(const QString &s) +{ + for (int i = 0; i < s.length(); ++i) + if (s.at(i).unicode() > 0xff) + return false; + return true; +} + +void tst_QStringRef::compare() +{ + QFETCH(QString, s1); + QFETCH(QString, s2); + QFETCH(int, csr); + QFETCH(int, cir); + + QStringRef r1(&s1, 0, s1.length()); + QStringRef r2(&s2, 0, s2.length()); + + QCOMPARE(sign(QString::compare(s1, s2)), csr); + QCOMPARE(sign(QStringRef::compare(r1, r2)), csr); + QCOMPARE(sign(s1.compare(s2)), csr); + QCOMPARE(sign(s1.compare(r2)), csr); + QCOMPARE(sign(r1.compare(r2)), csr); + + QCOMPARE(sign(s1.compare(s2, Qt::CaseSensitive)), csr); + QCOMPARE(sign(s1.compare(s2, Qt::CaseInsensitive)), cir); + QCOMPARE(sign(s1.compare(r2, Qt::CaseSensitive)), csr); + QCOMPARE(sign(s1.compare(r2, Qt::CaseInsensitive)), cir); + QCOMPARE(sign(r1.compare(r2, Qt::CaseSensitive)), csr); + QCOMPARE(sign(r1.compare(r2, Qt::CaseInsensitive)), cir); + + QCOMPARE(sign(QString::compare(s1, s2, Qt::CaseSensitive)), csr); + QCOMPARE(sign(QString::compare(s1, s2, Qt::CaseInsensitive)), cir); + QCOMPARE(sign(QString::compare(s1, r2, Qt::CaseSensitive)), csr); + QCOMPARE(sign(QString::compare(s1, r2, Qt::CaseInsensitive)), cir); + QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseSensitive)), csr); + QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseInsensitive)), cir); + + if (!cir) { + QCOMPARE(s1.toCaseFolded(), s2.toCaseFolded()); + } + + if (isLatin(s2)) { + QCOMPARE(sign(QString::compare(s1, QLatin1String(s2.toLatin1()))), csr); + QCOMPARE(sign(QString::compare(s1, QLatin1String(s2.toLatin1()), Qt::CaseInsensitive)), cir); + QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(s2.toLatin1()))), csr); + QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(s2.toLatin1()), Qt::CaseInsensitive)), cir); + } + + if (isLatin(s1)) { + QCOMPARE(sign(QString::compare(QLatin1String(s1.toLatin1()), s2)), csr); + QCOMPARE(sign(QString::compare(QLatin1String(s1.toLatin1()), s2, Qt::CaseInsensitive)), cir); + } +} + +void tst_QStringRef::compare2_data() +{ + compare_data(); +} + +void tst_QStringRef::compare2() +{ + QFETCH(QString, s1); + QFETCH(QString, s2); + QFETCH(int, csr); + QFETCH(int, cir); + + // prepend and append data + // we only use Latin1 here so isLatin1 still results true + s1.prepend("xyz").append("zyx"); + s2.prepend("foobar").append("raboof"); + + QStringRef r1(&s1, 3, s1.length() - 6); + QStringRef r2(&s2, 6, s2.length() - 12); + + QCOMPARE(sign(QStringRef::compare(r1, r2)), csr); + QCOMPARE(sign(r1.compare(r2)), csr); + + QCOMPARE(sign(r1.compare(r2, Qt::CaseSensitive)), csr); + QCOMPARE(sign(r1.compare(r2, Qt::CaseInsensitive)), cir); + + QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseSensitive)), csr); + QCOMPARE(sign(QStringRef::compare(r1, r2, Qt::CaseInsensitive)), cir); + + if (isLatin(s2)) { + QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(r2.toLatin1()))), csr); + QCOMPARE(sign(QStringRef::compare(r1, QLatin1String(r2.toLatin1()), Qt::CaseInsensitive)), cir); + } + + if (isLatin(s1)) { + QCOMPARE(sign(QStringRef::compare(r2, QLatin1String(r1.toLatin1()))), -csr); + QCOMPARE(sign(QStringRef::compare(r2, QLatin1String(r1.toLatin1()), Qt::CaseInsensitive)), -cir); + } +} + +void tst_QStringRef::toNum() +{ +#define TEST_TO_INT(num, func, type) \ + a = #num; \ + b = QStringRef(&a); \ + QCOMPARE(b.func(&ok), type(Q_INT64_C(num))); \ + QVERIFY2(ok, "Failed: num=" #num); + + QString a; + QStringRef b; + bool ok = false; + + TEST_TO_INT(0, toInt, int) + TEST_TO_INT(-1, toInt, int) + TEST_TO_INT(1, toInt, int) + TEST_TO_INT(2147483647, toInt, int) + TEST_TO_INT(-2147483648, toInt, int) + + TEST_TO_INT(0, toShort, short) + TEST_TO_INT(-1, toShort, short) + TEST_TO_INT(1, toShort, short) + TEST_TO_INT(32767, toShort, short) + TEST_TO_INT(-32768, toShort, short) + + TEST_TO_INT(0, toLong, long) + TEST_TO_INT(-1, toLong, long) + TEST_TO_INT(1, toLong, long) + TEST_TO_INT(2147483647, toLong, long) + TEST_TO_INT(-2147483648, toLong, long) + TEST_TO_INT(0, toLongLong, (long long)) + TEST_TO_INT(-1, toLongLong, (long long)) + TEST_TO_INT(1, toLongLong, (long long)) + TEST_TO_INT(9223372036854775807, toLongLong, (long long)) + TEST_TO_INT(-9223372036854775807, toLongLong, (long long)) + +#undef TEST_TO_INT + +#define TEST_TO_UINT(num, func, type) \ + a = #num; \ + b = QStringRef(&a); \ + QCOMPARE(b.func(&ok), type(Q_UINT64_C(num))); \ + QVERIFY2(ok, "Failed: num=" #num); + + TEST_TO_UINT(0, toUInt, (unsigned int)) + TEST_TO_UINT(1, toUInt, (unsigned int)) + TEST_TO_UINT(4294967295, toUInt, (unsigned int)) + + TEST_TO_UINT(0, toUShort, (unsigned short)) + TEST_TO_UINT(1, toUShort, (unsigned short)) + TEST_TO_UINT(65535, toUShort, (unsigned short)) + + TEST_TO_UINT(0, toULong, (unsigned long)) + TEST_TO_UINT(1, toULong, (unsigned long)) + TEST_TO_UINT(4294967295, toULong, (unsigned long)) + + TEST_TO_UINT(0, toULongLong, (unsigned long long)) + TEST_TO_UINT(1, toULongLong, (unsigned long long)) + TEST_TO_UINT(18446744073709551615, toULongLong, (unsigned long long)) + +#undef TEST_TO_UINT + +#define TEST_BASE(str, base, num) \ + a = str; \ + b = QStringRef(&a); \ + QCOMPARE(b.toInt(&ok,base), int(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toInt"); \ + QCOMPARE(b.toUInt(&ok, base), (unsigned int)(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toUInt"); \ + QCOMPARE(b.toShort(&ok, base), short(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toShort"); \ + QCOMPARE(b.toUShort(&ok, base), (unsigned short)(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toUShort"); \ + QCOMPARE(b.toLong(&ok, base), long(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toLong"); \ + QCOMPARE(b.toULong(&ok, base), (unsigned long)(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toULong"); \ + QCOMPARE(b.toLongLong(&ok, base), (long long)(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toLongLong"); \ + QCOMPARE(b.toULongLong(&ok, base), (unsigned long long)(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= " #base " num=" #num ", func=toULongLong"); + + TEST_BASE("FF", 16, 255) + TEST_BASE("0xFF", 16, 255) + TEST_BASE("77", 8, 63) + TEST_BASE("077", 8, 63) + + TEST_BASE("0xFF", 0, 255) + TEST_BASE("077", 0, 63) + TEST_BASE("255", 0, 255) + + TEST_BASE(" FF", 16, 255) + TEST_BASE(" 0xFF", 16, 255) + TEST_BASE(" 77", 8, 63) + TEST_BASE(" 077", 8, 63) + + TEST_BASE(" 0xFF", 0, 255) + TEST_BASE(" 077", 0, 63) + TEST_BASE(" 255", 0, 255) + + TEST_BASE("\tFF\t", 16, 255) + TEST_BASE("\t0xFF ", 16, 255) + TEST_BASE(" 77 ", 8, 63) + TEST_BASE("77 ", 8, 63) + +#undef TEST_BASE + +#define TEST_NEG_BASE(str, base, num) \ + a = str; \ + b = QStringRef(&a); \ + QCOMPARE(b.toInt(&ok, base), int(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toInt"); \ + QCOMPARE(b.toShort(&ok,base), short(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toShort"); \ + QCOMPARE(b.toLong(&ok, base), long(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toLong"); \ + QCOMPARE(b.toLongLong(&ok, base), (long long)(num)); \ + QVERIFY2(ok, "Failed: str=" #str " base= "#base " num=" #num ", func=toLongLong"); + + TEST_NEG_BASE("-FE", 16, -254) + TEST_NEG_BASE("-0xFE", 16, -254) + TEST_NEG_BASE("-77", 8, -63) + TEST_NEG_BASE("-077", 8, -63) + + TEST_NEG_BASE("-0xFE", 0, -254) + TEST_NEG_BASE("-077", 0, -63) + TEST_NEG_BASE("-254", 0, -254) + +#undef TEST_NEG_BASE + +#define TEST_DOUBLE(num, str) \ + a = str; \ + b = QStringRef(&a); \ + QCOMPARE(b.toDouble(&ok), num); \ + QVERIFY(ok); + + TEST_DOUBLE(1.2345, "1.2345") + TEST_DOUBLE(12.345, "1.2345e+01") + TEST_DOUBLE(12.345, "1.2345E+01") + TEST_DOUBLE(12345.6, "12345.6") + +#undef TEST_DOUBLE + +#define TEST_BAD(str, func) \ + a = str; \ + b = QStringRef(&a); \ + b.func(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str " func=" #func); + + TEST_BAD("32768", toShort) + TEST_BAD("-32769", toShort) + TEST_BAD("65536", toUShort) + TEST_BAD("2147483648", toInt) + TEST_BAD("-2147483649", toInt) + TEST_BAD("4294967296", toUInt) + if (sizeof(long) == 4) { + TEST_BAD("2147483648", toLong) + TEST_BAD("-2147483649", toLong) + TEST_BAD("4294967296", toULong) + } + TEST_BAD("9223372036854775808", toLongLong) + TEST_BAD("-9223372036854775809", toLongLong) + TEST_BAD("18446744073709551616", toULongLong) + TEST_BAD("-1", toUShort) + TEST_BAD("-1", toUInt) + TEST_BAD("-1", toULong) + TEST_BAD("-1", toULongLong) + +#undef TEST_BAD + +#define TEST_BAD_ALL(str) \ + a = str; \ + b = QStringRef(&a); \ + b.toShort(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); \ + b.toUShort(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); \ + b.toInt(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); \ + b.toUInt(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); \ + b.toLong(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); \ + b.toULong(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); \ + b.toLongLong(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); \ + b.toULongLong(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); \ + b.toFloat(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); \ + b.toDouble(&ok); \ + QVERIFY2(!ok, "Failed: str=" #str); + + TEST_BAD_ALL((const char*)0); + TEST_BAD_ALL(""); + TEST_BAD_ALL(" "); + TEST_BAD_ALL("."); + TEST_BAD_ALL("-"); + TEST_BAD_ALL("hello"); + TEST_BAD_ALL("1.2.3"); + TEST_BAD_ALL("0x0x0x"); + TEST_BAD_ALL("123-^~<"); + TEST_BAD_ALL("123ThisIsNotANumber"); + +#undef TEST_BAD_ALL + + a = "FF"; + b = QStringRef(&a); + b.toULongLong(&ok, 10); + QVERIFY(!ok); + + a = "FF"; + b = QStringRef(&a); + b.toULongLong(&ok, 0); + QVERIFY(!ok); + +#ifdef QT_NO_FPU + double d = 3.40282346638528e+38; // slightly off FLT_MAX when using hardfloats +#else + double d = 3.4028234663852886e+38; // FLT_MAX +#endif + auto num = QString::number(d, 'e', 17); + QStringRef(&num).toFloat(&ok); + QVERIFY(ok); + num = QString::number(d + 1e32, 'e', 17); + QStringRef(&num).toFloat(&ok); + QVERIFY(!ok); + num = QString::number(-d, 'e', 17); + QStringRef(&num).toFloat(&ok); + QVERIFY(ok); + num = QString::number(-d - 1e32, 'e', 17); + QStringRef(&num).toFloat(&ok); + QVERIFY(!ok); + num = QString::number(d + 1e32, 'e', 17); + QStringRef(&num).toDouble(&ok); + QVERIFY(ok); + num = QString::number(-d - 1e32, 'e', 17); + QStringRef(&num).toDouble(&ok); + QVERIFY(ok); +} + +void tst_QStringRef::toUShort() +{ + QString a; + QStringRef b; + bool ok; + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = ""; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = "COMPARE"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = "123"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(123)); + QCOMPARE(b.toUShort(&ok), ushort(123)); + QVERIFY(ok); + + a = "123A"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = "1234567"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = "aaa123aaa"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = "aaa123"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = "123aaa"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = "32767"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(32767)); + QCOMPARE(b.toUShort(&ok), ushort(32767)); + QVERIFY(ok); + + a = "-32767"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = "65535"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(65535)); + QCOMPARE(b.toUShort(&ok), ushort(65535)); + QVERIFY(ok); + + if (sizeof(short) == 2) { + a = "65536"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + + a = "123456"; + b = QStringRef(&a); + QCOMPARE(b.toUShort(), ushort(0)); + QCOMPARE(b.toUShort(&ok), ushort(0)); + QVERIFY(!ok); + } +} + +void tst_QStringRef::toShort() +{ + QString a; + QStringRef b; + bool ok; + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + + a = ""; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + + a = "COMPARE"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + + a = "123"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(123)); + QCOMPARE(b.toShort(&ok), short(123)); + QVERIFY(ok); + + a = "123A"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + + a = "1234567"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + + a = "aaa123aaa"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + + a = "aaa123"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + + a = "123aaa"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + + a = "32767"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(32767)); + QCOMPARE(b.toShort(&ok), short(32767)); + QVERIFY(ok); + + a = "-32767"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(-32767)); + QCOMPARE(b.toShort(&ok), short(-32767)); + QVERIFY(ok); + + a = "-32768"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(-32768)); + QCOMPARE(b.toShort(&ok), short(-32768)); + QVERIFY(ok); + + if (sizeof(short) == 2) { + a = "32768"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + + a = "-32769"; + b = QStringRef(&a); + QCOMPARE(b.toShort(), short(0)); + QCOMPARE(b.toShort(&ok), short(0)); + QVERIFY(!ok); + } +} + +void tst_QStringRef::toInt() +{ + QString a; + QStringRef b; + bool ok; + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = ""; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = "COMPARE"; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = "123"; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 123); + QCOMPARE(b.toInt(&ok), 123); + QVERIFY(ok); + + a = "123A"; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = "1234567"; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 1234567); + QCOMPARE(b.toInt(&ok), 1234567); + QVERIFY(ok); + + a = "12345678901234"; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = "3234567890"; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = "aaa12345aaa"; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = "aaa12345"; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = "12345aaa"; + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = "2147483647"; // 2**31 - 1 + b = QStringRef(&a); + QCOMPARE(b.toInt(), 2147483647); + QCOMPARE(b.toInt(&ok), 2147483647); + QVERIFY(ok); + + if (sizeof(int) == 4) { + a = "-2147483647"; // -(2**31 - 1) + b = QStringRef(&a); + QCOMPARE(b.toInt(), -2147483647); + QCOMPARE(b.toInt(&ok), -2147483647); + QVERIFY(ok); + + a = "2147483648"; // 2**31 + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + + a = "-2147483648"; // -2**31 + b = QStringRef(&a); + QCOMPARE(b.toInt(), -2147483647 - 1); + QCOMPARE(b.toInt(&ok), -2147483647 - 1); + QVERIFY(ok); + + a = "2147483649"; // 2**31 + 1 + b = QStringRef(&a); + QCOMPARE(b.toInt(), 0); + QCOMPARE(b.toInt(&ok), 0); + QVERIFY(!ok); + } +} + +void tst_QStringRef::toUInt() +{ + bool ok; + QString a; + QStringRef b; + a = "3234567890"; + b = QStringRef(&a); + QCOMPARE(b.toUInt(&ok), 3234567890u); + QVERIFY(ok); + + a = "-50"; + b = QStringRef(&a); + QCOMPARE(b.toUInt(), 0u); + QCOMPARE(b.toUInt(&ok), 0u); + QVERIFY(!ok); + + a = "4294967295"; // 2**32 - 1 + b = QStringRef(&a); + QCOMPARE(b.toUInt(), 4294967295u); + QCOMPARE(b.toUInt(&ok), 4294967295u); + QVERIFY(ok); + + if (sizeof(int) == 4) { + a = "4294967296"; // 2**32 + b = QStringRef(&a); + QCOMPARE(b.toUInt(), 0u); + QCOMPARE(b.toUInt(&ok), 0u); + QVERIFY(!ok); + } +} + +///////////////////////////// to*Long ////////////////////////////////////// + +void tst_QStringRef::toULong_data() +{ + QTest::addColumn("str"); + QTest::addColumn("base"); + QTest::addColumn("result"); + QTest::addColumn("ok"); + + QTest::newRow("default") << QString() << 10 << 0UL << false; + QTest::newRow("empty") << QString("") << 10 << 0UL << false; + QTest::newRow("ulong1") << QString("3234567890") << 10 << 3234567890UL << true; + QTest::newRow("ulong2") << QString("fFFfFfFf") << 16 << 0xFFFFFFFFUL << true; +} + +void tst_QStringRef::toULong() +{ + QFETCH(QString, str); + QFETCH(int, base); + QFETCH(ulong, result); + QFETCH(bool, ok); + QStringRef strRef = QStringRef(&str); + + bool b; + QCOMPARE(strRef.toULong(0, base), result); + QCOMPARE(strRef.toULong(&b, base), result); + QCOMPARE(b, ok); +} + +void tst_QStringRef::toLong_data() +{ + QTest::addColumn("str"); + QTest::addColumn("base"); + QTest::addColumn("result"); + QTest::addColumn("ok"); + + QTest::newRow("default") << QString() << 10 << 0L << false; + QTest::newRow("empty") << QString("") << 10 << 0L << false; + QTest::newRow("normal") << QString("7fFFfFFf") << 16 << 0x7fFFfFFfL << true; + QTest::newRow("long_max") << QString("2147483647") << 10 << 2147483647L << true; + if (sizeof(long) == 4) { + QTest::newRow("long_max+1") << QString("2147483648") << 10 << 0L << false; + QTest::newRow("long_min-1") << QString("-80000001") << 16 << 0L << false; + } + QTest::newRow("negative") << QString("-7fffffff") << 16 << -0x7fffffffL << true; +// QTest::newRow("long_min") << QString("-80000000") << 16 << 0x80000000uL << true; +} + +void tst_QStringRef::toLong() +{ + QFETCH(QString, str); + QFETCH(int, base); + QFETCH(long, result); + QFETCH(bool, ok); + QStringRef strRef = QStringRef(&str); + + bool b; + QCOMPARE(strRef.toLong(0, base), result); + QCOMPARE(strRef.toLong(&b, base), result); + QCOMPARE(b, ok); +} + + +////////////////////////// to*LongLong ////////////////////////////////////// + +void tst_QStringRef::toULongLong() +{ + QString str; + QStringRef strRef; + bool ok; + str = "18446744073709551615"; // ULLONG_MAX + strRef = QStringRef(&str); + QCOMPARE(strRef.toULongLong(0), Q_UINT64_C(18446744073709551615)); + QCOMPARE(strRef.toULongLong(&ok), Q_UINT64_C(18446744073709551615)); + QVERIFY(ok); + + str = "18446744073709551616"; // ULLONG_MAX + 1 + strRef = QStringRef(&str); + QCOMPARE(strRef.toULongLong(0), Q_UINT64_C(0)); + QCOMPARE(strRef.toULongLong(&ok), Q_UINT64_C(0)); + QVERIFY(!ok); + + str = "-150"; + strRef = QStringRef(&str); + QCOMPARE(strRef.toULongLong(0), Q_UINT64_C(0)); + QCOMPARE(strRef.toULongLong(&ok), Q_UINT64_C(0)); + QVERIFY(!ok); +} + +void tst_QStringRef::toLongLong() +{ + QString str; + QStringRef strRef; + bool ok; + + str = "9223372036854775807"; // LLONG_MAX + strRef = QStringRef(&str); + QCOMPARE(strRef.toLongLong(0), Q_INT64_C(9223372036854775807)); + QCOMPARE(strRef.toLongLong(&ok), Q_INT64_C(9223372036854775807)); + QVERIFY(ok); + + str = "-9223372036854775808"; // LLONG_MIN + strRef = QStringRef(&str); + QCOMPARE(strRef.toLongLong(0), + -Q_INT64_C(9223372036854775807) - Q_INT64_C(1)); + QCOMPARE(strRef.toLongLong(&ok), + -Q_INT64_C(9223372036854775807) - Q_INT64_C(1)); + QVERIFY(ok); + + str = "aaaa9223372036854775807aaaa"; + strRef = QStringRef(&str); + QCOMPARE(strRef.toLongLong(0), Q_INT64_C(0)); + QCOMPARE(strRef.toLongLong(&ok), Q_INT64_C(0)); + QVERIFY(!ok); + + str = "9223372036854775807aaaa"; + strRef = QStringRef(&str); + QCOMPARE(strRef.toLongLong(0), Q_INT64_C(0)); + QCOMPARE(strRef.toLongLong(&ok), Q_INT64_C(0)); + QVERIFY(!ok); + + str = "aaaa9223372036854775807"; + strRef = QStringRef(&str); + QCOMPARE(strRef.toLongLong(0), Q_INT64_C(0)); + QCOMPARE(strRef.toLongLong(&ok), Q_INT64_C(0)); + QVERIFY(!ok); + + static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + for (int i = 0; i < 36; ++i) { + for (int j = 0; j < 36; ++j) { + for (int k = 0; k < 36; ++k) { + QString str; + str += QChar(digits[i]); + str += QChar(digits[j]); + str += QChar(digits[k]); + strRef = QStringRef(&str); + qlonglong value = (((i * 36) + j) * 36) + k; + QVERIFY(strRef.toLongLong(0, 36) == value); + } + } + } +} + +//////////////////////////////////////////////////////////////////////////// + +void tst_QStringRef::toFloat() +{ + QString a; + QStringRef b; + bool ok; + a = "0.000000000931322574615478515625"; + b = QStringRef(&a); + QCOMPARE(b.toFloat(&ok), float(0.000000000931322574615478515625)); + QVERIFY(ok); +} + +void tst_QStringRef::toDouble_data() +{ + QTest::addColumn("str"); + QTest::addColumn("result"); + QTest::addColumn("result_ok"); + + QTest::newRow("ok00") << QString("0.000000000931322574615478515625") << 0.000000000931322574615478515625 << true; + QTest::newRow("ok01") << QString(" 123.45") << 123.45 << true; + + QTest::newRow("ok02") << QString("0.1e10") << 0.1e10 << true; + QTest::newRow("ok03") << QString("0.1e-10") << 0.1e-10 << true; + + QTest::newRow("ok04") << QString("1e10") << 1.0e10 << true; + QTest::newRow("ok05") << QString("1e+10") << 1.0e10 << true; + QTest::newRow("ok06") << QString("1e-10") << 1.0e-10 << true; + + QTest::newRow("ok07") << QString(" 1e10") << 1.0e10 << true; + QTest::newRow("ok08") << QString(" 1e+10") << 1.0e10 << true; + QTest::newRow("ok09") << QString(" 1e-10") << 1.0e-10 << true; + + QTest::newRow("ok10") << QString("1.") << 1.0 << true; + QTest::newRow("ok11") << QString(".1") << 0.1 << true; + + QTest::newRow("wrong00") << QString("123.45 ") << 123.45 << true; + QTest::newRow("wrong01") << QString(" 123.45 ") << 123.45 << true; + + QTest::newRow("wrong02") << QString("aa123.45aa") << 0.0 << false; + QTest::newRow("wrong03") << QString("123.45aa") << 0.0 << false; + QTest::newRow("wrong04") << QString("123erf") << 0.0 << false; + + QTest::newRow("wrong05") << QString("abc") << 0.0 << false; + QTest::newRow("wrong06") << QString() << 0.0 << false; + QTest::newRow("wrong07") << QString("") << 0.0 << false; +} + +void tst_QStringRef::toDouble() +{ + QFETCH(QString, str); + QFETCH(bool, result_ok); + QStringRef strRef = QStringRef(&str); + bool ok; + double d = strRef.toDouble(&ok); + if (result_ok) { + QTEST(d, "result"); + QVERIFY(ok); + } else { + QVERIFY(!ok); + } +} + +void tst_QStringRef::integer_conversion_data() +{ + QTest::addColumn("num_str"); + QTest::addColumn("base"); + QTest::addColumn("good"); + QTest::addColumn("num"); + + QTest::newRow("C empty 0") << QString("") << 0 << false << (qlonglong)0; + QTest::newRow("C empty 8") << QString("") << 8 << false << (qlonglong)0; + QTest::newRow("C empty 10") << QString("") << 10 << false << (qlonglong)0; + QTest::newRow("C empty 16") << QString("") << 16 << false << (qlonglong)0; + + QTest::newRow("C null 0") << QString() << 0 << false << (qlonglong)0; + QTest::newRow("C null 8") << QString() << 8 << false << (qlonglong)0; + QTest::newRow("C null 10") << QString() << 10 << false << (qlonglong)0; + QTest::newRow("C null 16") << QString() << 16 << false << (qlonglong)0; + + QTest::newRow("C -0xf 0") << QString(" -0xf") << 0 << true << (qlonglong)-15; + QTest::newRow("C -0xf 0") << QString("-0xf ") << 0 << true << (qlonglong)-15; + QTest::newRow("C \t0xf\t 0") << QString("\t0xf\t") << 0 << true << (qlonglong)15; + QTest::newRow("C -010 0") << QString(" -010") << 0 << true << (qlonglong)-8; + QTest::newRow("C 010 0") << QString("010 ") << 0 << true << (qlonglong)8; + QTest::newRow("C \t-010\t 0") << QString("\t-010\t") << 0 << true << (qlonglong)-8; + QTest::newRow("C 123 10") << QString(" 123") << 10 << true << (qlonglong)123; + QTest::newRow("C 123 10") << QString("123 ") << 10 << true << (qlonglong)123; + QTest::newRow("C \t123\t 10") << QString("\t123\t") << 10 << true << (qlonglong)123; + QTest::newRow("C -0xf 16") << QString(" -0xf") << 16 << true << (qlonglong)-15; + QTest::newRow("C -0xf 16") << QString("-0xf ") << 16 << true << (qlonglong)-15; + QTest::newRow("C \t0xf\t 16") << QString("\t0xf\t") << 16 << true << (qlonglong)15; + + QTest::newRow("C -0 0") << QString("-0") << 0 << true << (qlonglong)0; + QTest::newRow("C -0 8") << QString("-0") << 8 << true << (qlonglong)0; + QTest::newRow("C -0 10") << QString("-0") << 10 << true << (qlonglong)0; + QTest::newRow("C -0 16") << QString("-0") << 16 << true << (qlonglong)0; + + QTest::newRow("C 1.234 10") << QString("1.234") << 10 << false << (qlonglong)0; + QTest::newRow("C 1,234 10") << QString("1,234") << 10 << false << (qlonglong)0; + + QTest::newRow("C 0x 0") << QString("0x") << 0 << false << (qlonglong)0; + QTest::newRow("C 0x 16") << QString("0x") << 16 << false << (qlonglong)0; + + QTest::newRow("C 10 0") << QString("10") << 0 << true << (qlonglong)10; + QTest::newRow("C 010 0") << QString("010") << 0 << true << (qlonglong)8; + QTest::newRow("C 0x10 0") << QString("0x10") << 0 << true << (qlonglong)16; + QTest::newRow("C 10 8") << QString("10") << 8 << true << (qlonglong)8; + QTest::newRow("C 010 8") << QString("010") << 8 << true << (qlonglong)8; + QTest::newRow("C 0x10 8") << QString("0x10") << 8 << false << (qlonglong)0; + QTest::newRow("C 10 10") << QString("10") << 10 << true << (qlonglong)10; + QTest::newRow("C 010 10") << QString("010") << 10 << true << (qlonglong)10; + QTest::newRow("C 0x10 10") << QString("0x10") << 10 << false << (qlonglong)0; + QTest::newRow("C 10 16") << QString("10") << 16 << true << (qlonglong)16; + QTest::newRow("C 010 16") << QString("010") << 16 << true << (qlonglong)16; + QTest::newRow("C 0x10 16") << QString("0x10") << 16 << true << (qlonglong)16; + + QTest::newRow("C -10 0") << QString("-10") << 0 << true << (qlonglong)-10; + QTest::newRow("C -010 0") << QString("-010") << 0 << true << (qlonglong)-8; + QTest::newRow("C -0x10 0") << QString("-0x10") << 0 << true << (qlonglong)-16; + QTest::newRow("C -10 8") << QString("-10") << 8 << true << (qlonglong)-8; + QTest::newRow("C -010 8") << QString("-010") << 8 << true << (qlonglong)-8; + QTest::newRow("C -0x10 8") << QString("-0x10") << 8 << false << (qlonglong)0; + QTest::newRow("C -10 10") << QString("-10") << 10 << true << (qlonglong)-10; + QTest::newRow("C -010 10") << QString("-010") << 10 << true << (qlonglong)-10; + QTest::newRow("C -0x10 10") << QString("-0x10") << 10 << false << (qlonglong)0; + QTest::newRow("C -10 16") << QString("-10") << 16 << true << (qlonglong)-16; + QTest::newRow("C -010 16") << QString("-010") << 16 << true << (qlonglong)-16; + QTest::newRow("C -0x10 16") << QString("-0x10") << 16 << true << (qlonglong)-16; + + // Let's try some Arabic + const char16_t arabic_str[] = { 0x0661, 0x0662, 0x0663, 0x0664, 0x0000 }; // "1234" + QTest::newRow("ar_SA 1234 0") << QString::fromUtf16(arabic_str) << 0 << false << (qlonglong)0; +} + +void tst_QStringRef::integer_conversion() +{ + QFETCH(QString, num_str); + QFETCH(int, base); + QFETCH(bool, good); + QFETCH(qlonglong, num); + QStringRef num_strRef = QStringRef(&num_str); + + bool ok; + qlonglong d = num_strRef.toLongLong(&ok, base); + QCOMPARE(ok, good); + + if (ok) { + QCOMPARE(d, num); + } +} + +void tst_QStringRef::double_conversion_data() +{ + QTest::addColumn("num_str"); + QTest::addColumn("good"); + QTest::addColumn("num"); + + // The good... + + QTest::newRow("C 1") << QString("1") << true << 1.0; + QTest::newRow("C 1.0") << QString("1.0") << true << 1.0; + QTest::newRow("C 1.234") << QString("1.234") << true << 1.234; + QTest::newRow("C 1.234e-10") << QString("1.234e-10") << true << 1.234e-10; + QTest::newRow("C 1.234E10") << QString("1.234E10") << true << 1.234e10; + QTest::newRow("C 1e10") << QString("1e10") << true << 1.0e10; + + // The bad... + + QTest::newRow("C empty") << QString("") << false << 0.0; + QTest::newRow("C null") << QString() << false << 0.0; + QTest::newRow("C .") << QString(".") << false << 0.0; + QTest::newRow("C 1e") << QString("1e") << false << 0.0; + QTest::newRow("C 1,") << QString("1,") << false << 0.0; + QTest::newRow("C 1,0") << QString("1,0") << false << 0.0; + QTest::newRow("C 1,000") << QString("1,000") << false << 0.0; + QTest::newRow("C 1e1.0") << QString("1e1.0") << false << 0.0; + QTest::newRow("C 1e+") << QString("1e+") << false << 0.0; + QTest::newRow("C 1e-") << QString("1e-") << false << 0.0; + QTest::newRow("de_DE 1,0") << QString("1,0") << false << 0.0; + QTest::newRow("de_DE 1,234") << QString("1,234") << false << 0.0; + QTest::newRow("de_DE 1,234e-10") << QString("1,234e-10") << false << 0.0; + QTest::newRow("de_DE 1,234E10") << QString("1,234E10") << false << 0.0; + + // And the ugly... + + QTest::newRow("C .1") << QString(".1") << true << 0.1; + QTest::newRow("C -.1") << QString("-.1") << true << -0.1; + QTest::newRow("C 1.") << QString("1.") << true << 1.0; + QTest::newRow("C 1.E10") << QString("1.E10") << true << 1.0e10; + QTest::newRow("C 1e+10") << QString("1e+10") << true << 1.0e+10; + QTest::newRow("C 1") << QString(" 1") << true << 1.0; + QTest::newRow("C 1 ") << QString("1 ") << true << 1.0; + + // Let's try some Arabic + const char16_t arabic_str[] = { 0x0660, 0x066B, 0x0661, 0x0662, + 0x0663, 0x0664, 0x0065, 0x0662, + 0x0000 }; // "0.1234e2" + QTest::newRow("ar_SA") << QString::fromUtf16(arabic_str) << false << 0.0; +} + +void tst_QStringRef::double_conversion() +{ +#define MY_DOUBLE_EPSILON (2.22045e-16) + + QFETCH(QString, num_str); + QFETCH(bool, good); + QFETCH(double, num); + QStringRef num_strRef = QStringRef(&num_str); + + bool ok; + double d = num_strRef.toDouble(&ok); + QCOMPARE(ok, good); + + if (ok) { + double diff = d - num; + if (diff < 0) + diff = -diff; + QVERIFY(diff <= MY_DOUBLE_EPSILON); + } +} + +void tst_QStringRef::trimmed() +{ + QVERIFY(QStringRef().trimmed().isNull()); + QString a = ""; + QVERIFY(!QStringRef(&a).trimmed().isNull()); + QStringRef b; + a = "Text"; + b = QStringRef(&a); + QCOMPARE(b.compare(QStringLiteral("Text")), 0); + QCOMPARE(b.trimmed().compare(QStringLiteral("Text")), 0); + a = " "; + b = QStringRef(&a); + QCOMPARE(b.compare(QStringLiteral(" ")), 0); + QCOMPARE(b.trimmed().compare(QStringLiteral("")), 0); + a = " a "; + b = QStringRef(&a); + QCOMPARE(b.trimmed().compare(QStringLiteral("a")), 0); + a = "Text a "; + //b = a.midRef(4); + //QCOMPARE(b.compare(QStringLiteral(" a ")), 0); + //QCOMPARE(b.trimmed().compare(QStringLiteral("a")), 0); +} + +void tst_QStringRef::truncate() +{ + const QString str = "OriginalString~"; + const QStringRef cref = QStringRef(&str); + { + QStringRef ref = cref; + ref.truncate(1000); + QCOMPARE(ref, cref); + for (int i = str.size(); i >= 0; --i) { + ref.truncate(i); + QCOMPARE(ref.size(), i); + QCOMPARE(ref, cref.left(i)); + } + QVERIFY(ref.isEmpty()); + } + + { + QStringRef ref = cref; + QVERIFY(!ref.isEmpty()); + ref.truncate(-1); + QVERIFY(ref.isEmpty()); + } +} + +void tst_QStringRef::chop() +{ + const QString originalString = QStringLiteral("OriginalString~"); + const QStringRef cref(&originalString); + { + const int n = 1; + QStringRef ref = cref; + QString str = originalString; + ref.chop(n); + str.chop(n); + QCOMPARE(ref.toString(), QLatin1String("OriginalString")); + QCOMPARE(ref.toString(), str); + } + { + const int n = -1; + QStringRef ref = cref; + QString str = originalString; + ref.chop(n); + str.chop(n); + QCOMPARE(ref.toString(), originalString); + QCOMPARE(ref.toString(), str); + } + { + const int n = 0; + QStringRef ref = cref; + QString str = originalString; + ref.chop(n); + str.chop(n); + QCOMPARE(ref.toString(), originalString); + QCOMPARE(ref.toString(), str); + } + { + const int n = 1000; + QStringRef ref = cref; + QString str = originalString; + ref.chop(n); + str.chop(n); + QCOMPARE(ref.toString(), str); + QVERIFY(ref.isEmpty()); + } +} + +void tst_QStringRef::left() +{ + QString originalString = "OrginalString~"; + QStringRef ref = QStringRef(&originalString, 0, originalString.size() - 1); + QCOMPARE(ref.toString(), QStringLiteral("OrginalString")); + + QVERIFY(ref.left(0).toString().isEmpty()); + QCOMPARE(ref.left(ref.size()).toString(), QStringLiteral("OrginalString")); + + QStringRef nullRef; + QVERIFY(nullRef.isNull()); + QVERIFY(nullRef.left(3).toString().isEmpty()); + QVERIFY(nullRef.left(0).toString().isEmpty()); + QVERIFY(nullRef.left(-1).toString().isEmpty()); + + QStringRef emptyRef(&originalString, 0, 0); + QVERIFY(emptyRef.isEmpty()); + QVERIFY(emptyRef.left(3).toString().isEmpty()); + QVERIFY(emptyRef.left(0).toString().isEmpty()); + QVERIFY(emptyRef.left(-1).toString().isEmpty()); + + QCOMPARE(ref.left(-1), ref); + QCOMPARE(ref.left(100), ref); +} + +void tst_QStringRef::right() +{ + QString originalString = "~OrginalString"; + QStringRef ref = QStringRef(&originalString, 1, originalString.size() - 1); + QCOMPARE(ref.toString(), QLatin1String("OrginalString")); + + QCOMPARE(ref.right(6).toString(), QLatin1String("String")); + QCOMPARE(ref.right(ref.size()).toString(), QLatin1String("OrginalString")); + QCOMPARE(ref.right(0).toString(), QLatin1String("")); + + QStringRef nullRef; + QVERIFY(nullRef.isNull()); + QVERIFY(nullRef.right(3).toString().isEmpty()); + QVERIFY(nullRef.right(0).toString().isEmpty()); + QVERIFY(nullRef.right(-1).toString().isEmpty()); + + QStringRef emptyRef(&originalString, 0, 0); + QVERIFY(emptyRef.isEmpty()); + QVERIFY(emptyRef.right(3).toString().isEmpty()); + QVERIFY(emptyRef.right(0).toString().isEmpty()); + QVERIFY(emptyRef.right(-1).toString().isEmpty()); + + QCOMPARE(ref.right(-1), ref); + QCOMPARE(ref.right(100), ref); +} + +void tst_QStringRef::mid() +{ + QString orig = QStringLiteral("~ABCDEFGHIEfGEFG~"); // 15 + 2 chars + QStringRef a = QStringRef(&orig, 1, 15); + QCOMPARE(a.size(), orig.size() - 2); + + QCOMPARE(a.mid(3,3).toString(),(QString)"DEF"); + QCOMPARE(a.mid(0,0).toString(),(QString)""); + QVERIFY(!a.mid(15,0).toString().isNull()); + QVERIFY(a.mid(15,0).toString().isEmpty()); + QVERIFY(!a.mid(15,1).toString().isNull()); + QVERIFY(a.mid(15,1).toString().isEmpty()); + QVERIFY(a.mid(9999).toString().isEmpty()); + QVERIFY(a.mid(9999,1).toString().isEmpty()); + + QCOMPARE(a.mid(-1, 6), a.mid(0, 5)); + QVERIFY(a.mid(-100, 6).isEmpty()); + QVERIFY(a.mid(INT_MIN, 0).isEmpty()); + QCOMPARE(a.mid(INT_MIN, -1), a); + QVERIFY(a.mid(INT_MIN, INT_MAX).isNull()); + QVERIFY(a.mid(INT_MIN + 1, INT_MAX).isEmpty()); + QCOMPARE(a.mid(INT_MIN + 2, INT_MAX), a.left(1)); + QCOMPARE(a.mid(INT_MIN + a.size() + 1, INT_MAX), a); + QVERIFY(a.mid(INT_MAX).isNull()); + QVERIFY(a.mid(INT_MAX, INT_MAX).isNull()); + QCOMPARE(a.mid(-5, INT_MAX), a); + QCOMPARE(a.mid(-1, INT_MAX), a); + QCOMPARE(a.mid(0, INT_MAX), a); + QCOMPARE(a.mid(1, INT_MAX).toString(), QString("BCDEFGHIEfGEFG")); + QCOMPARE(a.mid(5, INT_MAX).toString(), QString("FGHIEfGEFG")); + QVERIFY(a.mid(20, INT_MAX).isNull()); + QCOMPARE(a.mid(-1, -1), a); + + QStringRef nullRef; + QVERIFY(nullRef.mid(3,3).toString().isEmpty()); + QVERIFY(nullRef.mid(0,0).toString().isEmpty()); + QVERIFY(nullRef.mid(9999,0).toString().isEmpty()); + QVERIFY(nullRef.mid(9999,1).toString().isEmpty()); + + QVERIFY(nullRef.mid(-1, 6).isNull()); + QVERIFY(nullRef.mid(-100, 6).isNull()); + QVERIFY(nullRef.mid(INT_MIN, 0).isNull()); + QVERIFY(nullRef.mid(INT_MIN, -1).isNull()); + QVERIFY(nullRef.mid(INT_MIN, INT_MAX).isNull()); + QVERIFY(nullRef.mid(INT_MIN + 1, INT_MAX).isNull()); + QVERIFY(nullRef.mid(INT_MIN + 2, INT_MAX).isNull()); + QVERIFY(nullRef.mid(INT_MIN + nullRef.size() + 1, INT_MAX).isNull()); + QVERIFY(nullRef.mid(INT_MAX).isNull()); + QVERIFY(nullRef.mid(INT_MAX, INT_MAX).isNull()); + QVERIFY(nullRef.mid(-5, INT_MAX).isNull()); + QVERIFY(nullRef.mid(-1, INT_MAX).isNull()); + QVERIFY(nullRef.mid(0, INT_MAX).isNull()); + QVERIFY(nullRef.mid(1, INT_MAX).isNull()); + QVERIFY(nullRef.mid(5, INT_MAX).isNull()); + QVERIFY(nullRef.mid(20, INT_MAX).isNull()); + QVERIFY(nullRef.mid(-1, -1).isNull()); + + QString ninePineapples = "~Nine pineapples~"; + QStringRef x = QStringRef(&ninePineapples, 1, ninePineapples.size() - 1); + QCOMPARE(x.mid(5, 4).toString(), QString("pine")); + QCOMPARE(x.mid(5).toString(), QString("pineapples~")); + + QCOMPARE(x.mid(-1, 6), x.mid(0, 5)); + QVERIFY(x.mid(-100, 6).isEmpty()); + QVERIFY(x.mid(INT_MIN, 0).isEmpty()); + QCOMPARE(x.mid(INT_MIN, -1).toString(), x.toString()); + QVERIFY(x.mid(INT_MIN, INT_MAX).isNull()); + QVERIFY(x.mid(INT_MIN + 1, INT_MAX).isEmpty()); + QCOMPARE(x.mid(INT_MIN + 2, INT_MAX), x.left(1)); + QCOMPARE(x.mid(INT_MIN + x.size() + 1, INT_MAX).toString(), x.toString()); + QVERIFY(x.mid(INT_MAX).isNull()); + QVERIFY(x.mid(INT_MAX, INT_MAX).isNull()); + QCOMPARE(x.mid(-5, INT_MAX).toString(), x.toString()); + QCOMPARE(x.mid(-1, INT_MAX).toString(), x.toString()); + QCOMPARE(x.mid(0, INT_MAX), x); + QCOMPARE(x.mid(1, INT_MAX).toString(), QString("ine pineapples~")); + QCOMPARE(x.mid(5, INT_MAX).toString(), QString("pineapples~")); + QVERIFY(x.mid(20, INT_MAX).isNull()); + QCOMPARE(x.mid(-1, -1), x); + + QStringRef emptyRef(&ninePineapples, 0, 0); + QVERIFY(emptyRef.mid(1).isEmpty()); + QVERIFY(emptyRef.mid(-1).isEmpty()); + QVERIFY(emptyRef.mid(0).isEmpty()); + QVERIFY(emptyRef.mid(0, 3).isEmpty()); + QVERIFY(emptyRef.mid(-10, 3).isEmpty()); +} + +static bool operator ==(const QStringList &left, const QList &right) +{ + if (left.size() != right.size()) + return false; + + return std::equal(left.constBegin(), left.constEnd(), right.constBegin()); +} +static inline bool operator ==(const QList &left, const QStringList &right) { return right == left; } + +void tst_QStringRef::split_data() +{ + QTest::addColumn("str"); + QTest::addColumn("sep"); + QTest::addColumn("result"); + + QTest::newRow("a,b,c") << "a,b,c" << "," << (QStringList() << "a" << "b" << "c"); + QTest::newRow("a,b,c,a,b,c") << "a,b,c,a,b,c" << "," << (QStringList() << "a" << "b" << "c" << "a" << "b" << "c"); + QTest::newRow("a,b,c,,a,b,c") << "a,b,c,,a,b,c" << "," << (QStringList() << "a" << "b" << "c" << "" << "a" << "b" << "c"); + QTest::newRow("2") << QString("-rw-r--r-- 1 0 0 519240 Jul 9 2002 bigfile") + << " " + << (QStringList() << "-rw-r--r--" << "" << "1" << "0" << "" << "0" << "" + << "519240" << "Jul" << "" << "9" << "" << "2002" << "bigfile"); + QTest::newRow("one-empty") << "" << " " << (QStringList() << ""); + QTest::newRow("two-empty") << " " << " " << (QStringList() << "" << ""); + QTest::newRow("three-empty") << " " << " " << (QStringList() << "" << "" << ""); + + QTest::newRow("all-empty") << "" << "" << (QStringList() << "" << ""); + QTest::newRow("all-null") << QString() << QString() << (QStringList() << QString() << QString()); + QTest::newRow("sep-empty") << "abc" << "" << (QStringList() << "" << "a" << "b" << "c" << ""); +} + +void tst_QStringRef::split() +{ + QFETCH(QString, str); + QFETCH(QString, sep); + QFETCH(QStringList, result); + + QList list; + // we construct a bigger valid string to check + // if ref.split is using the right size + QString source = str + str + str; + QStringRef ref = QStringRef(&source, str.size(), str.size()); + QCOMPARE(ref.size(), str.size()); + + list = ref.split(sep); + QVERIFY(list == result); + if (sep.size() == 1) { + list = ref.split(sep.at(0)); + QVERIFY(list == result); + } + + list = ref.split(sep, Qt::KeepEmptyParts); + QVERIFY(list == result); + if (sep.size() == 1) { + list = ref.split(sep.at(0), Qt::KeepEmptyParts); + QVERIFY(list == result); + } + + result.removeAll(""); + list = ref.split(sep, Qt::SkipEmptyParts); + QVERIFY(list == result); + if (sep.size() == 1) { + list = ref.split(sep.at(0), Qt::SkipEmptyParts); + QVERIFY(list == result); + } +} + +void tst_QStringRef::nullToString() +{ + QStringRef nullRef; + QVERIFY(nullRef.isNull()); + QVERIFY(nullRef.toString().isNull()); + + QString str; + nullRef = &str; + QVERIFY(nullRef.isNull()); + QVERIFY(nullRef.toString().isNull()); +} + +void tst_QStringRef::qHashFunction() +{ + const QString s = QStringLiteral("abcdefghijklmnopqrstuvxyz").repeated(16); + QCOMPARE(qHash(s), qHash(QStringRef(&s))); + + QString null, empty(""); + QStringRef nullRef, emptyRef(&empty); + QCOMPARE(nullRef, emptyRef); + + const uint seed = 1045982819; + QCOMPARE(qHash(nullRef, seed), qHash(emptyRef, seed)); +} + +QTEST_APPLESS_MAIN(tst_QStringRef) + +#include "tst_qstringref.moc" diff --git a/tests/auto/core5/text/text.pro b/tests/auto/core5/text/text.pro new file mode 100644 index 0000000..855ddc5 --- /dev/null +++ b/tests/auto/core5/text/text.pro @@ -0,0 +1,5 @@ +TEMPLATE = subdirs + +SUBDIRS = \ + qstringref \ + qstringbuilder diff --git a/tests/benchmarks/core5/text/qstringbuilder/CMakeLists.txt b/tests/benchmarks/core5/text/qstringbuilder/CMakeLists.txt new file mode 100644 index 0000000..d6dc3f4 --- /dev/null +++ b/tests/benchmarks/core5/text/qstringbuilder/CMakeLists.txt @@ -0,0 +1,19 @@ +# Generated from qstringbuilder.pro. + +##################################################################### +## tst_bench_qstringbuilder Binary: +##################################################################### + +qt_add_benchmark(tst_bench_qstringbuilder + SOURCES + main.cpp + PUBLIC_LIBRARIES + Qt::Core5Compat + Qt::Test + COMPILE_OPTIONS + -g +) + +#### Keys ignored in scope 1:.:.:qstringbuilder.pro:: +# QMAKE_CFLAGS = "-g" +# TEMPLATE = "app" diff --git a/tests/benchmarks/core5/text/qstringbuilder/main.cpp b/tests/benchmarks/core5/text/qstringbuilder/main.cpp new file mode 100644 index 0000000..fd1c40b --- /dev/null +++ b/tests/benchmarks/core5/text/qstringbuilder/main.cpp @@ -0,0 +1,429 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// Select one of the scenarios below +#define SCENARIO 1 + +#if SCENARIO == 1 +// this is the "no harm done" version. Only operator% is active, +// with NO_CAST * defined +#undef QT_USE_FAST_OPERATOR_PLUS +#undef QT_USE_FAST_CONCATENATION +#define QT_NO_CAST_FROM_ASCII +#define QT_NO_CAST_TO_ASCII +#endif + + +#if SCENARIO == 2 +// this is the "full" version. Operator+ is replaced by a QStringBuilder +// based version +// with NO_CAST * defined +#define QT_USE_FAST_OPERATOR_PLUS +#define QT_USE_FAST_CONCATENATION +#define QT_NO_CAST_FROM_ASCII +#define QT_NO_CAST_TO_ASCII +#endif + +#if SCENARIO == 3 +// this is the "no harm done" version. Only operator% is active, +// with NO_CAST * _not_ defined +#undef QT_USE_FAST_OPERATOR_PLUS +#undef QT_USE_FAST_CONCATENATION +#undef QT_NO_CAST_FROM_ASCII +#undef QT_NO_CAST_TO_ASCII +#endif + +#if SCENARIO == 4 +// this is the "full" version. Operator+ is replaced by a QStringBuilder +// based version +// with NO_CAST * _not_ defined +#define QT_USE_FAST_OPERATOR_PLUS +#define QT_USE_FAST_CONCATENATION +#undef QT_NO_CAST_FROM_ASCII +#undef QT_NO_CAST_TO_ASCII +#endif + + +#include +#include +#include +#include + +#include + +#include + +// Select one of the scenarios below +#if SCENARIO == 1 +#define P % +#elif SCENARIO == 2 +#define P + +#elif SCENARIO == 3 +#define P % +#elif SCENARIO == 4 +#define P + +#endif + +#define COMPARE(a, b) QCOMPARE(a, b) +//#define COMPARE(a, b) + +#define SEP(s) qDebug() << "\n\n-------- " s " ---------"; + +#define LITERAL "some string literal" + +class tst_qstringbuilder : public QObject +{ + Q_OBJECT + +public: + tst_qstringbuilder() + : l1literal(LITERAL), + l1string(LITERAL), + ba(LITERAL), + string(l1string), + stdstring(LITERAL), + stringref(&string, 2, 10), + achar('c'), + r2(QLatin1String(LITERAL LITERAL)), + r3(QLatin1String(LITERAL LITERAL LITERAL)), + r4(QLatin1String(LITERAL LITERAL LITERAL LITERAL)), + r5(QLatin1String(LITERAL LITERAL LITERAL LITERAL LITERAL)) + {} + + +public: + enum { N = 10000 }; + + int run_traditional() + { + int s = 0; + for (int i = 0; i < N; ++i) { +#if 0 + s += QString(l1string + l1string).size(); + s += QString(l1string + l1string + l1string).size(); + s += QString(l1string + l1string + l1string + l1string).size(); + s += QString(l1string + l1string + l1string + l1string + l1string).size(); +#endif + s += QString(achar + l1string + achar).size(); + } + return s; + } + + int run_builder() + { + int s = 0; + for (int i = 0; i < N; ++i) { +#if 0 + s += QString(l1literal P l1literal).size(); + s += QString(l1literal P l1literal P l1literal).size(); + s += QString(l1literal P l1literal P l1literal P l1literal).size(); + s += QString(l1literal P l1literal P l1literal P l1literal P l1literal).size(); +#endif + s += QString(achar % l1literal % achar).size(); + } + return s; + } + +private slots: + + void separator_0() { + qDebug() << "\nIn each block the QStringBuilder based result appear first " + "(with a 'b_' prefix), QStringBased second ('q_' prefix), std::string " + "last ('s_' prefix)\n"; + } + + void separator_1() { SEP("literal + literal (builder first)"); } + + void b_2_l1literal() { + QBENCHMARK { r = l1literal P l1literal; } + COMPARE(r, r2); + } + #ifndef QT_NO_CAST_FROM_ASCII + void b_l1literal_LITERAL() { + QBENCHMARK { r = l1literal P LITERAL; } + COMPARE(r, r2); + } + #endif + void q_2_l1string() { + QBENCHMARK { r = l1string + l1string; } + COMPARE(r, r2); + } + + + void separator_2() { SEP("2 strings"); } + + void b_2_string() { + QBENCHMARK { r = string P string; } + COMPARE(r, r2); + } + void q_2_string() { + QBENCHMARK { r = string + string; } + COMPARE(r, r2); + } + void s_2_string() { + QBENCHMARK { stdr = stdstring + stdstring; } + COMPARE(stdr, stdstring + stdstring); + } + + + void separator_2c() { SEP("2 string refs"); } + + void b_2_stringref() { + QBENCHMARK { r = stringref % stringref; } + COMPARE(r, QString(stringref.toString() + stringref.toString())); + } + void q_2_stringref() { + QBENCHMARK { r = stringref.toString() + stringref.toString(); } + COMPARE(r, QString(stringref % stringref)); + } + + + void separator_2b() { SEP("3 strings"); } + + void b_3_string() { + QBENCHMARK { r = string P string P string; } + COMPARE(r, r3); + } + void q_3_string() { + QBENCHMARK { r = string + string + string; } + COMPARE(r, r3); + } + void s_3_string() { + QBENCHMARK { stdr = stdstring + stdstring + stdstring; } + COMPARE(stdr, stdstring + stdstring + stdstring); + } + + void separator_2e() { SEP("4 strings"); } + + void b_4_string() { + QBENCHMARK { r = string P string P string P string; } + COMPARE(r, r4); + } + void q_4_string() { + QBENCHMARK { r = string + string + string + string; } + COMPARE(r, r4); + } + void s_4_string() { + QBENCHMARK { stdr = stdstring + stdstring + stdstring + stdstring; } + COMPARE(stdr, stdstring + stdstring + stdstring + stdstring); + } + + + + void separator_2a() { SEP("string + literal (builder first)"); } + + void b_string_l1literal() { + QBENCHMARK { r = string % l1literal; } + COMPARE(r, r2); + } + #ifndef QT_NO_CAST_FROM_ASCII + void b_string_LITERAL() { + QBENCHMARK { r = string P LITERAL; } + COMPARE(r, r2); + } + void b_LITERAL_string() { + QBENCHMARK { r = LITERAL P string; } + COMPARE(r, r2); + } + #endif + void b_string_l1string() { + QBENCHMARK { r = string P l1string; } + COMPARE(r, r2); + } + void q_string_l1literal() { + QBENCHMARK { r = string + l1string; } + COMPARE(r, r2); + } + void q_string_l1string() { + QBENCHMARK { r = string + l1string; } + COMPARE(r, r2); + } + void s_LITERAL_string() { + QBENCHMARK { stdr = LITERAL + stdstring; } + COMPARE(stdr, stdstring + stdstring); + } + + + void separator_3() { SEP("3 literals"); } + + void b_3_l1literal() { + QBENCHMARK { r = l1literal P l1literal P l1literal; } + COMPARE(r, r3); + } + void q_3_l1string() { + QBENCHMARK { r = l1string + l1string + l1string; } + COMPARE(r, r3); + } + void s_3_l1string() { + QBENCHMARK { stdr = stdstring + LITERAL + LITERAL; } + COMPARE(stdr, stdstring + stdstring + stdstring); + } + + + void separator_4() { SEP("4 literals"); } + + void b_4_l1literal() { + QBENCHMARK { r = l1literal P l1literal P l1literal P l1literal; } + COMPARE(r, r4); + } + void q_4_l1string() { + QBENCHMARK { r = l1string + l1string + l1string + l1string; } + COMPARE(r, r4); + } + + + void separator_5() { SEP("5 literals"); } + + void b_5_l1literal() { + QBENCHMARK { r = l1literal P l1literal P l1literal P l1literal P l1literal; } + COMPARE(r, r5); + } + + void q_5_l1string() { + QBENCHMARK { r = l1string + l1string + l1string + l1string + l1string; } + COMPARE(r, r5); + } + + + void separator_6() { SEP("4 chars"); } + + void b_string_4_char() { + QBENCHMARK { r = string + achar + achar + achar + achar; } + COMPARE(r, QString(string P achar P achar P achar P achar)); + } + + void q_string_4_char() { + QBENCHMARK { r = string + achar + achar + achar + achar; } + COMPARE(r, QString(string P achar P achar P achar P achar)); + } + + void s_string_4_char() { + QBENCHMARK { stdr = stdstring + 'c' + 'c' + 'c' + 'c'; } + COMPARE(stdr, stdstring + 'c' + 'c' + 'c' + 'c'); + } + + + void separator_7() { SEP("char + string + char"); } + + void b_char_string_char() { + QBENCHMARK { r = achar + string + achar; } + COMPARE(r, QString(achar P string P achar)); + } + + void q_char_string_char() { + QBENCHMARK { r = achar + string + achar; } + COMPARE(r, QString(achar P string P achar)); + } + + void s_char_string_char() { + QBENCHMARK { stdr = 'c' + stdstring + 'c'; } + COMPARE(stdr, 'c' + stdstring + 'c'); + } + + + void separator_8() { SEP("string.arg"); } + + void b_string_arg() { + const QString pattern = l1string + QString::fromLatin1("%1") + l1string; + QBENCHMARK { r = l1literal P string P l1literal; } + COMPARE(r, r3); + } + + void q_string_arg() { + const QString pattern = l1string + QLatin1String("%1") + l1string; + QBENCHMARK { r = pattern.arg(string); } + COMPARE(r, r3); + } + + void q_bytearray_arg() { + QByteArray result; + QBENCHMARK { result = ba + ba + ba; } + } + + + void separator_9() { SEP("QString::reserve()"); } + + void b_reserve() { + QBENCHMARK { + r.clear(); + r = string P string P string P string; + } + COMPARE(r, r4); + } + void b_reserve_lit() { + QBENCHMARK { + r.clear(); + r = string P l1literal P string P string; + } + COMPARE(r, r4); + } + void s_reserve() { + QBENCHMARK { + r.clear(); + r.reserve(string.size() + string.size() + string.size() + string.size()); + r += string; + r += string; + r += string; + r += string; + } + COMPARE(r, r4); + } + void s_reserve_lit() { + QBENCHMARK { + r.clear(); + //r.reserve(string.size() + qstrlen(l1string.latin1()) + // + string.size() + string.size()); + r.reserve(1024); + r += string; + r += l1string; + r += string; + r += string; + } + COMPARE(r, r4); + } + +private: + const QLatin1String l1literal; + const QLatin1String l1string; + const QByteArray ba; + const QString string; + const std::string stdstring; + const QStringRef stringref; + const QLatin1Char achar; + const QString r2, r3, r4, r5; + + // short cuts for results + QString r; + std::string stdr; +}; + +QTEST_MAIN(tst_qstringbuilder) + +#undef P + +#include "main.moc" diff --git a/tests/benchmarks/core5/text/qstringbuilder/qstringbuilder.pro b/tests/benchmarks/core5/text/qstringbuilder/qstringbuilder.pro new file mode 100644 index 0000000..c17ad5f --- /dev/null +++ b/tests/benchmarks/core5/text/qstringbuilder/qstringbuilder.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +CONFIG += benchmark +QT = core testlib core5compat + +QMAKE_CXXFLAGS += -g +QMAKE_CFLAGS += -g + +TARGET = tst_bench_qstringbuilder +SOURCES += main.cpp