File: trimcpy.c

package info (click to toggle)
nmh 1.6-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,204 kB
  • ctags: 3,851
  • sloc: ansic: 48,922; sh: 16,422; makefile: 559; perl: 509; lex: 402; awk: 74
file content (74 lines) | stat: -rw-r--r-- 1,752 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

/*
 * trimcpy.c -- strip leading and trailing whitespace,
 *           -- replace internal whitespace with spaces,
 *           -- then return a copy.
 *
 * This code is Copyright (c) 2002, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <h/utils.h>


char *
trimcpy (char *cp)
{
    char *sp;

    /* skip over leading whitespace */
    while (isspace((unsigned char) *cp))
	cp++;

    /* start at the end and zap trailing whitespace */
    for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) {
	if (isspace((unsigned char) *sp))
	    *sp = '\0';
	else
	    break;
    }

    /* replace remaining whitespace with spaces */
    for (sp = cp; *sp; sp++) {
	if (isspace((unsigned char) *sp))
	    *sp = ' ';
    }

    /* now return a copy */
    return getcpy(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 = add (sp, NULL);

    /* 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;
}