|
-
Aug 1st, 2002, 09:49 AM
#1
Thread Starter
Frenzied Member
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 */
}
-
Aug 2nd, 2002, 07:45 PM
#2
Ya ya Baby!!!Me is Back
thx
I had just take an eye on your code and wow, it's very complex. Can you tell me why you use HEXadecimal number? 0x00 ?
-
Aug 2nd, 2002, 07:58 PM
#3
Ya ya Baby!!!Me is Back
And I do not understand that :
Code:
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)
-
Aug 2nd, 2002, 09:25 PM
#4
Thread Starter
Frenzied Member
0x00 marks the end of a C string. You know that you have reached string end of a string when you hit a 0x00 character.
Code:
extern char _Cdecl _ctype[]; /* Character type array */
Compilers create stuff in the background that you may not know about. This is one of those things - an array of char that is used by ctype operations - operations that work with individual characters.
extern means the variable is defined somewhere else. But we use it here, too.
Code:
#define VALUE(c) (_ctype[(c) + 1] & _IS_CTL)|(_ctype[(c) + 1] & _IS_SP)
This is just a macro. It tells you if the current character is a control character or a space. Control characters and spaces together make up the "white" space in text.
You could change it to find only spaces like this:
Code:
#define VALUE(c) (_ctype[(c) + 1] & _IS_SP)
That's why macros are nice - they let you make a change in one place, and it spreads throughout all of the code.
Do you see now what the code does? It looks complex but it is very simple, really.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|