Re: [Libclc-developers] new function: clc_strrev
Status: Planning
Brought to you by:
augestad
|
From: <bo...@me...> - 2003-03-23 12:25:02
|
Hallvard B Furuseth wrote:
> Bjørn Augestad writes:
>
>>We can't have a string module without a strrev function, can we?
>>I could not find a proposal for one on google, so here is my version.
>>q1: Do we need it?
>
>
> I don't think so. I can only remember needing a rev function once or
> twice in my life. I have heroically refrained from posting a few nice
> libclc functions I've rarely if ever needed myself:-)
>
Some sales pitches for clc_strrev. :-)
1. It seems to be a common problem for newbies to reverse a string, lots
of people have asked about this over the years. We can help them out by
providing it.
2. Microsoft has one (_strrev), ANSI C does not. clc_strrev can aid
portability.
3. Some functions can be implemented in a clearer way if we have a
clc_strrev. Consider the clc_ultostr() which in the latest version
writes from the end of buffer to the beginning and then calls memmove to
adjust the buffer. clc_ultostr() could be changed to writing from the
beginning of the buffer and then just reverse the output before
returning. Here's a quick&dirty implementation :
int clc_ultostr(char *ptr, size_t size, unsigned long num, int base)
{
const char *sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* sp;
sp = ptr;
size--; /* reserve one for \0 */
do {
*sp++ = sym[num % base];
num /= base;
} while (num > 0 && size-- > 0);
if(num > 0)
return 0;
*sp = '\0';
clc_strrev(ptr);
return 1;
}
I ran my test program with both versions. The clc_strrev version is 5%
faster on one of my machines than the original which uses memmove. ;-)
I think you should post the nice functions as well. Never know if
someone needs them or suddenly finds a new way of doing things. :-)
--
boa
Please join the libclc-developers list
at http://lists.sourceforge.net/lists/listinfo/libclc-developers
|