]> diplodocus.org Git - nmh/blob - sbr/trimcpy.c
forwsbr.c: Move interface declaration to own forwsbr.h.
[nmh] / sbr / trimcpy.c
1 /* trimcpy.c -- strip leading and trailing whitespace,
2 * -- replace internal whitespace with spaces,
3 * -- then return a copy.
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13
14 char *
15 trimcpy (char *cp)
16 {
17 char *sp;
18
19 /* skip over leading whitespace */
20 while (isspace((unsigned char) *cp))
21 cp++;
22
23 /* start at the end and zap trailing whitespace */
24 for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) {
25 if (isspace((unsigned char) *sp))
26 *sp = '\0';
27 else
28 break;
29 }
30
31 /* replace remaining whitespace with spaces */
32 for (sp = cp; *sp; sp++) {
33 if (isspace((unsigned char) *sp))
34 *sp = ' ';
35 }
36
37 /* now return a copy */
38 return mh_xstrdup(cp);
39 }
40
41
42 /*
43 * cpytrim() -- return a copy of the argument with:
44 * -- stripped leading and trailing whitespace, and
45 * -- internal whitespace replaced with spaces.
46 *
47 * This code is Copyright (c) 2013, by the authors of nmh. See the
48 * COPYRIGHT file in the root directory of the nmh distribution for
49 * complete copyright information.
50 */
51 char *
52 cpytrim (const char *sp)
53 {
54 char *dp;
55 char *cp;
56
57 /* skip over leading whitespace */
58 while (isspace ((unsigned char) *sp)) ++sp;
59
60 dp = mh_xstrdup(sp);
61
62 /* start at the end and zap trailing whitespace */
63 for (cp = dp + strlen (dp) - 1;
64 cp >= dp && isspace ((unsigned char) *cp);
65 *cp-- = '\0') continue;
66
67 /* replace remaining whitespace with spaces */
68 for (cp = dp; *cp; ++cp) {
69 if (isspace ((unsigned char) *cp)) *cp = ' ';
70 }
71
72 return dp;
73 }
74
75
76 /*
77 * rtrim() -- modify the argument to:
78 * -- strip trailing whitespace
79 *
80 * This code is Copyright (c) 2014, by the authors of nmh. See the
81 * COPYRIGHT file in the root directory of the nmh distribution for
82 * complete copyright information.
83 */
84 char *
85 rtrim (char *sp)
86 {
87 char *cp;
88
89 /* start at the end and zap trailing whitespace */
90 for (cp = sp + strlen (sp) - 1;
91 cp >= sp && isspace ((unsigned char) *cp);
92 --cp) { continue; }
93 *++cp = '\0';
94
95 return sp;
96 }