I found a few more bugs in this function. Below is a copy of the latest
CVS version. Lesson learned: No more submissions without a test program? ;-)
#include <stddef.h>
#include <string.h>
#include "clc_assert.h"
#include "clc_string.h"
/* Copyright(c) 2003, Jan Engelhart (hir...@us...) */
/* Comments from boa:
* The previous version was broken in several ways.
* 1. We must place the result from ptr[0] and rightwards.
* If we don't and ptr[0] == '\0', nothing is printed.
* memmove fixes that.
*
* 2. The previous version wrote outside the buffer, I added (size - 1).
*/
int clc_ultostr(char *ptr, size_t size, unsigned long num, int base)
{
const char *sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* sp;
clc_assert_not_null(clc_ultostr, ptr);
clc_assert_arg(clc_ultostr, size > 1);
clc_assert_arg(clc_ultostr, base >= 2 && base <= 36);
/* Point to the last char in buffer */
sp = ptr + size - 1;
*sp = '\0';
do {
*--sp = sym[num % base];
num /= base;
} while (num > 0 && sp != ptr);
if(num > 0)
return 0;
else if(ptr != sp)
memmove(ptr, sp, size - (sp - ptr));
return 1;
}
--
boa
Please join the libclc-developers list
at http://lists.sourceforge.net/lists/listinfo/libclc-developers
|