DAOK - generalized trim f(x)'s
Code:
/*
// General trim function(s)
// generalize the functions to remove white space
// make the VALUE macro t use is_ctrl and is_space
// for spaces only use is_space
*/
#define _IS_SP 1 /* is space */
#define _IS_CTL 32 /* Control */
extern char _Cdecl _ctype[]; /* Character type array */
/* is_ctrl() macro is_space() macro */
#define VALUE(c) (_ctype[(c) + 1] & _IS_CTL)|(_ctype[(c) + 1] & _IS_SP)
char *ltrim(char *s)
{
int set=0;
char *buf;
char *keep;
if(*s==0x00) return s; /* exit early */
buf=s,keep=s;
while ( VALUE(*buf) ) buf++,set++;
if(set){ /* make shortened s start at a new beginning */
while (*buf!=0x00)*keep++=*buf++;
*keep=0x00;
}
return s;
}
char *rtrim(char *s)
{
char *buf;
if(*s==0x00) return s; /* exit early */
buf=s;
buf+=strlen(buf)-1;
while(VALUE(*buf) )*buf--=0x00; /* shorten s */
return s;
}
char *trim(char *s)
{
if(*s==0x00) return s; /* exit early */
return ltrim( rtrim(s) ); /* trim = remove blanks both ends */
}