summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qbytearray.h
Commit message (Collapse)AuthorAgeFilesLines
* QString/QByteArray: add explicit constructors for Q{String,ByteArray}ViewThiago Macieira2024-03-121-1/+2
| | | | | | | | | | | | | | | | | | | std::string has them too, see constructor 10 in [1]. There, they are StringViewLike, something we may want to do here too, which would also lower the precedence of the constructor in the overload resolution. This will be needed for the non-const heterogeneous QHash::operator[]. [ChangeLog][QtCore][QByteArray] Added a constructor to create a QByteArray from QByteArrayView. [ChangeLog][QtCore][QString] Added a constructor to create a QString from QStringView. [1] https://en.cppreference.com/w/cpp/string/basic_string/basic_string Change-Id: I6818d78a57394e37857bfffd17b9ab03bd0253e6 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QByteArray: use comparison helper macrosIvan Solovev2024-03-021-51/+36
| | | | | | | | | | | | | | | | | | Replace the existing friend relational operators with the macros. Add the previously-missing relational opertors with QChar and char16_t. This allows to remove the last dummy relational operators and the macros to generate them in tst_qstringapisymmetry. Because of a bug in libstdc++[0], we have to explicitly keep the QBA vs QBA relational operators even in C++20 mode. This problem is specific to QByteArray, because it is convertible to const void *. [0]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114153 Task-number: QTBUG-117661 Change-Id: Iac7f81cd3274331b7c7695a51803321b511361c0 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QString: use comparison helper macros - comparison with byte arrays [2/3]Ivan Solovev2024-03-021-1/+3
| | | | | | | | | | | | | | | | | | Use the comparison helper macros to replace the member relational operators for comparison with QByteArray and const char *. As QString and QByteArray are exported, we cannot simply remove the inline methods, so wrap them into QT_CORE_REMOVED_SINCE. Add relational operators with QByteArrayView. Provide more unit-tests for the comparison with the byte array types. This enables operator<=> for QString vs byte arrays in C++20 builds. Task-number: QTBUG-117661 Change-Id: I305343e1b6c5d78b10f2976573db4e904ba6b44b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QString/QByteArray: add slice() methodsAhmad Samir2024-02-291-0/+11
| | | | | | | | | [ChangeLog][QtCore][QString/QByteArray] Added slice() methods that work like sliced(), but modify the string/byte-array they are called on. Task-number: QTBUG-99218 Change-Id: I3075562983ef123d9aa022a2304c7e774cf2ea42 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Containers: add max_size()Giuseppe D'Angelo2024-02-271-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | One more method for STL compatibility. This one is particularly subtle as it's required by the `reservable-container` concept: https://eel.is/c++draft/ranges#range.utility.conv.general-3 Without this concept, ranges::to won't reserve() before copying the elements (out of a sized range which isn't a common_range). Implementation notes: there were already a couple of constants denoting the maximum QByteArray and QString size. Centralize that implementation in QTypedArrayData, so that QList can use it too. The maximum allocation size (private constant) needs a even more central place so that even QVLA can use it. Lacking anything better, I've put it in qcontainerfwd.h. Since our containers aren't allocator-aware, I can make max_size() a static member, and replace the existing constants throughout the rest of qtbase. (I can't kill them yet as they're used by other submodules.) [ChangeLog][QtCore][QList] Added max_size(). [ChangeLog][QtCore][QString] Added max_size(). [ChangeLog][QtCore][QByteArray] Added max_size(). [ChangeLog][QtCore][QVarLengthArray] Added max_size(). Change-Id: I176142e31b998f4f787c96333894b8f6653eb70d Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Add QByteArray vs QByteArrayView relational operatorsIvan Solovev2024-02-201-0/+11
| | | | | | | | | | ... by using the new comparison helper macors. This allows to enable related tests in tst_qstringapisymmetry. Task-number: QTBUG-108805 Change-Id: I2cef8f4a25889b74a921fea47995d59c3a49d368 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QByteArray/QString: add resizeForOverwriteGiuseppe D'Angelo2024-02-161-0/+1
| | | | | | | | | | | | | | | For these classes it's not really a new feature, but exposing the current resize() behavior (which does uninitialized resizes) under a proper name. Changing the existing behavior for resize() is a behavioral break that we can only likely afford in Qt 7. [ChangeLog][QtCore][QString] Added resizeForOverwrite(). [ChangeLog][QtCore][QByteArray] Added resizeForOverwrite(). Change-Id: I15b3104aee2bc29d23e91d97b0e64f87612d0099 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QByteArray: allow begin() to return nullptr, like QStringThiago Macieira2023-12-211-1/+1
| | | | | | | | | | | | | | | | | That is, don't call QByteArray::data() because that one is still under QT5_NULL_STRINGS conditional. Instead, like QString, call d.data() directly, which is allowed to return a null pointer. This necessitated a fix to QStringBuilder's QConcatenable because it was iterating from &QByteArray::_empty to nullptr. [ChangeLog][Important Behavior Changes] Calling begin() or end() in a const QByteArray will return a null pointer if isNull() is true. This is the same behavior as QString. This is important for code attempting to iterate from data() to end() instead of begin() to end(). Change-Id: Ica7a43f6147b49c187ccfffd179e4cda75bd1031 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* QByteArray: inline QByteArray::indexOf() and use a char overloadThiago Macieira2023-12-211-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | QByteArray::indexOf() had the best implementation of the two *because* it was out-of-line, in qbytearray.cpp. The compiler could thus see the body of the QtPrivate::findByteArray() function and inline it, with constant-propagation, so only findCharHelper() was expanded and we had a fast path towards memchr(). On the other hand, QByteArrayView::indexOf() was inline, so the compiler emitted the call to QtPrivate::findByteArray() in user code, causing the full function to be executed. We fix that by adding a char overload for QtPrivate::findByteArray() and lastIndexOf(), so we get to memchr() more quickly. Also, inline QByteArray::indexOf() and lastIndexOf(). Before: QByteArray::indexOf(): 8.83287376 nsecs per iteration 22.01766832 CPU cycles per iteration, 2.49 GHz 62.00000330 instructions per iteration, 2.816 instr/cycle 21.00000281 branch instructions per iteration, 2.38 G/sec QByteArrayView::indexOf(): 9.64034694 nsecs per iteration 24.03001151 CPU cycles per iteration, 2.49 GHz 68.00000355 instructions per iteration, 2.830 instr/cycle 23.00000306 branch instructions per iteration, 2.39 G/sec After (same result for both, requires recompilation): 8.83207052 nsecs per iteration 22.01568546 CPU cycles per iteration, 2.49 GHz 60.00000331 instructions per iteration, 2.725 instr/cycle 21.00000281 branch instructions per iteration, 2.38 G/sec The inlining appears to have cut 2 instructions, but the effect is not measurable in this benchmark (the entire code is in cache and the Branch Predictor is probably primed). Fixes: QTBUG-119750 Change-Id: Ica7a43f6147b49c187ccfffd179e1cb4b306fc62 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* QByteArray: bring end() into idiomatic formMarc Mutz2023-12-071-2/+2
| | | | | | | | | | | | For contiguous containers, end() is always begin() + size(), so use that instead of data() + size(). Centralizes what it means to be an iterator in just begin() now, which will simplify a follow-up commit of Thiago's. Pick-to: 6.6 6.5 Change-Id: I53ca1a335910bdea3d46b9496ba39bc1a2d3fd93 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QString/QByteArray: add lvalue and rvalue overloads of left/mid/rightThiago Macieira2023-10-251-4/+30
| | | | | | | | | | | | The first/last/sliced API may be what we suggest users use, but the vast majority of the installed codebase uses left/mid/right because they've been available since time immemorial. An additional benefit of this is to make left() and right() available as inline methods. Change-Id: Ifeb6206a9fa04424964bfffd1788383817ed906c Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QString/QByteArray: make the sliced(pos) overload call the other slicedThiago Macieira2023-10-251-1/+1
| | | | | | | | Complements 00c1c04bd536aa3db8f0d2ee33261cb4d2a79002. Change-Id: I8f3ce163ccc5408cac39fffd178c543d594cf64f Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
* QString/QBA: add lvalue and rvalue overloads to first/last/sliced/choppedThiago Macieira2023-10-251-5/+30
| | | | | | | | | | | | Those ought to have been the original implementation, when they were added in commit 38096a3d7040edac4f769270d2402ff4e39d7694, for Qt 6.0. Because these classes are exported, we need to provide the previous only implementations for MSVC. All other compilers would provide inline or emit local, out-of-line copies. Change-Id: Ifeb6206a9fa04424964bfffd178836a2ae56157d Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QByteArray: Remove unnecessary <stdarg.h> headerThiago Macieira2023-10-101-1/+0
| | | | | | | | | | We don't use va_list and don't have variadic functions in this file. [ChangeLog][Potentially Source-Incompatible Changes] The header qbytearray.h no longer includes the header stdarg.h. Change-Id: I8f3ce163ccc5408cac39fffd178c7fb49d12b739 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* QString/QByteArray: avoid data() handling _empty in sliced()Thiago Macieira2023-10-091-1/+1
| | | | | | | | | | | | | | | | | .data() in both classes has a null pointer check so it will return non- null even if the object is storing a null pointer, for compatibility with Qt 5 (controlled by QT5_NULL_STRINGS). We don't need this in first()/last()/sliced()/chopped(), so we can skip the test and pass whatever pointer it is directly to the class constructor. Both of them handle null pointers creating an isNull() object. This is a minor performance optimization and interestingly makes these functions now retain isNull() with the result. I'm not adding test for that as I don't want to hardcode that they will do so. Change-Id: Ifeb6206a9fa04424964bfffd17888d14ec8244ec Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* String classes: make first/last/chopped() delegate to sliced()Ahmad Samir2023-09-301-3/+3
| | | | | | | De-duplicates code. Change-Id: Id29511e7e571ed14f9e3cfd4355b901d81ea2562 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Add a verify() method to all sequential containersAhmad Samir2023-09-281-8/+17
| | | | | | | | | | | | | | | | | | | A helper method encasuplating the asserts related to index into the container and length, modelled after the QVLA::verify(). `pos <= size` is OK because if pos == size, the e.g. sliced()'ed container is just going to be empty. Normalize how verify() is used, the first arg is an index and the second a length. This method is constexpr even in QString/QByteArray merely for consistency with similar methods in other string classes (this necessitates using `d.size` in verify() in QString/QBA because size() isn't constexpr). Change-Id: I90e3c56d76c802259297a06d11d46ee342a1daf2 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Qt UDLs: avoid the deprecated form of UDL definitionsMarc Mutz2023-09-121-2/+2
| | | | | | | | | | | | | | cppreference.com says¹ that the space after the "" is deprecated now, so remove the space. Let's just hope all our compilers support this, otherwise it will get ugly². ¹ https://en.cppreference.com/w/cpp/language/user_literal#Literal_operators ² https://github.com/yhirose/cpp-httplib/issues/953 Pick-to: 6.6 6.5 6.2 Change-Id: Ic8e4939e3ba713023c5a5c020305c96b521dbda9 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QByteArray: add STL-style assign()Dennis Oberst2023-06-021-0/+21
| | | | | | | | | | | | | | | | | Implemented assign() methods for QByteArray to align with the criteria of std::basic_string, addressing the previously missing functionality. This is a subset of the overloads provided by the standard. Reference: https://en.cppreference.com/w/cpp/string/basic_string/assign [ChangeLog][QtCore][QByteArray] Added assign(). Fixes: QTBUG-106199 Change-Id: I899b14d74e8f774face8690303efb8610ead95b5 Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
* Rename QBA::{to,from}Uint8Array() → {to,from}EcmaUint8Array()Marc Mutz2023-02-211-2/+2
| | | | | | | | | Found in API review: from/toUintArray() is too generic a name, make sure its name gives enough context. Pick-to: 6.5 Change-Id: Ie10ff06ae11a5e168c4c91b60a9698a41d0429fc Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* QString, QByteArray: don't detach in removeIf/erase/eraseif()Ahmad Samir2023-02-091-3/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | If the object is shared, instead of detaching, copy characters from "this" to a new object except for the chacters that would be erased, this is more efficient than detaching (which would copy the whole data then erase). - Extend tst_QString::removeIf() to catch a corner-case (that I saw with tst_QByteArray::removeIf()). - Add q_uninitialized_remove_copy_if, which works like std::remove_copy_if but for uninitialized memory like q_uninitialized_relocate_n (but copies rather than relocates/moves). With the same static_assert from q_relocate_overlap_n that the type destructor is non-throwing. Added q_uninitialized_remove_copy_if in this commit rather than a separate one so that it's unittested by its usage in eraseIf(). [ChangeLog][QtCore][QString, QByteArray] Removing characters from a currently shared string or byte array is now done more efficiently Task-number: QTBUG-106181 Task-number: QTBUG-106183 Change-Id: Icc0ed31633cef71d482b97e0d2d20d763163d383 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QString/QByteArray/QList: de-pessimize op+ [2/2]: overload on rvalue LHSMarc Mutz2022-12-071-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The + operator is left-associative, so a + b + c is (a + b) + c. Apply the same trick C++20 applied to std::string's op+ and overload for rvalue left-hand sides. This means that a + b + c is now equivalent to [&] { auto tmp = a; tmp += b; tmp += c; return tmp; }() removing a ton of temporary buffers (not objects, because CoW makes it impossible for the compiler to track the single conceptual object passing through the chain) when not using QStringBuilder (which isn't available for QList). This is BC, because the operators are all inline free functions or at least inline members of non-exported classes. Use multi-\fn to document the new operators. No \since is needed, as this doesn't change the set of supported operations, just makes some of them faster. [ChangeLog][QtCore][QList/QString/QByteArray] Chained additions (a + b + c) now produce only one temporary buffer for the whole expression instead of one per addition. Using += or QStringBuilder is still faster, though. Change-Id: I87e837d8803e79dc29c9268f73e6df9fcc0b09a3 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* wasm: add native QByteArray conversion functionsMorten Sørvig2022-12-051-0/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Add functions which converts to and from JavaScript data arrays: static QByteArray::fromUint8Array(emscripten::val array) emscripten::val QByteArray::toUint8Array() const with corresponding internal qstdweb API: static Uint8Array Uint8Array::copyFrom(const QByteArray &buffer) QByteArray Uint8Array::copyToQByteArray() const Both functions will make a copy of the data, i.e. there is no shared reference counting. They take and return Uint8Array typed array views, via emscripten::val JavaScript object references. Unlike other native conversion functions, these have the special property that the data referenced by the native Uint8Array exists outside the heap memory area. This means we can’t e.g. memcpy the data. However, the heap is itself a JavaScript ArrayBuffer, and we can create a Uint8Array view to the buffer owned by the QByteArray, and then use JavaScript API to copy. See the qstdweb::Uint8Array::copy() implementation. That also means that a fromRawUint8Array() variant (which does not copy) is not possible to implement, since we can’t create a pointer to the source data. The inverse toRawUint8Array() is implementable - it would return a Uint8Array view which references the heap’s ArrayBuffer. However, this may turn out to be ill-advised, since Emscripten will create a new ArrayBuffer if/when it resizes the heap. In any case this left for a future expansion. Change-Id: Icaf48fd17ea8686bf04cb523cc1eb581ce63ed34 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
* QString/QByteArray: de-pessimize op+ [1/2]: non-const return typesMarc Mutz2022-12-011-5/+5
| | | | | | | | | | | | | | const return types prevent move semantics, so drop the const. This is BC, because the operators are all inline free functions. In particular, they're not exported (MSVC mangles the return type). [ChangeLog][QtCore][QString/QByteArray] operator+ no longer returns a const object, enabling move-semantics on the return value, but also hidden detaches. Change-Id: Ifc18f6b1df1e3840f57b2521949dbade8e11fd76 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QString, QByteArray: add removeAt/First/Last() convenience methodsAhmad Samir2022-11-161-0/+5
| | | | | | | | | | | Requested in codereview.qt-project.org/c/qt/qtbase/+/441770 [ChangeLog][QtCore][Text] Add removeAt/First/Last() convenience methods to QString and QByteArray Change-Id: I48a803e456e70cc51d51726a5e3aa7c125aedb1c Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QString, QByteArray: add erase(iterator) methodAhmad Samir2022-11-031-0/+1
| | | | | | Fixes: QTBUG-106182 Change-Id: Idc74cc643b90252838ca1a9ca40a330315da421f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Replace usages of Q_CLANG_QDOC with Q_QDOCLuca Di Sera2022-10-211-1/+1
| | | | | | | | | | | | | | | | | | | | | | | To allow the user to customize the C++ code that QDoc sees, so as to be able to work-around some limitations on QDoc itself, QDoc defines two symbols: Q_QDOC and Q_CLANG_QDOC, both of which are "true" during an entire execution of QDoc. At a certain point in time, QDoc allowed the user the choice between a custom C++ parser and a Clang based one. The Q_QDOC symbol would always be defined while the Q_CLANG_QDOC symbol would be defined only when the Clang based parser was chosen. In more recent times, QDoc always uses a Clang based parser, such that both Q_CLANG_QDOC and Q_QDOC are always defined, making them equivalent. To avoid using different symbols, and the possible confusion and fragmentation that derives from it, all usages of Q_CLANG_QDOC are now replaced by the equivalent usages of Q_QDOC. Change-Id: I5810abb9ad1016a4c5bbea99acd03381b8514b3f Reviewed-by: Kai Koehne <kai.koehne@qt.io>
* QByteArray: delegate more of operator== & !+ to QByteArrayViewThiago Macieira2022-08-041-4/+4
| | | | | | | QByteArrayView is capable of dealing with null pointers. Change-Id: I89c4eb48af38408daa7cfffd16fde9ab021ebf06 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* QByteArray: inline QByteArray::isNull()Thiago Macieira2022-07-271-0/+8
| | | | | | | | | | | | | | | | | Keeping it out-of-line is unnecessary and has been since 5.0. In Qt 4.x, it was necessary to compare to the shared_null, which for QByteArray was a static variable. The one for QString has always been inline because the shared null was a static member of QString (in Qt 3, QString::null was a QString variable). See: https://code.qt.io/cgit/qt/qt.git/tree/src/corelib/tools/qbytearray.cpp#n3249 https://code.qt.io/cgit/qt/qt.git/tree/src/corelib/tools/qstring.h#n505 Pick-to: 6.4 Change-Id: I89c4eb48af38408daa7cfffd16fdd7696520f1b6 Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
* QByteArray: de-inline to/fromStdString()Marc Mutz2022-06-071-8/+2
| | | | | | | | | | | Removes another owning container manipulation that made the top entry in Clang -ftime-trace's most-expensive template instantiations in a QtWidgets build. Pick-to: 6.4 Task-number: QTBUG-97601 Change-Id: I8c765ea2e6c46da5188b70c9503dd87e63f6b328 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
* Use SPDX license identifiersLucie Gérard2022-05-161-39/+3
| | | | | | | | | | | | | Replace the current license disclaimer in files by a SPDX-License-Identifier. Files that have to be modified by hand are modified. License files are organized under LICENSES directory. Task-number: QTBUG-67283 Change-Id: Id880c92784c40f3bbde861c0d93f58151c18b9f1 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>
* Deprecate _qs and _qba literal operators in favor of _s and _baSona Kurazyan2022-05-021-0/+5
| | | | | | | | | | [ChangeLog][QtCore] Deprecated _qs and _qba literal operators for QString and QByteArray in favor of _s and _ba in the Qt::Literals::StringLiterals namespace. Task-number: QTBUG-101408 Change-Id: I26aee0055e3b4c1860de6eda8e0eb857c5b3e11a Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Add literal operators for QString/QByteArray to StringLiterals namespaceSona Kurazyan2022-04-071-1/+14
| | | | | | | | | | | | [ChangeLog][QtCore] Added literal operators for _s and _ba for QString and QByteArray respectively in the Qt::Literals::StringLiterals namespace. Task-number: QTBUG-101408 Change-Id: I5cd4e7f36f614ea805cfecc27b91c5d981cd3794 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* QByteArray/QVarLengthArray: add missing resize(n, v) overloadsMarc Mutz2022-04-061-0/+1
| | | | | | | | | | | | | | | | | | QList and QString had them, so add them to QByteArray and QVarLengthArray, too. In the QVLA case, we need to jump though a hoop or two to avoid having to duplicate all the reallocation logic. Nothing a few template tricks cannot solve. [ChangeLog][QtCore][QByteArray] Added resize(n, ch) overload. [ChangeLog][QtCore][QVarLengthArray] Added resize(n, v) overload. Fixes: QTBUG-102270 Change-Id: I0d281ae5b574f440f682e4a62427b434dcf5b687 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Add QByteArray::percentDecoded() as an instance methodEdward Welbourne2022-03-181-0/+1
| | | | | | | | | | | | | Percent-decoding was previously only present as a static method taking a QBA parameter; it might as well be an instance method of that parameter. Change most QBA tests to use it rather the static method. [ChangeLog][QtCore][QByteArray] percentDecoded() is now available as an instance method of the byte array to be decoded, equivalent to the static QByteArray::fromPercentEncoding(). Change-Id: I982101c44bdac5cc4041e85598d52ac101d38fa1 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Get rid of QByteArray::nulTerminated()Edward Welbourne2022-03-181-2/+1
| | | | | | | | | | | It is private and was not called by anything inline, even in 5.15, so can safely be removed without causing binary incompatibilities, now that it is no longer used even out-of-line. Task-number: QTBUG-74286 Change-Id: Iaec3877079746bd9ba7d1a39efbfb2711e7c00a5 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QString/QByteArray: = default the move ctorsMarc Mutz2022-03-161-1/+1
| | | | | | | | | | | | | Instead of relying on implicit construction of the d-pointer and then swapping that with other.d (using overly-generic qSwap(), no less), simply let the compiler do The Right Thing™ via QArrayDataPointer's move constructor. Pick-to: 6.3 6.2 Task-number: QTBUG-97601 Change-Id: I3be30f99f6fb0e17d3550edbe64cbe80880e604a Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* Deprecate {QString, QByteArray}::count()Sona Kurazyan2022-03-121-0/+3
| | | | | | | | | | | | And remove their uses. [ChangeLog][QtCore][Deprecation Notice] Deprecated QString::count() and QByteArray::count() that take no parameters, to avoid confusion with the algorithm overloads of the same name. They can be replaced by size() or length() methods. Change-Id: I6541e3235ab58cf750d89568d66d3b1d9bbd4a04 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QtCore: replace qSwap with std::swap/member-swap where possibleMarc Mutz2022-01-201-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | qSwap() is a monster that looks for ADL overloads of swap() and also detects the noexcept of the wrapped swap() function, so it should only be used when the argument type is unknown. In the vast majority of cases, the type is known to be efficiently std::swap()able or to have a member-swap. Call either of these. For the common case of pointer types, circumvent the expensive trait checks on std::swap() by providing a hand-rolled qt_ptr_swap() template, the advantage being that it can be unconditionally noexcept, removing all type traits instantiations. Don't document it, otherwise we'd be unable to pick it to 6.2. Effects on Clang -ftime-trace of a PCH'ed libQt6Gui.so build: before: **** Template sets that took longest to instantiate: [...] 27766 ms: qSwap<$> (9073 times, avg 3 ms) [...] 2806 ms: std::swap<$> (1229 times, avg 2 ms) (30572ms) after: **** Template sets that took longest to instantiate: [...] 5047 ms: qSwap<$> (641 times, avg 7 ms) [...] 3371 ms: std::swap<$> (1376 times, avg 2 ms) [qt_ptr_swap<$> does not appear in the top 400, so < 905ms] (< 9323ms) As a drive-by, remove superfluous inline keywords and template ornaments. Task-number: QTBUG-97601 Pick-to: 6.3 6.2 Change-Id: I88f9b4e3cbece268c4a1238b6d50e5712a1bab5a Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QString::toLatin1_helper_inplace: simplify the codeThiago Macieira2022-01-171-4/+4
| | | | | | | | | Hot function, so help the compiler out. In particular, this removes any touch to the ref count, so there are no atomic operations or dead code leading to memory allocations and deallocations. Change-Id: I0e5f6bec596a4a78bd3bfffd16c9a0fbd8dd2c12 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* QByteArray: fix append() with a negative lengthThiago Macieira2022-01-111-2/+2
| | | | | | | | | | | | The documentation says that if it's negative, we find the null termination. This bug was introduced with the clean up to use QByteArrayView in commit 8897aa071a668563a53a4c2e6909572f1762b1e7. Fixes: QTBUG-99640 Pick-to: 6.3 6.2 Change-Id: Ib42b3adc93bf4d43bd55fffd16c89fa4a960f3a9 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* QByteArray: sprinkle API with noexceptMarc Mutz2021-12-141-41/+21
| | | | | | | | | | | Mark (const) data()/begin()/end()/size()/empty() and similar as noexcept. Move some trivial definitions into the class body. Pick-to: 6.3 Change-Id: I6f3ab792264347cdc6a476dacecf065f59f16ff9 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Add isValidUtf8() methods to QUtf8StringView and QByteArray{,View}Ievgenii Meshcheriakov2021-09-221-0/+5
| | | | | | | | | | | | | | | The new methods return true if the string contains valid UTF-8 encoded data, or false otherwise. [ChangeLog][QtCore][QByteArray] Added isValidUtf8() method. [ChangeLog][QtCore][QByteArrayView] Added isValidUtf8() method. [ChangeLog][QtCore][QUtf8StringView] Added isValidUtf8() method. Task-number: QTBUG-92021 Change-Id: I5d0cb613265d98b1f189c5f5cc09c1f7db302272 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QS(V)/QBA(V)/QL1S::lastIndexOf: fix the offset calculationsGiuseppe D'Angelo2021-08-191-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When trying to fix 0-length matches at the end of a QString, be83ff65c424cff1036e7da19d6175826d9f7ed9 actually introduced a regression due to how lastIndexOf interprets its `from` parameter. The "established" (=legacy) interpretation of a negative `from` is that it is supposed to indicate that we want the last match at offset `from + size()`. With the default from of -1, that means we want a match starting at most at position `size() - 1` inclusive, i.e. *at* the last position in the string. The aforementioned commit changed that, by allowing a match at position `size()` instead, and this behavioral change broke code. The problem the commit tried to fix was that empty matches *are* allowed to happen at position size(): the last match of regexp // inside the string "test" is indeed at position 4 (the regexp matches 5 times). Changing the meaning of negative from to include that last position (in general: to include position `from+size()+1` as the last valid matching position, in case of a negative `from`) has unfortunately broken client code. Therefore, we need to revert it. This patch does that, adapting the tests as necessary (drive-by: a broken #undef is removed). Reverting the patch however is not sufficient. What we are facing here is an historical API mistake that forces the default `from` (-1) to *skip* the truly last possible match; the mistake is that thre is simply no way to pass a negative `from` and obtain that match. This means that the revert will now cause code like this: str.lastIndexOf(QRE("")); // `from` defaulted to -1 NOT to return str.size(), which is counter-intuitive and wrong. Other APIs expose this inconsistency: for instance, using QRegularExpressionIterator would actually yield a last match at position str.size(). Similarly, using QString::count would return `str.size()+1`. Note that, in general, it's still possible for clients to call str.lastIndexOf(~~~, str.size()) to get the "truly last" match. This patch also tries to fix this case ("have our cake and eat it"). First and foremost, a couple of bugs in QByteArray and QString code are fixed (when dealing with 0-length needles). Second, a lastIndexOf overload is added. One overload is the "legacy" one, that will honor the pre-existing semantics of negative `from`. The new overload does NOT take a `from` parameter at all, and will actually match from the truly end (by simply calling `lastIndexOf(~~~, size())` internally). These overloads are offered for all the existing lastIndexOf() overloads, not only the ones taking QRE. This means that code simply using `lastIndexOf` without any `from` parameter get the "correct" behavior for 0-length matches, and code that specifies one gets the legacy behavior. Matches of length > 0 are not affected anyways, as they can't match at position size(). [ChangeLog][Important Behavior Changes] A regression in the behavior of the lastIndexOf() function on text-related containers and views (QString, QStringView, QByteArray, QByteArrayView, QLatin1String) has been fixed, and the behavior made consistent and more in line with user expectations. When lastIndexOf() is invoked with a negative `from` position, the last match has now to start at the last character in the container/view (before, it was at the position *past* the last character). This makes a difference when using lastIndexOf() with a needle that has 0 length (for instance an empty string, a regular expression that can match 0 characters, and so on); any other case is unaffected. To retrieve the "truly last" match, one can pass a positive `from` offset to lastIndexOf() (basically, pass `size()` as the `from` parameter). To make calls such as `text.lastIndexOf(~~~);`, that do not pass any `from` parameter, behave properly, a new lastIndexOf() overload has been added to all the text containers/views. This overload does not take a `from` parameter at all, and will search starting from one character past the end of the text, therefore returning a correct result when used with needles that may yield 0-length matches. Client code may need to be recompiled in order to use this new overload. Conversely, client code that needs to skip the "truly last" match now needs to pass -1 as the `from` parameter instead of relying on the default. Change-Id: I5e92bdcf1a57c2c3cca97b6adccf0883d00a92e5 Fixes: QTBUG-94215 Pick-to: 6.2 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* Consolidate documentation of floating-point formatting codeEdward Welbourne2021-08-031-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Move the documentation of the format and precision parameters to QLocale::toString(double, char, int), reference it from various QString methods (instead of repeating there and referencing one of those from QLocale). Add brief first lines for various documentation comments. Mention the special handling of negative precision in the moved documentation. Mention QLocale::FloatingPointShortest, add its type to \sa lines of methods it affects. Change a comment on some code implementing its special treatment to make clear that it does apply to 'e' and 'f' formats, not only to 'g', even though it has no overt special handling in that code; and update docs to report the undocumented behavior the comment previously described. Document how infinity and NaN are represented. Be somewhat more consistent about single-quoting the format names where referred to and applying \c to character constants. Make clear what things are different between different methods using these parameters. Reference QString::number() from QByteArray's relevant methods, since they share its quirks. In the process, rename the format and precision parameters of relevant functions so they're consistently named, replacing a mixture of abbreviated forms. Pick-to: 6.2 Change-Id: I077521b30346000e4b4f6968a8e750e934f72937 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QByteArray: don't coerce negative to unsigned for any baseEdward Welbourne2021-08-021-3/+3
| | | | | | | | | | | | | | | | This follows up on commit 98666c8afc80cccb80ca4426b97ec52916c6e610, which did the same for QString. If someone wants to get formatting suitable to an unsigned value, they can cast the value to that unsigned type and the correct overload shall pick it up. [ChangeLog][Important Behavior Changes] QByteArray's formatting of negative whole numbers to bases other than ten now, like QString's (since Qt 6.0), formats the absolute value and prepends a minus sign. Task-number: QTBUG-53706 Pick-to: 6.2 Change-Id: I91fee23d25ac0d5d5bcfcbeccbac1386627c004a Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QByteArray: Move some free-functions aroundMårten Nordheim2021-05-201-57/+0
| | | | | | | | | | Most of them go to qbytearrayalgorithms.h while the deprecated (inline) version of qChecksum goes to qbytearrayview.h In preparation for adding compare to QByteArrayView. Change-Id: If7f65e9e7cd74838e11ebdb952309b811cef079d Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Add literal operators for QString and QByteArrayAndrei Golubev2021-03-301-0/+7
| | | | | | | | | | | | [ChangeLog][QtCore][QString] Added literal operator u"..."_qs that converts a char16_t string literal to QString [ChangeLog][QtCore][QByteArray] Added literal operator "..."_qba that converts char string literal to QByteArray Change-Id: I4aa59b28cc17bff346b378eb70008fb8185d21ac Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
* Use Core library for qmake instead of the Bootstrap libraryAlexey Edelev2021-02-251-1/+1
| | | | | | | | | | | | | | | | | | | Move the qmake-specific logic of the QLibraryInfo class to qmake internals. 'qconfig.cpp.in' now stores information about the library info entries to keep them consistent between qmake and the Core library. qmake requires specific features enabled in the Core library, so building qmake will be skipped if the features are not enabled. All flags directly related to the qmake have been removed from Core lib. Remove all bootstrap related sections from qmake CMakeLists.txt Task-number: QTBUG-89369 Change-Id: I26de157d3bfd4a5526699296e9d46e1c180b89ae Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Kai Koehne <kai.koehne@qt.io>
* Workaround bogus compiler warningFabian Kosmale2020-12-101-1/+10
| | | | | | | | | gcc 9.x (but not 10.x) issues a bogus warning when strlen is used on a string literal; disable the warning for those versions. Upstream bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91490 Change-Id: I7a2a4d0f6ddafcafcd9fcc62fc41ad5d78e61627 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>