summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstring.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2022-04-14 19:29:40 +0200
committerMarc Mutz <marc.mutz@qt.io>2022-04-28 08:40:19 +0000
commite48b4d2b7c1d99ba8962a9c10a1508d16dc33fbf (patch)
tree520952b109a14e1bbddf51771b6ae6602ba3a614 /src/corelib/text/qstring.cpp
parent7a5d7e8440a1e4beec49b751d0ecacdf55aa45d5 (diff)
Implement support for '0b' prefix in toInt() etc
[ChangeLog][QtCore][QByteArray/QByteArrayView/QLatin1String/QString/QStringView] The string-to-integer conversion functions (toInt() etc) now support the 0b prefix for binary literals. That means that base = 0 will recognize 0b to mean base = 2 and an explicit base = 2 argument will make toInt() (etc) skip an optional 0b. [ChangeLog][QtCore][Important Behavior Changes] Due to the newly-introduced support for 0b (binary) prefixes in integer parsing, some strings that were previously rejected as invalid now parse as valid. E.g., Qt 6.3 with autodetected bases would have tried to parse "0b1" as an octal value and fail, whereas 6.4 will parse it as the binary literal and return 1. Fixes: QTBUG-85002 Change-Id: Id4eff72d63619080e5afece4d059b6ffd52f28c8 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'src/corelib/text/qstring.cpp')
-rw-r--r--src/corelib/text/qstring.cpp77
1 files changed, 52 insertions, 25 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index a350401b30..00f899ee99 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -7111,9 +7111,10 @@ QString QString::vasprintf(const char *cformat, va_list ap)
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
- If \a base is 0, the C language convention is used: if the string
- begins with "0x", base 16 is used; otherwise, if the string begins with "0",
- base 8 is used; otherwise, base 10 is used.
+ If \a base is 0, the C language convention is used: if the string begins
+ with "0x", base 16 is used; otherwise, if the string begins with "0b", base
+ 2 is used; otherwise, if the string begins with "0", base 8 is used;
+ otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For
locale-dependent conversion use QLocale::toLongLong()
@@ -7124,6 +7125,8 @@ QString QString::vasprintf(const char *cformat, va_list ap)
This function ignores leading and trailing whitespace.
+ \note Support for the "0b" prefix was added in Qt 6.4.
+
\sa number(), toULongLong(), toInt(), QLocale::toLongLong()
*/
@@ -7153,9 +7156,10 @@ qlonglong QString::toIntegral_helper(QStringView string, bool *ok, int base)
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
- If \a base is 0, the C language convention is used: if the string
- begins with "0x", base 16 is used; otherwise, if the string begins with "0",
- base 8 is used; otherwise, base 10 is used.
+ If \a base is 0, the C language convention is used: if the string begins
+ with "0x", base 16 is used; otherwise, if the string begins with "0b", base
+ 2 is used; otherwise, if the string begins with "0", base 8 is used;
+ otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For
locale-dependent conversion use QLocale::toULongLong()
@@ -7166,6 +7170,8 @@ qlonglong QString::toIntegral_helper(QStringView string, bool *ok, int base)
This function ignores leading and trailing whitespace.
+ \note Support for the "0b" prefix was added in Qt 6.4.
+
\sa number(), toLongLong(), QLocale::toULongLong()
*/
@@ -7196,9 +7202,10 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
- If \a base is 0, the C language convention is used: if the string
- begins with "0x", base 16 is used; otherwise, if the string begins with "0",
- base 8 is used; otherwise, base 10 is used.
+ If \a base is 0, the C language convention is used: if the string begins
+ with "0x", base 16 is used; otherwise, if the string begins with "0b", base
+ 2 is used; otherwise, if the string begins with "0", base 8 is used;
+ otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For
locale-dependent conversion use QLocale::toLongLong()
@@ -7209,6 +7216,8 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
This function ignores leading and trailing whitespace.
+ \note Support for the "0b" prefix was added in Qt 6.4.
+
\sa number(), toULong(), toInt(), QLocale::toInt()
*/
@@ -7222,9 +7231,10 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
- If \a base is 0, the C language convention is used: if the string
- begins with "0x", base 16 is used; otherwise, if the string begins with "0",
- base 8 is used; otherwise, base 10 is used.
+ If \a base is 0, the C language convention is used: if the string begins
+ with "0x", base 16 is used; otherwise, if the string begins with "0b", base
+ 2 is used; otherwise, if the string begins with "0", base 8 is used;
+ otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For
locale-dependent conversion use QLocale::toULongLong()
@@ -7235,6 +7245,8 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
This function ignores leading and trailing whitespace.
+ \note Support for the "0b" prefix was added in Qt 6.4.
+
\sa number(), QLocale::toUInt()
*/
@@ -7247,9 +7259,10 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
- If \a base is 0, the C language convention is used: if the string
- begins with "0x", base 16 is used; otherwise, if the string begins with "0",
- base 8 is used; otherwise, base 10 is used.
+ If \a base is 0, the C language convention is used: if the string begins
+ with "0x", base 16 is used; otherwise, if the string begins with "0b", base
+ 2 is used; otherwise, if the string begins with "0", base 8 is used;
+ otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For
locale-dependent conversion use QLocale::toInt()
@@ -7260,6 +7273,8 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
This function ignores leading and trailing whitespace.
+ \note Support for the "0b" prefix was added in Qt 6.4.
+
\sa number(), toUInt(), toDouble(), QLocale::toInt()
*/
@@ -7272,9 +7287,10 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
- If \a base is 0, the C language convention is used: if the string
- begins with "0x", base 16 is used; otherwise, if the string begins with "0",
- base 8 is used; otherwise, base 10 is used.
+ If \a base is 0, the C language convention is used: if the string begins
+ with "0x", base 16 is used; otherwise, if the string begins with "0b", base
+ 2 is used; otherwise, if the string begins with "0", base 8 is used;
+ otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For
locale-dependent conversion use QLocale::toUInt()
@@ -7285,6 +7301,8 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
This function ignores leading and trailing whitespace.
+ \note Support for the "0b" prefix was added in Qt 6.4.
+
\sa number(), toInt(), QLocale::toUInt()
*/
@@ -7298,9 +7316,10 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
- If \a base is 0, the C language convention is used: if the string
- begins with "0x", base 16 is used; otherwise, if the string begins with "0",
- base 8 is used; otherwise, base 10 is used.
+ If \a base is 0, the C language convention is used: if the string begins
+ with "0x", base 16 is used; otherwise, if the string begins with "0b", base
+ 2 is used; otherwise, if the string begins with "0", base 8 is used;
+ otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For
locale-dependent conversion use QLocale::toShort()
@@ -7311,6 +7330,8 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
This function ignores leading and trailing whitespace.
+ \note Support for the "0b" prefix was added in Qt 6.4.
+
\sa number(), toUShort(), toInt(), QLocale::toShort()
*/
@@ -7324,9 +7345,10 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
- If \a base is 0, the C language convention is used: if the string
- begins with "0x", base 16 is used; if the string begins with "0",
- base 8 is used; otherwise, base 10 is used.
+ If \a base is 0, the C language convention is used: if the string begins
+ with "0x", base 16 is used; otherwise, if the string begins with "0b", base
+ 2 is used; otherwise, if the string begins with "0", base 8 is used;
+ otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For
locale-dependent conversion use QLocale::toUShort()
@@ -7337,6 +7359,8 @@ qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base)
This function ignores leading and trailing whitespace.
+ \note Support for the "0b" prefix was added in Qt 6.4.
+
\sa number(), toShort(), QLocale::toUShort()
*/
@@ -10210,7 +10234,8 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
If \a base is 0, the base is determined automatically using the following
rules: if the Latin-1 string begins with "0x", the rest of it is read as
- hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is
+ hexadecimal (base 16); otherwise, if it begins with "0b", the rest of it is
+ read as binary (base 2); otherwise, if it begins with "0", the rest of it is
read as octal (base 8); otherwise it is read as decimal.
Returns 0 if the conversion fails.
@@ -10225,6 +10250,8 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
This function ignores leading and trailing spacing characters.
//! [latin1-numeric-conversion-note]
+
+ \note Support for the "0b" prefix was added in Qt 6.4.
*/
/*!