diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qstring.cpp | 75 | ||||
-rw-r--r-- | src/corelib/text/qstring.h | 40 | ||||
-rw-r--r-- | src/plugins/platforms/ios/qiosscreen.mm | 2 |
3 files changed, 68 insertions, 49 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 5b654264ac..5721a25106 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -5133,30 +5133,36 @@ QList<uint> QtPrivate::convertToUcs4(QStringView string) return qt_convert_to_ucs4(string); } -QString::DataPointer QString::fromLatin1_helper(const char *str, qsizetype size) +/*! + \fn QString QString::fromLatin1(QByteArrayView str) + \overload + \since 6.0 + + Returns a QString initialized with the Latin-1 string \a str. +*/ +QString QString::fromLatin1(QByteArrayView ba) { DataPointer d; - if (!str) { + if (!ba.data()) { // nothing to do - } else if (size == 0 || (!*str && size < 0)) { + } else if (ba.size() == 0) { d = DataPointer::fromRawData(&_empty, 0); } else { - if (size < 0) - size = qstrlen(str); - d = DataPointer(Data::allocate(size), size); - d.data()[size] = '\0'; + d = DataPointer(Data::allocate(ba.size()), ba.size()); + d.data()[ba.size()] = '\0'; char16_t *dst = d.data(); - qt_from_latin1(dst, str, size_t(size)); + + qt_from_latin1(dst, ba.data(), size_t(ba.size())); } - return d; + return QString(std::move(d)); } -/*! \fn QString QString::fromLatin1(const char *str, qsizetype size) +/*! + \fn QString QString::fromLatin1(const char *str, qsizetype size) Returns a QString initialized with the first \a size characters of the Latin-1 string \a str. - If \a size is -1 (default), it is taken to be strlen(\a - str). + If \a size is \c{-1}, \c{strlen(str)} is used instead. \sa toLatin1(), fromUtf8(), fromLocal8Bit() */ @@ -5169,12 +5175,12 @@ QString::DataPointer QString::fromLatin1_helper(const char *str, qsizetype size) Returns a QString initialized with the Latin-1 string \a str. */ -/*! \fn QString QString::fromLocal8Bit(const char *str, qsizetype size) +/*! + \fn QString QString::fromLocal8Bit(const char *str, qsizetype size) Returns a QString initialized with the first \a size characters of the 8-bit string \a str. - If \a size is -1 (default), it is taken to be strlen(\a - str). + If \a size is \c{-1}, \c{strlen(str)} is used instead. On Unix systems this is equivalen to fromUtf8(), on Windows the systems current code page is being used. @@ -5189,24 +5195,29 @@ QString::DataPointer QString::fromLatin1_helper(const char *str, qsizetype size) Returns a QString initialized with the 8-bit string \a str. */ -QString QString::fromLocal8Bit_helper(const char *str, qsizetype size) + +/*! + \fn QString QString::fromLocal8Bit(QByteArrayView str) + \overload + \since 6.0 + + Returns a QString initialized with the 8-bit string \a str. +*/ +QString QString::fromLocal8Bit(QByteArrayView ba) { - if (!str) + if (ba.isNull()) return QString(); - if (size < 0) - size = qstrlen(str); - if (size == 0) + if (ba.isEmpty()) return QString(DataPointer::fromRawData(&_empty, 0)); QStringDecoder toUtf16(QStringDecoder::System, QStringDecoder::Flag::Stateless); - return toUtf16(str, size); + return toUtf16(ba.data(), ba.size()); } /*! \fn QString QString::fromUtf8(const char *str, qsizetype size) Returns a QString initialized with the first \a size bytes of the UTF-8 string \a str. - If \a size is -1 (default), it is taken to be strlen(\a - str). + If \a size is \c{-1}, \c{strlen(str)} is used instead. UTF-8 is a Unicode codec and can represent all characters in a Unicode string like QString. However, invalid sequences are possible with UTF-8 @@ -5238,13 +5249,21 @@ QString QString::fromLocal8Bit_helper(const char *str, qsizetype size) Returns a QString initialized with the UTF-8 string \a str. */ -QString QString::fromUtf8_helper(const char *str, qsizetype size) + +/*! + \fn QString QString::fromUtf8(QByteArrayView str) + \overload + \since 6.0 + + Returns a QString initialized with the UTF-8 string \a str. +*/ +QString QString::fromUtf8(QByteArrayView ba) { - if (!str) + if (ba.isNull()) return QString(); - - Q_ASSERT(size != -1); - return QUtf8::convertToUnicode(str, size); + if (ba.isEmpty()) + return QString(DataPointer::fromRawData(&_empty, 0)); + return QUtf8::convertToUnicode(ba.data(), ba.size()); } /*! diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 0e564b20ed..d12c96632e 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -669,29 +669,32 @@ public: [[nodiscard]] QList<uint> toUcs4() const; // note - this are all inline so we can benefit from strlen() compile time optimizations - static inline QString fromLatin1(const char *str, qsizetype size = -1) + static QString fromLatin1(QByteArrayView ba); + Q_WEAK_OVERLOAD + static inline QString fromLatin1(const QByteArray &ba) { return fromLatin1(QByteArrayView(ba)); } + static inline QString fromLatin1(const char *str, qsizetype size) { - return QString(fromLatin1_helper(str, (str && size == -1) ? qsizetype(strlen(str)) : size)); + return fromLatin1(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size)); } - static inline QString fromUtf8(const char *str, qsizetype size = -1) + static QString fromUtf8(QByteArrayView utf8); + Q_WEAK_OVERLOAD + static inline QString fromUtf8(const QByteArray &ba) { return fromUtf8(QByteArrayView(ba)); } + static inline QString fromUtf8(const char *utf8, qsizetype size) { - return fromUtf8_helper(str, (str && size == -1) ? qsizetype(strlen(str)) : size); + return fromUtf8(QByteArrayView(utf8, !utf8 || size < 0 ? qstrlen(utf8) : size)); } #ifdef __cpp_char8_t Q_WEAK_OVERLOAD - static inline QString fromUtf8(const char8_t *str, qsizetype size = -1) + static inline QString fromUtf8(const char8_t *str, qsizetype size) { return fromUtf8(reinterpret_cast<const char *>(str), int(size)); } #endif - static inline QString fromLocal8Bit(const char *str, qsizetype size = -1) + static QString fromLocal8Bit(QByteArrayView ba); + Q_WEAK_OVERLOAD + static inline QString fromLocal8Bit(const QByteArray &ba) { return fromLocal8Bit(QByteArrayView(ba)); } + static inline QString fromLocal8Bit(const char *str, qsizetype size) { - return fromLocal8Bit_helper(str, (str && size == -1) ? qsizetype(strlen(str)) : size); + return fromLocal8Bit(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size)); } - static inline QString fromLatin1(const QByteArray &str) - { return str.isNull() ? QString() : fromLatin1(str.data(), qstrnlen(str.constData(), str.size())); } - static inline QString fromUtf8(const QByteArray &str) - { return str.isNull() ? QString() : fromUtf8(str.data(), qstrnlen(str.constData(), str.size())); } - static inline QString fromLocal8Bit(const QByteArray &str) - { return str.isNull() ? QString() : fromLocal8Bit(str.data(), qstrnlen(str.constData(), str.size())); } static QString fromUtf16(const char16_t *, qsizetype size = -1); static QString fromUcs4(const char32_t *, qsizetype size = -1); static QString fromRawData(const QChar *, qsizetype size); @@ -964,9 +967,6 @@ private: static QString trimmed_helper(QString &str); static QString simplified_helper(const QString &str); static QString simplified_helper(QString &str); - static DataPointer fromLatin1_helper(const char *str, qsizetype size = -1); - static QString fromUtf8_helper(const char *str, qsizetype size); - static QString fromLocal8Bit_helper(const char *, qsizetype size); static QByteArray toLatin1_helper(const QString &); static QByteArray toLatin1_helper_inplace(QString &); static QByteArray toUtf8_helper(const QString &); @@ -1057,8 +1057,8 @@ QString QAnyStringView::toString() const // // QString inline members // -inline QString::QString(QLatin1String aLatin1) : d(fromLatin1_helper(aLatin1.latin1(), aLatin1.size())) -{ } +inline QString::QString(QLatin1String latin1) +{ *this = QString::fromLatin1(latin1.data(), latin1.size()); } inline const QChar QString::at(qsizetype i) const { Q_ASSERT(size_t(i) < size_t(size())); return QChar(d.data()[i]); } inline const QChar QString::operator[](qsizetype i) const @@ -1304,9 +1304,9 @@ QT_ASCII_CAST_WARN inline bool QLatin1String::operator>=(const QByteArray &s) co { return QString::fromUtf8(s) <= *this; } QT_ASCII_CAST_WARN inline bool QString::operator==(const QByteArray &s) const -{ return QString::compare_helper(constData(), size(), s.constData(), qstrnlen(s.constData(), s.size())) == 0; } +{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) == 0; } QT_ASCII_CAST_WARN inline bool QString::operator!=(const QByteArray &s) const -{ return QString::compare_helper(constData(), size(), s.constData(), qstrnlen(s.constData(), s.size())) != 0; } +{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) != 0; } QT_ASCII_CAST_WARN inline bool QString::operator<(const QByteArray &s) const { return QString::compare_helper(constData(), size(), s.constData(), s.size()) < 0; } QT_ASCII_CAST_WARN inline bool QString::operator>(const QByteArray &s) const diff --git a/src/plugins/platforms/ios/qiosscreen.mm b/src/plugins/platforms/ios/qiosscreen.mm index 411f6ebf81..c7dbe43124 100644 --- a/src/plugins/platforms/ios/qiosscreen.mm +++ b/src/plugins/platforms/ios/qiosscreen.mm @@ -245,7 +245,7 @@ static QString deviceModelIdentifier() char value[size]; sysctlbyname(key, &value, &size, NULL, 0); - return QString::fromLatin1(value); + return QString::fromLatin1(QByteArrayView(value, qsizetype(size))); #endif } |