]> diplodocus.org Git - nmh/blob - sbr/trimcpy.c
mhbuildsbr.c: Flip logic, moving goto to then-block; no need for else.
[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 char *dp;
54 char *cp;
55
56 /* skip over leading whitespace */
57 while (isspace ((unsigned char) *sp)) ++sp;
58
59 dp = mh_xstrdup(sp);
60
61 /* start at the end and zap trailing whitespace */
62 for (cp = dp + strlen (dp) - 1;
63 cp >= dp && isspace ((unsigned char) *cp);
64 *cp-- = '\0') continue;
65
66 /* replace remaining whitespace with spaces */
67 for (cp = dp; *cp; ++cp) {
68 if (isspace ((unsigned char) *cp)) *cp = ' ';
69 }
70
71 return dp;
72 }
73
74
75 /*
76 * rtrim() -- modify the argument to:
77 * -- strip trailing whitespace
78 *
79 * This code is Copyright (c) 2014, by the authors of nmh. See the
80 * COPYRIGHT file in the root directory of the nmh distribution for
81 * complete copyright information.
82 */
83 char *
84 rtrim (char *sp) {
85 char *cp;
86
87 /* start at the end and zap trailing whitespace */
88 for (cp = sp + strlen (sp) - 1;
89 cp >= sp && isspace ((unsigned char) *cp);
90 --cp) { continue; }
91 *++cp = '\0';
92
93 return sp;
94 }