summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstring.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2023-09-25 11:37:53 -0700
committerThiago Macieira <thiago.macieira@intel.com>2023-10-25 12:01:58 -0700
commit2fefc8c63c1090925986e7650626a91af308d82e (patch)
treed16dee562b35faeea01f23d428d6ec3da8a4cd73 /src/corelib/text/qstring.h
parent1cce2cceffed30c3300ceffe85c0aba201064979 (diff)
QString/QByteArray: add lvalue and rvalue overloads of left/mid/right
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>
Diffstat (limited to 'src/corelib/text/qstring.h')
-rw-r--r--src/corelib/text/qstring.h34
1 files changed, 31 insertions, 3 deletions
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h
index 411430a189..6ec7baf404 100644
--- a/src/corelib/text/qstring.h
+++ b/src/corelib/text/qstring.h
@@ -331,17 +331,45 @@ public:
#if QT_CONFIG(regularexpression)
[[nodiscard]] QString section(const QRegularExpression &re, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const;
#endif
- [[nodiscard]] QString left(qsizetype n) const;
- [[nodiscard]] QString right(qsizetype n) const;
- [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const;
#if QT_CORE_REMOVED_SINCE(6, 7)
+ QString left(qsizetype n) const;
+ QString right(qsizetype n) const;
+ QString mid(qsizetype position, qsizetype n = -1) const;
+
QString first(qsizetype n) const;
QString last(qsizetype n) const;
QString sliced(qsizetype pos) const;
QString sliced(qsizetype pos, qsizetype n) const;
QString chopped(qsizetype n) const;
#else
+ [[nodiscard]] QString left(qsizetype n) const &
+ {
+ if (size_t(n) >= size_t(size()))
+ return *this;
+ return first(n);
+ }
+ [[nodiscard]] QString left(qsizetype n) &&
+ {
+ if (size_t(n) >= size_t(size()))
+ return std::move(*this);
+ return std::move(*this).first(n);
+ }
+ [[nodiscard]] QString right(qsizetype n) const &
+ {
+ if (size_t(n) >= size_t(size()))
+ return *this;
+ return last(n);
+ }
+ [[nodiscard]] QString right(qsizetype n) &&
+ {
+ if (size_t(n) >= size_t(size()))
+ return std::move(*this);
+ return std::move(*this).last(n);
+ }
+ [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const &;
+ [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) &&;
+
[[nodiscard]] QString first(qsizetype n) const &
{ verify(0, n); return sliced(0, n); }
[[nodiscard]] QString last(qsizetype n) const &