Re: [PATCH] strings: helper for maximum decimal encoding of anunsigned integer

From: Jan Engelhardt
Date: Mon Sep 10 2012 - 02:19:16 EST


On Tuesday 2012-08-21 23:29, J. Bruce Fields wrote:

>I've seen a couple examples recently where we've gotten this wrong.
>Maybe something like this would help? Is there some better way?
>(Approximation due to Jim Rees).
>
>+/*
>+ * length of the decimal representation of an unsigned integer. Just an
>+ * approximation, but it's right for types of size 1 to 36 bytes:
>+ */
>+#define base10len(i) (sizeof(i) * 24 / 10 + 1)


gcc provides... "interesting" features at times.

/* for unsigned "i"s */
#define base10len(i) ((const int[]){1,3,5,8,10,13,15,17,20}[i])

But enumerating it (using your ULONG_MAX_STR proposal) should be
sufficient, since there is only a limited number of types that get e.g.
sprintf'ed into a buffer.


Something else tho, it seems some don't know that sizeof("") is already
1, and an extra \0 is often unneeded, like in:

arch/um/drivers/net_user.c: char addr_buf[sizeof("255.255.255.255\0")];
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/