/* Pieter Suurmond, 8 mei 2012. */ #include /* Removes leading and trailing whitespace as well as multiple spaces inside. Also converts tabs, newlines, etc. to spaces. Suitable for (destructive) in-place conversion because strings never grow. Returns the number of characters written to destination dst excluding the terminating null character. Never returns negative values. */ static int trim_whitespace(const char *src, char* dst) { const char space = ' '; char* start = dst; unsigned char c; /* When c is a signed char (when you ommit 'unsigned'), this routine goes terribly wrong for ISO-LATIN encoded characters: they are then less than zero and will be skipped! */ do { /* Skip leading whitespace. */ c = *src++; } while (c && (c <= space)); while (c) { if (c < space) c = space; if ((c > space) || (*src > space)) /* Not a space or next not space or 0. */ *dst++ = c; c = *src++; } *dst = c; /* Terminate C-string (c=0). */ return (int)(dst - start); }