Remove trailing spaces.
[gnulib.git] / lib / trim.c
1 /* Removes leading and/or trailing whitespaces
2    Copyright (C) 2006-2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Davide Angelocola <davide.angelocola@gmail.com> */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "trim.h"
23
24 #include <ctype.h>
25 #include <string.h>
26
27 #if HAVE_MBRTOWC
28 # include <stddef.h>
29 # include <stdlib.h>
30 # include "mbchar.h"
31 # include "mbiter.h"
32 #endif
33
34 #include "xalloc.h"
35
36 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
37 #ifdef lint
38 # define IF_LINT(Code) Code
39 #else
40 # define IF_LINT(Code) /* empty */
41 #endif
42
43 char *
44 trim2(const char *s, int how)
45 {
46   char *d;
47
48   d = strdup(s);
49
50   if (!d)
51     xalloc_die();
52
53 #if HAVE_MBRTOWC
54   if (MB_CUR_MAX > 1)
55     {
56       mbi_iterator_t i;
57
58       /* Trim leading whitespaces. */
59       if (how != TRIM_TRAILING)
60         {
61           mbi_init (i, d, strlen (d));
62
63           for (; mbi_avail (i) && mb_isspace (mbi_cur (i)); mbi_advance (i))
64             ;
65
66           memmove (d, mbi_cur_ptr (i), strlen (mbi_cur_ptr (i)) + 1);
67         }
68
69       /* Trim trailing whitespaces. */
70       if (how != TRIM_LEADING)
71         {
72           int state = 0;
73           char *r IF_LINT (= NULL); /* used only while state = 2 */
74
75           mbi_init (i, d, strlen (d));
76
77           for (; mbi_avail (i); mbi_advance (i))
78             {
79               if (state == 0 && mb_isspace (mbi_cur (i)))
80                 {
81                   state = 0;
82                   continue;
83                 }
84
85               if (state == 0 && !mb_isspace (mbi_cur (i)))
86                 {
87                   state = 1;
88                   continue;
89                 }
90
91               if (state == 1 && !mb_isspace (mbi_cur (i)))
92                 {
93                   state = 1;
94                   continue;
95                 }
96
97               if (state == 1 && mb_isspace (mbi_cur (i)))
98                 {
99                   state = 2;
100                   r = (char *) mbi_cur_ptr (i);
101                 }
102               else if (state == 2 && mb_isspace (mbi_cur (i)))
103                 {
104                   state = 2;
105                 }
106               else
107                 {
108                   state = 1;
109                 }
110             }
111
112           if (state == 2)
113             *r = '\0';
114         }
115     }
116   else
117 #endif /* HAVE_MBRTOWC */
118     {
119       char *p;
120
121       /* Trim leading whitespaces. */
122       if (how != TRIM_TRAILING) {
123         for (p = d; *p && isspace ((unsigned char) *p); p++)
124           ;
125
126         memmove (d, p, strlen (p) + 1);
127       }
128
129       /* Trim trailing whitespaces. */
130       if (how != TRIM_LEADING) {
131         for (p = d + strlen (d) - 1; p >= d && isspace ((unsigned char) *p); p--)
132           *p = '\0';
133       }
134     }
135
136   return d;
137 }
138