-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrim.c
44 lines (38 loc) · 902 Bytes
/
strim.c
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
#include <stddef.h>
#include <string.h>
#include "strim.h" // ISWHITE()
//+============================================================================
// strimt ... trim suffixing whitespace (string trim tail)
// strimh ... trim prefixing whitespace (string trim head)
// strim ... trim line from both ends (string trim head+tail)
//
//+====================================
char* strimh (char* s)
{
if (s && *s) {
char *src, *dst;
for (src = s; *src && ISWHITE(*src); src++) ;
for (dst = s; *src; *dst++ = *src++) ;
*dst = '\0';
}
return s;
}
//+====================================
char* strimt (char* s)
{
char* cp = NULL;
if (s && *s)
for (cp = s + strlen(s) - 1;
(cp >= s) && ISWHITE(*cp);
*cp-- = '\0') ;
return s;
}
//+====================================
char* strim (char* s)
{
if (s) {
(void)strimt(s);
(void)strimh(s);
}
return s;
}