lkml.org 
[lkml]   [2012]   [Sep]   [10]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    /
    Date
    From
    SubjectRe: [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")];


    \
     
     \ /
      Last update: 2012-09-10 09:01    [W:5.413 / U:0.008 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site