summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstring.cpp
diff options
context:
space:
mode:
authorhjk <hjk121@nokiamail.com>2014-08-25 14:21:30 +0200
committerhjk <hjk121@nokiamail.com>2014-10-02 16:26:14 +0200
commit7dd394a16c51d11cd3ce91e288253ad137190994 (patch)
tree915432b9ed48de11122a3bcccfd2b13c7c6d44aa /src/corelib/tools/qstring.cpp
parent37597494666e6968ba7b2d946cdaf357069ec0b3 (diff)
Add a macro to disable only some 8 bit/16 bit string conversions
This is an opt-in trade-off between type safety and user code convenience. QT_NO_CAST_FROM_ASCII is highly beneficial to avoid unintended conversions from 8 bit data with potentially "unsuitable" encodings to QString. However, it has the undesirable side-effect to require user code to wrap character and string literals in QLatin1Char(...) and QLatin1String(...) or use similar construction, cluttering the code significantly. This patch introduces a QT_RESTRICTED_CAST_FROM_ASCII macro that works almost as QT_NO_CAST_FROM_ASCII, except that it enables the QChar(char) constructor and adds an additional QString(const char (&ch)[N]) constructor that matches C++ string literals, but no arbitrary character pointers. This avoids a significant share of the need to clutter the user code by only a slight relaxation of the type-safety. [ChangeLog][QtCore][QString] Added QT_RESTRICTED_CAST_FROM_ASCII macro as less intrusive alternative to QT_NO_CAST_FROM_ASCII. Change-Id: Iac72f1f90f81fbcae9bfb1fe68b0fec6ffb36c50 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com> Reviewed-by: Kai Koehne <kai.koehne@digia.com> Reviewed-by: Eike Ziller <eike.ziller@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r--src/corelib/tools/qstring.cpp40
1 files changed, 34 insertions, 6 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index 2067605646..95e45c8d93 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -732,12 +732,30 @@ inline char qToLower(char ch)
const QString::Null QString::null = { };
/*!
+ \macro QT_RESTRICTED_CAST_FROM_ASCII
+ \relates QString
+
+ Defining this macro disables most automatic conversions from source
+ literals and 8-bit data to unicode QStrings, but allows the use of
+ the \c{QChar(char)} and \c{QString(const char (&ch)[N]} constructors,
+ and the \c{QString::operator=(const char (&ch)[N])} assignment operator
+ giving most of the type-safety benefits of QT_NO_CAST_FROM_ASCII
+ but does not require user code to wrap character and string literals
+ with QLatin1Char, QLatin1String or similar.
+
+ Using this macro together with source strings outside the 7-bit range,
+ non-literals, or literals with embedded NUL characters is undefined.
+
+ \sa QT_NO_CAST_FROM_ASCII, QT_NO_CAST_TO_ASCII
+*/
+
+/*!
\macro QT_NO_CAST_FROM_ASCII
\relates QString
Disables automatic conversions from 8-bit strings (char *) to unicode QStrings
- \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_BYTEARRAY
+ \sa QT_NO_CAST_TO_ASCII, QT_RESTRICTED_CAST_FROM_ASCII, QT_NO_CAST_FROM_BYTEARRAY
*/
/*!
@@ -746,7 +764,7 @@ const QString::Null QString::null = { };
disables automatic conversion from QString to 8-bit strings (char *)
- \sa QT_NO_CAST_FROM_ASCII, QT_NO_CAST_FROM_BYTEARRAY
+ \sa QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII, QT_NO_CAST_FROM_BYTEARRAY
*/
/*!
@@ -760,7 +778,7 @@ const QString::Null QString::null = { };
Note: This only works for compilers that support warnings for
deprecated API.
- \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII
+ \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII
*/
/*!
@@ -994,6 +1012,9 @@ const QString::Null QString::null = { };
\list
\li \c QT_NO_CAST_FROM_ASCII disables automatic conversions from
C string literals and pointers to Unicode.
+ \li \c QT_RESTRICTED_CAST_FROM_ASCII allows automatic conversions
+ from C characters and character arrays, but disables automatic
+ conversions from character pointers to Unicode.
\li \c QT_NO_CAST_TO_ASCII disables automatic conversion from QString
to C strings.
\endlist
@@ -1311,6 +1332,12 @@ const QString::Null QString::null = { };
can be useful if you want to ensure that all user-visible strings
go through QObject::tr(), for example.
+ \note Defining QT_RESTRICTED_CAST_FROM_ASCII also disables
+ this constructor, but enables a \c{QString(const char (&ch)[N])}
+ constructor instead. Using non-literal input, or input with
+ embedded NUL characters, or non-7-bit characters is undefined
+ in this case.
+
\sa fromLatin1(), fromLocal8Bit(), fromUtf8()
*/
@@ -1749,10 +1776,11 @@ QString &QString::operator=(const QString &other)
Assigns \a str to this string. The const char pointer is converted
to Unicode using the fromUtf8() function.
- You can disable this operator by defining \c
- QT_NO_CAST_FROM_ASCII when you compile your applications. This
- can be useful if you want to ensure that all user-visible strings
+ You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
+ or \c QT_RESTRICTED_CAST_FROM_ASCII when you compile your applications.
+ This can be useful if you want to ensure that all user-visible strings
go through QObject::tr(), for example.
+
*/
/*! \fn QString &QString::operator=(char ch)