From d2833a3ce5af725d66ef9338f2a61b766dd3cb2d Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Tue, 2 Jun 2020 15:51:15 +0200 Subject: Ensure left/right/mid behave in a compatible way MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/corelib/tools/qvector.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/corelib/tools/qvector.h') diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index c2cdcba41d..b502a86442 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -710,8 +710,10 @@ int QVector::lastIndexOf(const T &t, int from) const noexcept template inline QVector QVector::mid(int pos, int len) const { + qsizetype p = pos; + qsizetype l = len; using namespace QtPrivate; - switch (QContainerImplHelper::mid(d.size, &pos, &len)) { + switch (QContainerImplHelper::mid(d.size, &p, &l)) { case QContainerImplHelper::Null: case QContainerImplHelper::Empty: return QVector(); @@ -722,8 +724,8 @@ inline QVector QVector::mid(int pos, int len) const } // Allocate memory - DataPointer copied(Data::allocate(len)); - copied->copyAppend(constBegin() + pos, constBegin() + pos + len); + DataPointer copied(Data::allocate(l)); + copied->copyAppend(constBegin() + p, constBegin() + p + l); return copied; } -- cgit v1.2.3