[Libclc-developers] clc_ultostr(3)
Status: Planning
Brought to you by:
augestad
|
From: <bo...@me...> - 2003-03-20 17:14:04
|
Sorry for the odd reply, email problems.
Jan Engelhardt wrote:
> #include <errno.h>
> #include <stdio.h>
>
> char *clc_ultostr(char *ptr, size_t size, unsigned long num, int base) {
> char *sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
> *sp = ptr + size - 1;
> clc_assert_not_null(clc_ultostr, ptr);
> clc_assert_arg(clc_ultostr, size > 1);
> clc_assert_arg(clc_ultostr, base < 2 || base > 36);
> *sp = '\0';
> while(size-- > 0 && num > 0) {
> *--sp = sym[num % base];
> num /= base;
> }
> while(--size > 0) { *--sp = ' '; }
> return ptr;
> }
>
> //==[ End of file ]====================================================
>
>
> Bj/orn: YOU are _now_ responsible for the indent style! :P
Thanks. :-)
>
> I bet there is still errors in there...any?
Hmm, let's see...
1. The assert(base < 2 || base > 36) is incorrect.
Should have been >= 2 && base <= 36
2. stddef.h and clc_assert.h is missing and stdio.h and errno.h
is not needed
3. nit pick. char* sym can be const char* sym.
Always a few lints :-) Here's my version.
#include <stddef.h>
#include "clc_assert.h"
#include "clc_string.h"
char *clc_ultostr(char *ptr, size_t size, unsigned long num, int base)
{
const char *sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *sp = ptr + size - 1;
clc_assert_not_null(clc_ultostr, ptr);
clc_assert_arg(clc_ultostr, size > 1);
clc_assert_arg(clc_ultostr, base >= 2 && base <= 36);
*sp = '\0';
while (size-- > 0 && num > 0) {
*--sp = sym[num % base];
num /= base;
}
while (--size > 0) {
*--sp = ' ';
}
return ptr;
}
--
boa
Please join the libclc-developers list
at http://lists.sourceforge.net/lists/listinfo/libclc-developers
|