X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/2e768e3cc0ce768373d3320eca4cdd08b8cf646a..94187a80bd60baab4b9c4b949ad820d730578123:/sbr/trimcpy.c diff --git a/sbr/trimcpy.c b/sbr/trimcpy.c index 3a2fbd76..625c26c0 100644 --- a/sbr/trimcpy.c +++ b/sbr/trimcpy.c @@ -1,6 +1,4 @@ - -/* - * trimcpy.c -- strip leading and trailing whitespace, +/* trimcpy.c -- strip leading and trailing whitespace, * -- replace internal whitespace with spaces, * -- then return a copy. * @@ -10,6 +8,7 @@ */ #include +#include char * @@ -36,5 +35,60 @@ trimcpy (char *cp) } /* now return a copy */ - return getcpy(cp); + return mh_xstrdup(cp); +} + + +/* + * cpytrim() -- return a copy of the argument with: + * -- stripped leading and trailing whitespace, and + * -- internal whitespace replaced with spaces. + * + * This code is Copyright (c) 2013, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. + */ +char * +cpytrim (const char *sp) { + char *dp; + char *cp; + + /* skip over leading whitespace */ + while (isspace ((unsigned char) *sp)) ++sp; + + dp = mh_xstrdup(sp); + + /* start at the end and zap trailing whitespace */ + for (cp = dp + strlen (dp) - 1; + cp >= dp && isspace ((unsigned char) *cp); + *cp-- = '\0') continue; + + /* replace remaining whitespace with spaces */ + for (cp = dp; *cp; ++cp) { + if (isspace ((unsigned char) *cp)) *cp = ' '; + } + + return dp; +} + + +/* + * rtrim() -- modify the argument to: + * -- strip trailing whitespace + * + * This code is Copyright (c) 2014, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. + */ +char * +rtrim (char *sp) { + char *cp; + + /* start at the end and zap trailing whitespace */ + for (cp = sp + strlen (sp) - 1; + cp >= sp && isspace ((unsigned char) *cp); + --cp) { continue; } + *++cp = '\0'; + + return sp; }