trim whitespace, bullet proof version

J

James

[...]
However, on my Interix test platform, PTRDIFF_MAX is not defined, and
ptrdiff_t is signed. It's my understanding I can't use the same cast
trick with a signed type.

So what are my options for determining the maximum value representable
in a signed ptrdiff_t variable, when PTRDIFF_MAX is not defined?

Well, this crap actually compiles on Comeau:


#if ! defined (SIZE_MAX)
# define SIZE_MAX ((size_t)-1)
#endif


#if ! defined (PTRDIFF_MAX)
# if (SIZE_MAX == ULONG_MAX)
# define PTRDIFF_MAX LONG_MAX
# define PTRDIFF_MIN LONG_MIN
# elif (SIZE_MAX == UINT_MAX)
# define PTRDIFF_MAX INT_MAX
# define PTRDIFF_MIN INT_MIN
# elif (SIZE_MAX == USHRT_MAX)
# define PTRDIFF_MAX SHRT_MAX
# define PTRDIFF_MIN SHRT_MIN
# elif (SIZE_MAX == UCHAR_MAX)
# define PTRDIFF_MAX SCHAR_MAX
# define PTRDIFF_MIN SCHAR_MIN
# else
# error PTRDIFF_MAX cannot be defined!
# endif
#endif



It also compiles on VS 2010 express and EDG C99...

lol.
 
P

Peter Nilsson

John Kelly said:
trim whitespace, bullet proof version
<snip>

char *ltrim(char *s)
{
unsigned char *p = (unsigned char *) s;
unsigned char *q = (unsigned char *) s;
while (isspace(*q)) q++;
if (p != q) while ((*p++ = *q++) != 0) ;
return s;
}

char *rtrim(char *s)
{
unsigned char *p = (unsigned char *) s;
unsigned char *q = (unsigned char *) strchr(s, 0);
while (q != p && isspace(q[-1])) q--;
*q = 0;
return s;
}

char *trim(char *s)
{
return ltrim(rtrim(s));
}
 
M

Moonman

John Kelly said:
trim whitespace, bullet proof version
<snip>

char *ltrim(char *s)
{
unsigned char *p = (unsigned char *) s; unsigned char *q = (unsigned
char *) s; while (isspace(*q)) q++;
if (p != q) while ((*p++ = *q++) != 0) ; return s;
}

char *rtrim(char *s)
{
unsigned char *p = (unsigned char *) s; unsigned char *q = (unsigned
char *) strchr(s, 0); while (q != p && isspace(q[-1])) q--; *q = 0;
return s;
}

char *trim(char *s)
{
return ltrim(rtrim(s));
}

can even be optimised:
one should not apply this to constant or read only strings though :)

char * trim(char *s)
{
unsigned char *p = (unsigned char*) s;
unsigned char *q = (unsigned char*) s;

while(isspace(*q)) q++;
if(p != q)
while((*p++ = *q++) !=0);

q = p-1;
p = s;

while(p != q && isspace(q[-1])) q--;
q[0] = 0;

return s;
}

greetz
 
J

James

[...]
linux fork() is fast enough that many apps don't need threads. Someone
who needs threads can try writing trim_r().

You can do a low-level trim function that is read-only; e.g., returns offset
and length of trimmed string. Why exactly would you need a special variant
trim_r() for a multi-threaded environment?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top