Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

A little hidden gem: QStringIterator

/*!
    Returns \c true if the string only contains uppercase letters,
    otherwise returns \c false.
*/
bool QString::isUpper() const
{
    if (isEmpty())
        return false;

    const QChar *d = data();

    for (int i = 0, max = size(); i < max; ++i) {
        if (!d[i].isUpper())
            return false;
    }

    return true;
}
QT += core-private
target_link_libraries(my_target Qt5::CorePrivate)
#include <private/qstringiterator_p.h>

bool QString::isUpper() const
{
    QStringIterator it(*this);

    while (it.hasNext()) {
        uint c = it.next();
        if (!QChar::isUpper(c))
            return false;
    }

    return true;
}
QStringIterator i(str);
while (i.hasNext())
  use(i.next());
// C++11
for (auto cp : QStringIterator(str))
  use(cp);
 
// C++20
auto stringLenInCodePoints = std::ranges::distance(QStringIterator(str));
bool stringIsUpperCase = std::ranges::all_of(QStringIterator(str), &QChar::isUpper);
 
// C++20 + P1206
auto decodedString = QStringIterator(str) | std::ranges::to<QVector<uint>>;

About KDAB


2 Comments

15 - Feb - 2022

Brian Warner

15 - Feb - 2022

Giuseppe D'Angelo

GiuseppeD'Angelo

Giuseppe D’Angelo

Senior Software Engineer

Learn Modern C++

Learn more