You know I really had to look hard at this to understand it. Horrible
variables names almost purposely made "no standard" as far as C around
the world goes for such a function. Unnecessary length check.
Unnecessary size_t. Multiple assignments on one line making perusal
with a debugger almost impossible.
Yep - CBF is a living master-class in anti-style.
Much nicer IMO:
#include <string.h>
char * my_strrev(char *s){
char t;
char *e = s+strlen(s)-1;
while(e>s){
t = *s;
*s++ = *e;
*e-- = t;
}
}
Much nicer! Of course the "UB" that got the regs in a state will never
be a problem in the real world.
As efficiency was a concern for the OP, it might be worth dealing
specially with the favorable case when both the string length and its
starting location match up with the machine's natural alignment, so that
one can use only aligned accesses.
For example, on a 32-bit machine this might be more efficient (though of
course only profiling will tell for sure):
#define SWAP32(x) ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
char *my_strrev(char *s)
{
size_t len = strlen(s);
if(((unsigned long) s & len & 0x11)==0) {
/* s aligned on 4-byte boundary, and length a multiple of 4 */
unsigned *start = (unsigned *) s;
unsigned *end = start + (len>>2) - 1;
unsigned temp;
while(end > start) {
temp = SWAP32(*start);
*start++ = SWAP32(*end);
*end-- = temp;
}
}
else {
/* use generic string reverser */
}
return s;
}