From 8481500f639e3d5e2259db57847a2e7068e30650 Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Wed, 17 Jun 2015 11:07:14 +0200 Subject: [PATCH] Improve QString doc when using non-spaced numbered place marker Currently when a developer uses a string like QString("%1%3%2").arg(x).arg(y).arg(z) he can be bitten by the sequential replacement done by QString. Adding an example with a little explanation should help future Qt user avoid generating buggy strings. Task-number: QTBUG-44044 Change-Id: I81e20af8d9fb2a07e12ec61dcd5bb4544d863777 Reviewed-by: Sze Howe Koh --- src/corelib/doc/snippets/qstring/main.cpp | 13 +++++++++++++ src/corelib/tools/qstring.cpp | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/corelib/doc/snippets/qstring/main.cpp b/src/corelib/doc/snippets/qstring/main.cpp index 4687d52e54..e03e705a0b 100644 --- a/src/corelib/doc/snippets/qstring/main.cpp +++ b/src/corelib/doc/snippets/qstring/main.cpp @@ -261,6 +261,19 @@ void Widget::argFunction() str.arg("%1f").arg("Hello"); // returns "Hellof %2" //! [13] + //! [97] + str = "%1%3%2"; + str.arg("Hello").arg(20).arg(50); // returns "Hello500" + + str = "%1%2%3"; + str.arg("Hello").arg(50).arg(20); // returns "Hello5020" + //! [97] + + //! [98] + str = "%1%2%3"; + str.arg("Hello", QString::number(20), QString::number(50)); // returns "Hello5020" + //! [98] + //! [14] str = QString("Decimal 63 is %1 in hexadecimal") .arg(63, 0, 16); diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index c933e261cc..bbb5647eea 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -7206,6 +7206,25 @@ QString QString::arg(const QString &a, int fieldWidth, QChar fillChar) const difference if \a a1 contains e.g. \c{%1}: \snippet qstring/main.cpp 13 + + A similar problem occurs when the numbered place markers are not + white space separated: + + \snippet qstring/main.cpp 12 + \snippet qstring/main.cpp 97 + + Let's look at the substitutions: + \list + \li First, \c Hello replaces \c {%1} so the string becomes \c {"Hello%3%2"}. + \li Then, \c 20 replaces \c {%2} so the string becomes \c {"Hello%320"}. + \li Since the maximum numbered place marker value is 99, \c 50 replaces \c {%32}. + \endlist + Thus the string finally becomes \c {"Hello500"}. + + In such cases, the following yields the expected results: + + \snippet qstring/main.cpp 12 + \snippet qstring/main.cpp 98 */ /*!