diff options
author | Dave Beckett <dave@dajobe.org> | 2011-08-31 21:27:50 -0700 |
---|---|---|
committer | Dave Beckett <dave@dajobe.org> | 2011-08-31 21:27:50 -0700 |
commit | 874133e19bc0095f7206dd95e4faf50a688c264f (patch) | |
tree | f68e33c0c573a8ed6cf51f42b8e711e7cbc537c1 /src/snprintf.c | |
parent | 3744a8337ccb624f43f365d9af13cbfba75bac41 (diff) | |
download | raptor-874133e19bc0095f7206dd95e4faf50a688c264f.tar.gz |
(raptor_format_hexadecimal): Added for formatting uppercase hex in a
fixed width field.
(raptor_iostream_hexadecimal_write): Use raptor_format_hexadecimal()
(raptor_xml_escape_string_any): Use raptor_format_hexadecimal() to
remove a sprintf for &#xXX.
Diffstat (limited to 'src/snprintf.c')
-rw-r--r-- | src/snprintf.c | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/snprintf.c b/src/snprintf.c index 0d7c858a..41b656fb 100644 --- a/src/snprintf.c +++ b/src/snprintf.c @@ -392,7 +392,7 @@ raptor_vasprintf(char **ret, const char *format, va_list arguments) } -static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; +static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /** * raptor_format_integer: @@ -444,3 +444,51 @@ raptor_format_integer(char* buffer, size_t bufsize, int integer) return len; } + + + +/** + * raptor_format_hexadecimal: + * @buffer: buffer (or NULL) + * @bufsize: size of above (or 0) + * @integer: unsigned integer value to format + * @width: width of output + * + * INTERNAL - Format an integer as uppercase hexadecimal into a + * buffer or calculate the size needed. + * + * Works Like the C99 snprintf() but just for hexadecimal integers and + * always writes @width hex digits. + * + * If @buffer is NULL or the @bufsize is too small, the number of + * bytes needed (excluding NUL) is returned and no formatting is done. + * + * Return value: number of bytes needed or written (excluding NUL) + */ +int +raptor_format_hexadecimal(char* buffer, size_t bufsize, + unsigned int integer, int width) +{ + char *p; + + if(width < 1) + return 1; + + if(!buffer || bufsize < RAPTOR_GOOD_CAST(size_t, (width + 1))) /* for NUL */ + return width; + + p = &buffer[width]; + *p-- = '\0'; + do { + *p-- = digits[integer & 15]; + integer >>= 4; + } while(integer); + + while(p >= buffer) + *p-- = '0'; + + return width; +} + + + |