Messages in this thread | | | Date | Mon, 10 Sep 2012 08:19:21 +0200 (CEST) | From | Jan Engelhardt <> | Subject | Re: [PATCH] strings: helper for maximum decimal encoding of an unsigned integer |
| |
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")];
| |