summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qlocale_tools.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/qlocale_tools.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/qlocale_tools.cpp')
-rw-r--r--src/corelib/text/qlocale_tools.cpp40
1 files changed, 26 insertions, 14 deletions
diff --git a/src/corelib/text/qlocale_tools.cpp b/src/corelib/text/qlocale_tools.cpp
index 8259878448..7a2cce6f2c 100644
--- a/src/corelib/text/qlocale_tools.cpp
+++ b/src/corelib/text/qlocale_tools.cpp
@@ -401,30 +401,42 @@ double qt_asciiToDouble(const char *num, qsizetype numLen, bool &ok, int &proces
return d;
}
-/* Detect base if 0 and, if base is hex, skip over 0x prefix */
+/* Detect base if 0 and, if base is hex or bin, skip over 0x/0b prefixes */
static auto scanPrefix(const char *p, const char *stop, int base)
{
+ struct R
+ {
+ const char *next;
+ int base;
+ };
if (p < stop && *p >= '0' && *p <= '9') {
if (*p == '0') {
- const char *x = p + 1;
- if (x < stop && (*x == 'x' || *x == 'X')) {
- if (base == 0)
- base = 16;
- if (base == 16)
- p += 2;
- } else if (base == 0) {
- base = 8;
+ const char *x_or_b = p + 1;
+ if (x_or_b < stop) {
+ switch (*x_or_b) {
+ case 'b':
+ case 'B':
+ if (base == 0)
+ base = 2;
+ if (base == 2)
+ p += 2;
+ return R{p, base};
+ case 'x':
+ case 'X':
+ if (base == 0)
+ base = 16;
+ if (base == 16)
+ p += 2;
+ return R{p, base};
+ }
}
+ if (base == 0)
+ base = 8;
} else if (base == 0) {
base = 10;
}
Q_ASSERT(base);
}
- struct R
- {
- const char *next;
- int base;
- };
return R{p, base};
}