C++ Logo

std-proposals

Advanced search

Re: [std-proposals] string_view support for std::stoi and friends

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sat, 11 Jan 2025 15:05:29 -0300
On Saturday 11 January 2025 14:06:26 Brasilia Standard Time Lénárd Szolnoki
via Std-Proposals wrote:
> strtoll is also technically locale-dependent, but I don't know what that
> means in practice. In what locales (if any) is the behavior different?

According to the strtoll_l man page I've found, the functions use <ctype.h>:
isspace, isalpha, isdigit. This version[1] of FreeBSD's strtoll.c uses all
three, but the version[2] I had imported into Qt only used isspace(). Then
again, the older version also stopped parsing on !isascii(c). The current
version[3] still only uses isascii_l(c) and parses as ASCII.

This means, in theory, an implementation could parse other characters and
digits. The C standard does allow it, 7.22.1.4p6 "In other than the "C"
locale, additional locale-specific subject sequence forms may be accepted". So,
for example, in a legacy 8-bit encoding could interpret non-ASCII digits (such
as ISO-8859-6 and -8 for Arabic and Hebrew, respectively). In practice, given
the switch to UTF-8 in the Unix world in the early 2000s and the upcoming
switch on Windows in the late 2020s or early 2030s, that ability has a limited
lifetime.

That goes true for wcstol() as well. For example, the current FreeBSD
implementation[4] has an #ifdef notyet on converting non-ASCII digits to an
integer value. The glibc implementation doesn't have that at all.

Therefore, I'd suggest the C++ implementation be described as requiring
compatibility to the ISO C23 implementation (which includes the "0b" prefix)
and that support for non-ASCII is implementation-defined. That way,
implementations can decide if they need to do anything, if the platforms they
support have non-ASCII support and if they care about it.

[1] https://socsvn.freebsd.org/socsvn/mirror/FreeBSD/user/mjacob/sys/libkern/
strtol.c
[2] https://github.com/freebsd/freebsd-src/blob/
18b29f3fb8abee5d57ed8f4a44f806bec7e0eeff/lib/libc/stdlib/strtoll.c
[3] https://github.com/freebsd/freebsd-src/blob/master/lib/libc/stdlib/
strtoll.c
[4] https://github.com/freebsd/freebsd-src/blob/master/lib/libc/locale/
wcstol.c
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2025-01-11 18:05:34