From 71832cfe6fa0005312f9a648f97d7ae9613a591f Mon Sep 17 00:00:00 2001 From: Florian Vichot Date: Tue, 23 Mar 2010 20:24:58 +0100 Subject: Use the default codec with QString::vsprintf() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QString::vsprintf was not decoding the format string according to QTextCodec::codecForCString, thus making qDebug("ééé") display improperly, even if using a utf8 terminal, utf8 source files and a codec for cstrings configured as utf8. Added a unit test. Merge-request: 530 Reviewed-by: Benjamin Poulain --- src/corelib/tools/qstring.cpp | 11 +++++++++++ tests/auto/qstring/tst_qstring.cpp | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 464e77b..2f12b80 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -5104,8 +5104,19 @@ QString &QString::vsprintf(const char* cformat, va_list ap) const char *c = cformat; for (;;) { // Copy non-escape chars to result +#ifndef QT_NO_TEXTCODEC + int i = 0; + while (*(c + i) != '\0' && *(c + i) != '%') + ++i; + if (codecForCStrings) + result.append(codecForCStrings->toUnicode(c, i)); + else + result.append(fromLatin1(c, i)); + c += i; +#else while (*c != '\0' && *c != '%') result.append(QLatin1Char(*c++)); +#endif if (*c == '\0') break; diff --git a/tests/auto/qstring/tst_qstring.cpp b/tests/auto/qstring/tst_qstring.cpp index d79ebb9..1bea4b7 100644 --- a/tests/auto/qstring/tst_qstring.cpp +++ b/tests/auto/qstring/tst_qstring.cpp @@ -938,6 +938,11 @@ void tst_QString::sprintf() // Check utf8 conversion for %s QCOMPARE(a.sprintf("%s", "\303\266\303\244\303\274\303\226\303\204\303\234\303\270\303\246\303\245\303\230\303\206\303\205"), QString("\366\344\374\326\304\334\370\346\345\330\306\305")); + // Check codecForCStrings is used to read non-modifier sequences in the format string + QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); + QCOMPARE(a.sprintf("\303\251\303\250\303\240 %s", "\303\251\303\250\303\240"), QString("\303\251\303\250\303\240 \303\251\303\250\303\240")); + QTextCodec::setCodecForCStrings(0); + int n1; a.sprintf("%s%n%s", "hello", &n1, "goodbye"); QCOMPARE(n1, 5); -- cgit v0.12