summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringview.h
Commit message (Collapse)AuthorAgeFilesLines
* QString et al: fix lastIndexOf() API asymmetryMarc Mutz2021-12-021-1/+3
| | | | | | | | | | | | | | | | | Commit 6cee204d56205e250b0675c9c6d4dd8a2367f3c4 introduced overloads of lastIndexOf() which drop the 'from' argument, inadvertently fixing QTBUG-80694, but failed to provide the new overloads for all existing lastIndexOf() overloads, making the fix for QTBUG-80694 incomplete. This patch completes the fix, by adding the missing overloads (for char-likes) and also adds the missing (non-regex) tests to tst_qstringapisymmetry. Also amends 1c164ec7f21a78025475c561a70b94d1e3dd6bb6. Fixes: QTBUG-80694 Change-Id: Ib4b3d597d658ce2edf01a2bce0d711ecea593d6e Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
* QS(V)/QBA(V)/QL1S::lastIndexOf: fix the offset calculationsGiuseppe D'Angelo2021-08-191-3/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* Q{*String,ByteArray}View::length(): use qsizetype, not intGiuseppe D'Angelo2021-04-151-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Looks like these ones have been forgotten in the Qt 5 -> 6 upgrade to qsizetype. Change them to return qsizetype as well, and fix the docs. Being entirely inline, non-exported classes, we should be able to get away with it without affecting binary compatibility. Moreover, there's no reason for keeping them deprecated. Requires some minor adjustments, just like the ones done for size()'s changes. [ChangeLog][QtCore][QStringView] The length() function now returns `qsizetype`, not `int`, for consistency with the other string and container classes in Qt. Following this change, it has been un-deprecated. [ChangeLog][QtCore][QAnyStringView] The length() function now returns `qsizetype`, not `int`, for consistency with the other string and container classes in Qt. Following this change, it has been un-deprecated. [ChangeLog][QtCore][QUtf8StringView] The length() function now returns `qsizetype`, not `int`, for consistency with the other string and container classes in Qt. Following this change, it has been un-deprecated. [ChangeLog][QtCore][QByteArrayView] The length() function now returns `qsizetype`, not `int`, for consistency with the other string and container classes in Qt. Following this change, it has been un-deprecated. Fixes: QTBUG-92496 Change-Id: Ie0f4939d1083884e95d4725f891b6c6764532cb8 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Mark places where we missed conversion to unicode character typesEdward Welbourne2021-01-081-1/+1
| | | | | | | | | | | We should use char32_t for the toUcs4() methods of QString and QStringView and use char16_t for QString::utf16(), thereby matching QStringView. These naturally imply knock-on changes in various places. Unfortunately, we didn't make those changes in Qt 6, so they'll have to wait for Qt 7. Change-Id: I18451d4b31b76658092e19d3fcbc8bc0fe5ce307 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QStringView: add constBegin and constEndThiago Macieira2020-12-121-0/+2
| | | | | | Change-Id: I55083c2909f64a1f8868fffd164f21118a9d3ec5 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QStringView: add some QRegularExpression-related overloadsGiuseppe D'Angelo2020-12-101-0/+20
| | | | | | | | | [ChangeLog][QtCore][QStringView] Added the indexOf(), contains(), lastIndexOf() and count() methods taking a QRegularExpression. Fixes: QTBUG-89050 Change-Id: Ic726754f67e06b3764302d2fad252e0378a77afc Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Add noexcept to slicing and indexing of QStringViewAllan Sandfeld Jensen2020-11-041-11/+11
| | | | | | | | None of those paths should be able to cast. Change-Id: I947f26c0aae134076499c9f80e910b0257d2bc62 Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Make remaining String binary comparisons hidden friendsAllan Sandfeld Jensen2020-10-261-0/+23
| | | | | | | | | Also forced the introduction of explicit comparisons with char16_t* and std::nullptr_t. Change-Id: I8e32c14a1d3aeec234ee070b9fefc6af06db0fda Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* Use c++20 when available at compile timeAllan Sandfeld Jensen2020-09-251-8/+7
| | | | | | | | Also std::end is constexpr where std::next sometimes isn't always with sanitizers active. Change-Id: Ibe4c11eb4945fb286247e841b6a7f6cc3ff1eaa6 Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
* Replace Q_REQUIRED_RESULT with [[nodiscard]]Allan Sandfeld Jensen2020-09-251-85/+85
| | | | | | | It was already used many places directly making the code inconsistent. Change-Id: I3b14bc6c333640fb3ba33c71eba97e78c973e44b Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Q(Any|Utf8)StringView: move array size deduction feature to fromArrayMårten Nordheim2020-09-181-2/+8
| | | | | | | | | | | | | The constructor taking an array literal will now stop at the first null-terminator encountered. And fromArray is introduced which only supports array literals. Constructs a view of the full size. Explicit so it shouldn't be surprising. Change-Id: I1497c33a5c12453a95e87c990abe6335b2817081 Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Convert QStringView::toWCharArray() to return qsizetypeEdward Welbourne2020-08-281-1/+1
| | | | | | | | As requested in a ### Qt6 comment. Task-number: QTBUG-85700 Change-Id: I28a02bf49f4a6455a21a6032179318ce2915b8ca Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Move QStringRef and remains to Qt5CompatKarsten Heimrich2020-08-201-8/+3
| | | | | | | | | Export some private functions from QUtf8 to resolve undefined symbols in Qt5Compat after moving QStringRef. Task-number: QTBUG-84437 Change-Id: I9046dcb14ed520d8868a511d79da6e721e26f72b Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Replace Qt CONSTEXPR defines with constexprAllan Sandfeld Jensen2020-08-141-28/+28
| | | | | | | | Both normal and relaxed constexpr are required by our new minimum of C++17. Change-Id: Ic028b88a2e7a6cb7d5925f3133b9d54859a81744 Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
* Fix warning in QStringView::compare(QChar) on qsizetype->intThiago Macieira2020-07-301-1/+4
| | | | | | | | | | | | | | | | | | size() - 1 is converted to int as the result, but size() is a qsizetype and could be bigger than INT_MAX. So rewrite to not depend on the cast. This was introduced on b2f79cceb11dfd15ac9eea631bc18ad6b036eb91. I could have replaced size() - 1 with int(size() > 1) - 1, but that's even more complex. To simplify, I split the function in two while retaining the C++11 constexpr requirements. Bonus: removes the use of the ambiguously-named "empty()" function that looks like a verb. Fixes: QTBUG-85665 Pick-to: 5.15 Change-Id: Ieffc3453b88c4517a1dbfffd162338fdb084a376 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Rename from() to sliced()Lars Knoll2020-07-061-1/+1
| | | | | | | | After API discussions, agreement was that from(n) is a bad name for the method. Let's go with sliced(n) instead. Change-Id: I0338cc150148a5008c3ee72bd8fda96fb93e9c35 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Use QList instead of QVector in corelibJarek Kobus2020-06-251-1/+1
| | | | | | | | | | Applied to headers only. Source file to be changed separately. Omitted statemachine for now to avoid conflicts. Omitted qmetatype.h for now - to be handled later. Task-number: QTBUG-84469 Change-Id: I317376037a62467c313467d92955ad0b7473aa97 Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
* Don't truncate parameters passed to QStringView::{first/last/from/slice}Sona Kurazyan2020-06-191-4/+4
| | | | | | | | QStringView doesn't need to convert qsizetype parameters to int. Change-Id: Iba8b5259ab3ed7a24a57bb6748446fd3e45bb182 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Rename new slice() method sliced()Marc Mutz2020-06-111-1/+1
| | | | | | | | | | | | | | The recently-added slice() method has the problem that it's a noun as well as a verb in the imperative. Like std::vector::empty, which is both an adjective and a verb in the imperative, this may cause confusion as to what the function does. Using the passive voice form of slice(), sliced(), removes the confusion. While it can be read as an adjective, too, that doesn't change the meaning compared to the verb form. Change-Id: If0aa01acb6cf5dd5eafa8226e3ea7f7a0c9da4f1 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Ensure left/right/mid behave in a compatible wayLars Knoll2020-06-091-8/+18
| | | | | | | | | | | | | | | | | | | | QString and QStringRef did bounds checking for left/right/mid, whereas QStringView was asserting on out of bounds. Relax the behavior for QStringView and do bounds checking on pos/n as well. This removes a source of potentially hidden errors when porting from QStringRef (or QString) to QStringView. Unfortunately, one difference remains, where QByteArray::left/right() behaves differently (and somewhat more sane) than QString and QStringRef. We're keeping the difference here, as it has been around for many years. Mark left/right/mid as obsolete and to be replaced with the new first/last/slice methods. Change-Id: I18c203799ba78c928a4610a6038089f27696c22e Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* Implement first/last/from and slice() for string-like classesLars Knoll2020-06-081-0/+9
| | | | | | | | | | | | These methods are scheduled as a replacement for left/right/mid() in Qt 6 with a consistent, narrow contract that does not allow out of bounds indices, and therefore does permit faster implementations. Change-Id: Iabf22e8d4f3fef3c5e69a17f103e6cddebe420b1 Reviewed-by: Alex Blasche <alexander.blasche@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QString/View: add tokenize() member functionsMarc Mutz2020-06-061-1/+7
| | | | | | | | | [ChangeLog][QtCore][QString, QStringView, QLatin1String] Added tokenize(). Change-Id: I5fbeab0ac1809ff2974e565129b61a6bdfb398bc Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Add QStringView::split() methodsLars Knoll2020-06-011-0/+16
| | | | | | | | | | | | | Since QString::split() is not going away in Qt 6, we should aim to provide API symmetry here, and ease porting existing code from QString(Ref) to use QStringView. This is easier than having to port everything to use tokenize() at the same time. tokenize() will however lead to better performance and thus should be preferred. Change-Id: I1eb43300a90167c6e9389ab56f416f2bf7edf506 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* Add QStringView::constData()Lars Knoll2020-05-301-0/+1
| | | | | | | | | | This is doing the same as data(), but the method is widely being used in Qt (e.g. in QString), so this will make it easier to change the type of a variable from QString(Ref) to QStringView in a source compatible way. Change-Id: Ic49bef688d3ce3c550336edf90130aa5cac8b497 Reviewed-by: Marc Mutz <marc.mutz@kdab.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Add support for count() to QStringViewLars Knoll2020-05-301-0/+5
| | | | | | | Make the API more symmetric with regards to both QString and QStringRef. Change-Id: Ia67c53ba708f6c33874d1a127de8e2857ad9b5b8 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* Add toInt() and friends to QStringViewLars Knoll2020-05-291-0/+11
| | | | | | | | | Make the API more symmetric with regards to both QString and QStringRef. Having this available helps making QStringView more of a drop-in replacement for QStringRef. QStringRef is planned to get removed in Qt 6. Change-Id: Ife036c0b55970078f42e1335442ff9ee5f4a2f0d Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* QStringView: add converting constructor from array-like containersGiuseppe D'Angelo2020-05-141-35/+38
| | | | | | | | | | | | | | | | | Centralize, rather than keeping adding constructors from any array-like container. A more robust implementation, likely following the converting constructor for std::span ([span.cons]), is out of scope for C++17 and will require C++20's ranges and concepts. [ChangeLog][QtCore][QStringView] QStringView can now be constructed from any contiguous container, as long as they hold string-like data. For instance, it's now possible to create a QStringView object from a std::vector<char16_t>, a QVarLengthArray<ushort> and so on. Change-Id: I7043eb194f617e98bd1f8af1237777a93a6c5e75 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
* Drop the deprecated QStringViewLiteralGiuseppe D'Angelo2020-05-131-7/+0
| | | | | Change-Id: I6e6643b4c7cbcd43c495c332de0d6874856b9528 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
* Port qustrlen and qustrchr to char16_tMarc Mutz2020-05-121-2/+2
| | | | | | | Fix callers. Change-Id: Iaf604c890941aa3de3bd31da300b2066e65968ed Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Explain what the QStringView levels meanGiuseppe D'Angelo2020-05-091-0/+12
| | | | | | | | | Insofar the information was just in the commit message (00a8be85d168530e71511f6eb265d00fdd747cd3); add that explanation to the code itself. Change-Id: Ice1919c803a31918e8228091b3192728cbbc32f4 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QChar: add fromUcs{2,4}()Marc Mutz2020-05-091-0/+16
| | | | | | | | | | | | | | | | | | | | The fromUcs2() named ctor is designed to replace all the non-char integral-type constructors of QChar which make it very hard to control the implicit QChar conversions, which have caused a few bugs in Qt itself. As a classical named contructor, it simply returns QChar. The fromUcs4() named "ctor", however, needs to expand surrogate pairs, and thus can't just return QChar. Instead, it returns a small struct that contains one or two char16_t's, can be iterated over and be implicitly converted to QStringView. To avoid bikeshedding the name (FromUcs4Result, of course :), it's defined inline and thus can't be named outside the function. This function replaces most uses of QChar::requiresSurrogates() in QtBase. [ChangeLog][QtCore][QChar] Added fromUcs2(), fromUcs4(). Change-Id: I803708c14001040f75cb599e33c24a3fb8d2579c Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QStringView: add toNSString() / toCFString()Marc Mutz2020-05-081-0/+10
| | | | | | | | | | | | | | Use it in QString and QCocoaMenuItem. As a drive-by, s,OS X,\macos,g. [ChangeLog][QtCore][QStringView] Added toNSString(), toCFString() on macOS. Change-Id: Ib05818015a4be11a0d72d4487fb82c580d27854e Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* QStringView: adapt to C++20 constexpr std::basic_stringMarc Mutz2020-03-121-1/+1
| | | | | | | | | | | | Just mark the constructor constexpr. If std::basic_string isn't, then this will be silently dropped. If std::basic_string is, we can now construct QStringView from std::basic_strings at compile-time. [ChangeLog][QtCore][QStringView] Conversion from std::basic_string can now be constexpr (when std::basic_string is). Change-Id: Ia608f1a71c9a24f417b3e21e150ff6bd3d2f4fc4 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QLatin1String/QStringView: add (missing) member compare()Marc Mutz2020-03-051-0/+5
| | | | | | | | | | | [ChangeLog][QtCore][QLatin1String] Added compare(). [ChangeLog][QtCore][QStringView] Added compare() overloads taking QLatin1String, QChar. Change-Id: Ie2aa400299cb63495e65ce29b2a32133066de826 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Introduce QString(View)::isValidUtf16Giuseppe D'Angelo2019-12-201-0/+2
| | | | | | | | | | | | | | | | | | | QString(View)s can be built or manipulated in ways that make them contain/refer to improperly encoded UTF-16 data. Problem is, we don't have public APIs to check whether a string contains valid UTF-16. This knowledge is precious if the string is to be fed in algorithms, regular expressions, etc. that expect validated input (e.g. QRegularExpression can be faster if it can assume valid UTF-16, otherwise it has to employ extra checks). Add a function that does the validation. [ChangeLog][QtCore][QStringView] Added QStringView::isValidUtf16. [ChangeLog][QtCore][QString] Added QString::isValidUtf16. Change-Id: Idd699183f6ec08013046c76c6a5a7c524b6c6fbc Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Deprecate QStringViewLiteralMarc Mutz2019-08-101-0/+7
| | | | | | | | | | | | | | | | | As a macro, we can't directly deprecate it, but need to make it call something deprecated. That is a new ctor with a new enum type added. The type might be useful for other such ventures, so put it into qglobal.h Remove the QT_NO_UNICODE_LITERAL protection, as it's always false these days, and QT_UNICODE_LITERAL is unconditionally #defined a 20 lines above. [ChangeLog][QtCore][QStringView] Deprecated the (undocumented) QStringViewLiteral macro. Just use u"" or QStringView(u"") instead. Change-Id: I9141320225037e1bc6b7f920bf01a9d0144fdac2 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* QStringView: clean up storage_typeMarc Mutz2019-07-291-6/+0
| | | | | | | | | | | Now that all supported compilers support char16_t, we don't need the storage_type == wchar_t hack for MSVC anymore. Remove it. Adapt docs. Change-Id: I55df6c8a9fa5a9c7e6f53ba89f3850956b369061 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
* QStringView: two fixes for newly-added toWCharArray()Marc Mutz2019-07-291-1/+1
| | | | | | | | | | | | | | | | | | | | Amends e89fbd8c3aa50a24e5fc02ab710ccca67fce98e2. - While QString::data() never returns nullptr, QStringView::data() may, which makes calling QStringView{}.toWCharArray() UB on Windows (since memcpy's 2nd argument must never be nullptr, even if the size is zero). Fix by protecting the memcpy call. - QStringView, by design, does not use out-of-line member functions, because calling these forces the QStringView object onto the stack. Fix by making inline. Also use the more efficient qToStringViewIgnoringNull(), as the result does not depend on QString::isNull() (no characters are written either way), and add a missing article to the function's docs. Change-Id: I5d6b31361522812b0db8303b93c43d4b9ed11933 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* Move text-related code out of corelib/tools/ to corelib/text/Edward Welbourne2019-07-101-0/+338
This includes byte array, string, char, unicode, locale, collation and regular expressions. Change-Id: I8b125fa52c8c513eb57a0f1298b91910e5a0d786 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>