Re: [Libclc-developers] new function: clc_strrev
Status: Planning
Brought to you by:
augestad
|
From: Hallvard B F. <h.b...@us...> - 2003-03-23 13:40:41
|
Bj=F8rn Augestad writes:
> Some sales pitches for clc_strrev. :-)
OK.
In that case, here is a smaller version. I added clc_strnrev() too
since I imagine that can be useful sometimes, or at least faster when
we already know strlen.
BTW, I did as you said and replaced #include "clc_*" with <clc_*>:-)
Note: The 'if(len)' is a very slight optimization, it can be omitted
(and the end-- moved) at the price of an extra loop for uneven-length
strings.
/* $Id$ */
/*
* Copyright(c) 2003, Hallvard B Furuseth <h.b...@us...>
*/
#include <string.h>
#include <clc_assert.h>
#include <clc_string.h>
char *
clc_strrev(char *s)
{
clc_assert_not_null(clc_strrev, s);
return clc_strnrev(s, strlen(s));
}
char *
clc_strnrev(char *s, size_t len)
{
clc_assert_not_null(clc_strnrev, s);
if(len) {
char *beg =3D s;
char *end =3D s + len - 1;
while (beg < end) {
char tmp =3D *end;
*end-- =3D *beg;
*beg++ =3D tmp;
}
}
return s;
}
--=20
Hallvard
|