[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
new module proposal: strip
From: |
Davide Angelocola |
Subject: |
new module proposal: strip |
Date: |
Thu, 31 Aug 2006 20:26:12 +0200 |
User-agent: |
KMail/1.9.4 |
this module defines three functions:
chomp() strip leading whitespaces
chug() strip trailing whitespaces
strip() strip trailing and leading whitespaces
Here is the code:
/* Strip mode. */
#define STRIP_TRAILING 0
#define STRIP_LEADING 1
#define STRIP_BOTH 2
#define strip(s) strip2((s), STRIP_BOTH)
#define chug(s) strip2((s), STRIP_TRAILING)
#define chomp(s) strip2((s), STRIP_LEADING)
char *
strip2(char *s, int how)
{
char *p;
/* Strip leading whitespaces. */
if (how != STRIP_TRAILING) {
for (p = s; *p && *p == ' '; p++)
;
memmove(s, p, strlen(p) + 1);
}
/* Strip trailing whitespaces. */
if (how != STRIP_LEADING) {
for (p = s + strlen(s) - 1; p >= s && *p == ' '; p--)
*p = '\0';
}
return s;
}
and a simple test program:
int
main(void)
{
char buf[] = " a string ";
printf("strip '%s'\n", strip(strdup(buf)));
printf("chug '%s'\n", chug(strdup(buf)));
printf("strip '%s'\n", chomp(strdup(buf)));
return 0;
}
Best regards,
-- Davide Angelocola (dfa)
--
-- Davide Angelocola
- new module proposal: strip,
Davide Angelocola <=