From f3c340c276847cbf69ef0a10d33e970ef1371bde Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 15 Mar 2022 07:46:03 +0100 Subject: [PATCH] QStringTokenizer::toContainer(): allow more types of target containers The previous constraint called for the value_type of the container to exactly match the value_type of the tokenizer, which means toContainer() could only ever work on containers of views. But there is value in allowing QStringList, even though it works only on QL1S needles (QStringView -> QString isn't implicit). But users may have other types that for better or worse implicitly convert from views, so we shouldn't over-constrain the function. [ChangeLog][QtCore][QStringTokenizer] toContainer() now works on containers whose value_type can be constructed from the tokenizer's value_type. It no longer requires an exact match. Fixes: QTBUG-101702 Change-Id: Ie384cd1c4b51eaa57675f2a014141ceec8651c81 Reviewed-by: Fabian Kosmale Reviewed-by: Qt CI Bot --- src/corelib/text/qstringtokenizer.h | 2 +- .../text/qstringtokenizer/tst_qstringtokenizer.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/corelib/text/qstringtokenizer.h b/src/corelib/text/qstringtokenizer.h index d14099ffbbe..0b98f0646f1 100644 --- a/src/corelib/text/qstringtokenizer.h +++ b/src/corelib/text/qstringtokenizer.h @@ -292,7 +292,7 @@ class QStringTokenizer using if_haystack_not_pinned = typename if_haystack_not_pinned_impl::type; template ()))> using if_compatible_container = typename std::enable_if< - std::is_same< + std::is_convertible< typename Base::value_type, typename std::iterator_traits::value_type >::value, diff --git a/tests/auto/corelib/text/qstringtokenizer/tst_qstringtokenizer.cpp b/tests/auto/corelib/text/qstringtokenizer/tst_qstringtokenizer.cpp index 1d2658c38f2..e1619fee4d4 100644 --- a/tests/auto/corelib/text/qstringtokenizer/tst_qstringtokenizer.cpp +++ b/tests/auto/corelib/text/qstringtokenizer/tst_qstringtokenizer.cpp @@ -145,6 +145,19 @@ void tst_QStringTokenizer::toContainer() const auto v = tok.toContainer(); QVERIFY((std::is_same_v>)); } + // QLatin1String value_type into QStringList + { + auto tok = qTokenize(QLatin1String{"a,b,c"}, u','); + QStringList result; + tok.toContainer(result); + QCOMPARE(result, QStringList({"a", "b", "c"})); + } + // QLatin1String value_type into QStringList: rvalue overload + { + QStringList result; + qTokenize(QLatin1String{"a,b,c"}, u',').toContainer(result); + QCOMPARE(result, QStringList({"a", "b", "c"})); + } } QTEST_APPLESS_MAIN(tst_QStringTokenizer)