1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <ctype.h>
char *trim (char *str)
{
char *ibuf, *obuf;
if (str)
{
for (ibuf = obuf = str; *ibuf;)
{
while (*ibuf && (isspace (*ibuf)))
ibuf++;
if (*ibuf && (obuf != str))
*(obuf++) = ' ';
while (*ibuf && (!isspace (*ibuf)))
*(obuf++) = *(ibuf++);
}
*obuf = '\0';
}
return (str);
}
#ifdef TEST
#include <stdio.h>
#include <string.h>
#define NB(a) (sizeof (a)/sizeof*(a))
int main (void)
{
typedef struct
{
char const *sin;
char const *sout;
}
test_s;
/*
* ben la fonction trim, d'habitude, supprime les
* espaces a gauche et a droite d'une chaine passee
* en parametre!
*/
static test_s const a[] =
{
{NULL, NULL},
{"", ""},
{" a", "a"},
{"a ", "a"},
{" a", "a"},
{"a ", "a"},
{" a b", "a b"},
{" a b ", "a b"},
};
size_t i;
for (i = 0; i < NB (a); i++)
{
test_s const *p = a + i;
if (p->sin == NULL)
{
char *sout = trim (NULL);
if (sout != NULL)
{
printf ("ERR at test %u\n", i + 1);
break;
}
}
else
{
char sin[32];
strcpy (sin, p->sin);
{
char *sout = trim (sin);
if (strcmp (sout, p->sout) != 0)
{
printf ("ERR at test %u\n", i + 1);
break;
}
}
}
}
if (i == NB (a))
{
puts ("PASSED");
}
return 0;
}
#endif |
Partager