Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD

Alejandro Colomar <[email protected]> Wed, 19 Mar 2025 17:25:40 +0100
Newsgroups dev.linux.lists.liba2i
Message-ID <6alwxn5mchma25qxvmzebx5vfheulfvgev7f7xjcjshma3hfok@3qtsvbzoljb2>
--ywj7kwbjvtzjahow
Content-Type: text/plain; protected-headers=v1; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
From: Alejandro Colomar <[email protected]>
To: [email protected]
Cc: Bruno Haible <[email protected]>, [email protected], 
	[email protected], [email protected], christos <[email protected]>, 
	=?utf-8?B?xJBvw6BuIFRy4bqnbiBDw7RuZw==?= Danh <[email protected]>, Paul Eggert <[email protected]>, 
	Eli Schwartz <[email protected]>, Guillem Jover <[email protected]>, 
	Iker Pedrosa <[email protected]>, Michael Vetter <[email protected]>, Robert Elz <[email protected]>, 
	[email protected], Sam James <[email protected]>, "Serge E. Hallyn" <[email protected]>
Subject: Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD
References: <mgcfwxfmv3kpfnkkf6uj63kx5tdzl64p2zg2us4ntsu6q5xkwj@k52z5yyiywoo>
 <18739733.sWSEgdgrri@nimes>
 <mvwnrmk2xf45ivyk4kzxdxuwdk67666yt3kwafck6vo4vq2lru@wkqmoqsacqkf>
 <[email protected]>
MIME-Version: 1.0
In-Reply-To: <[email protected]>

Hi Thorsten,

On Wed, Mar 19, 2025 at 04:56:32PM +0100, Thorsten Glaser wrote:
> On Tue, 18 Mar 2025, Alejandro Colomar wrote:
>=20
> >> To address this adoption problem, how about changing these function to
> >> generic functions (in the sense of <tgmath.h>)? In such a way that
> >>     strtoi (n, &end, base, LONG_MIN, LONG_MAX, &status)
> >> is known to return a 'long' rather than 'intmax_t', and
> >>     strtoi (n, &end, base, INT_MIN, INT_MAX, &status)
>=20
> That, and especially=E2=80=A6

Please propose an implementation of the overload-selectinging macro, and
clarify how this should work:

	n =3D strto*(s, NULL, 0, SHRT_MIN, UINT_MAX, &status);

Do we use typeof(min + max)?  Which is the type to use?

Also, aren't you worried about going inventive in the standard?  I feel
safer with this old API than one invented now.

> >I have designed a better API, through a type-generic macro:
> >
> >	int
> >	a2i(typename T, T *restrict n, QChar *s,
> >	    QChar *_Optional *restrict endp, int base,
> >	    T min, T max);
> [=E2=80=A6]
>=20
> =E2=80=A6 this is a nightmare. This effectively will prevent people from
> adding that to systems that do not use C2y as primary/only target
> yet, and mixing code.

That macro can be implemented with C11.  Here's the implementation I
wrote for shadow-utils, which we've been using and distributing for a
year already:

	#define a2i(TYPE, n, s, ...)                                              =
    \
	(                                                                         =
    \
		_Generic((void (*)(TYPE, typeof(s))) 0,                               \
			void (*)(short,              const char *):  a2sh_c,          \
			void (*)(short,              const void *):  a2sh_c,          \
			void (*)(short,              char *):        a2sh_nc,         \
			void (*)(short,              void *):        a2sh_nc,         \
			void (*)(int,                const char *):  a2si_c,          \
			void (*)(int,                const void *):  a2si_c,          \
			void (*)(int,                char *):        a2si_nc,         \
			void (*)(int,                void *):        a2si_nc,         \
			void (*)(long,               const char *):  a2sl_c,          \
			void (*)(long,               const void *):  a2sl_c,          \
			void (*)(long,               char *):        a2sl_nc,         \
			void (*)(long,               void *):        a2sl_nc,         \
			void (*)(long long,          const char *):  a2sll_c,         \
			void (*)(long long,          const void *):  a2sll_c,         \
			void (*)(long long,          char *):        a2sll_nc,        \
			void (*)(long long,          void *):        a2sll_nc,        \
			void (*)(unsigned short,     const char *):  a2uh_c,          \
			void (*)(unsigned short,     const void *):  a2uh_c,          \
			void (*)(unsigned short,     char *):        a2uh_nc,         \
			void (*)(unsigned short,     void *):        a2uh_nc,         \
			void (*)(unsigned int,       const char *):  a2ui_c,          \
			void (*)(unsigned int,       const void *):  a2ui_c,          \
			void (*)(unsigned int,       char *):        a2ui_nc,         \
			void (*)(unsigned int,       void *):        a2ui_nc,         \
			void (*)(unsigned long,      const char *):  a2ul_c,          \
			void (*)(unsigned long,      const void *):  a2ul_c,          \
			void (*)(unsigned long,      char *):        a2ul_nc,         \
			void (*)(unsigned long,      void *):        a2ul_nc,         \
			void (*)(unsigned long long, const char *):  a2ull_c,         \
			void (*)(unsigned long long, const void *):  a2ull_c,         \
			void (*)(unsigned long long, char *):        a2ull_nc,        \
			void (*)(unsigned long long, void *):        a2ull_nc         \
		)(n, s, __VA_ARGS__)                                                  \
	)

And here's the definition of one of the overloads:

	int
	a2sl_nc(long *restrict n, char *s,
	    char **restrict endp, int base, long min, long max)
	{
		int  status;

		*n =3D strtoi(s, endp, base, min, max, &status);
		if (status !=3D 0) {
			errno =3D status;
			return -1;
		}
		return 0;
	}

> (Besides, a2i is a too generic name.)

I named it like atoi(3), just replacing s/to/2/.  It is just as generic
as the name of the APIs it intends to supersede.

> bye,
> //mirabilos
> PS: Please don=E2=80=99t Cc me explicitly on this thread.

Ok.  Have a lovely day!
Alex

--=20
<https://www.alejandro-colomar.es/>

--ywj7kwbjvtzjahow
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmfa7/4ACgkQ64mZXMKQ
wqnbvg/8CTCyhlEymcbApFfZyeQtouC1+/tDfp4pOJfMm3Sfkuc8AkPx9zLNpLrK
uI3CSeGgNiVsVAx6MMLja53ck4OgTQbj73QyJRVsnS6x/TnO4WIWv40gNuu62Q+B
UzWQe6fEF72eCNEgT68H/VkSxSnDLOBr2XUit4gF2TPsHV7Q0Uw4K7X31eze8/Mz
c2/feS2yt6BSd0VKEmoV3x3RZCELKAkaJSb2H28G1BgY63ET22PS+3dzAe9akphp
LeZiGvRqPZDTYVCrOj9Jbgt2xKLggRVWT93tZi1Fypg/YxR5QnBct1UyP531J2Bb
xgpCOwi5sbyBWxNh5wYPS9crb7Gf5OErdw/KUsw0zhBJxBQqVeb419yzt96s4FAo
p370HkQWPOub87lRiIzPsOp6ZBeCnR7ORydL3fNyX1kY8DjixaGBc/FI3AztM/ud
lw7+FPEgRB7kaYccDr+/gRjMUqakH2AbrY+ig92DhhF49r/rkFvhzAdtjcW94GUc
D6FTpgcY4X2DCLil8hZkADCsvAYp0LDa1X7v6xMJYIaHInholM1XCiOfnEts5PkW
ZNfxPNhnv2eOmKvUKYQuBR9rwbbpkoLaMkTmjsjH/nkNUZssz9Ohdmvv9JRRmVIi
dWHENs1y0HJu+ksoIPCOiCzz5zKCJmn2dgwKQ9YRvhszTuqUfZE=
=DgVV
-----END PGP SIGNATURE-----

--ywj7kwbjvtzjahow--