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