]> diplodocus.org Git - nmh/blob - sbr/trimcpy.c
sendsbr.c: Move interface to own file.
[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 "trimcpy.h"
12 #include "h/utils.h"
13
14
15 char *
16 trimcpy (char *cp)
17 {
18 char *sp;
19
20 /* skip over leading whitespace */
21 while (isspace((unsigned char) *cp))
22 cp++;
23
24 /* start at the end and zap trailing whitespace */
25 for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) {
26 if (isspace((unsigned char) *sp))
27 *sp = '\0';
28 else
29 break;
30 }
31
32 /* replace remaining whitespace with spaces */
33 for (sp = cp; *sp; sp++) {
34 if (isspace((unsigned char) *sp))
35 *sp = ' ';
36 }
37
38 /* now return a copy */
39 return mh_xstrdup(cp);
40 }
41
42
43 /*
44 * cpytrim() -- return a copy of the argument with:
45 * -- stripped leading and trailing whitespace, and
46 * -- internal whitespace replaced with spaces.
47 *
48 * This code is Copyright (c) 2013, by the authors of nmh. See the
49 * COPYRIGHT file in the root directory of the nmh distribution for
50 * complete copyright information.
51 */
52 char *
53 cpytrim (const char *sp)
54 {
55 char *dp;
56 char *cp;
57
58 /* skip over leading whitespace */
59 while (isspace ((unsigned char) *sp)) ++sp;
60
61 dp = mh_xstrdup(sp);
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 {
88 char *cp;
89
90 /* start at the end and zap trailing whitespace */
91 for (cp = sp + strlen (sp) - 1;
92 cp >= sp && isspace ((unsigned char) *cp);
93 --cp) { continue; }
94 *++cp = '\0';
95
96 return sp;
97 }