Fix detection of overflow: don't assume that snprintf is C99 compliant.
[gnulib.git] / lib / trim.c
1 /* Removes leading and/or trailing whitespaces
2    Copyright (C) 2006-2007 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
26 #if HAVE_MBRTOWC 
27 # include <stddef.h>
28 # include <stdlib.h>
29 # include "mbchar.h"
30 # include "mbiter.h"
31 #endif
32
33 #include "xalloc.h"
34
35 char *
36 trim2(const char *s, int how)
37 {
38   char *d;
39     
40   d = strdup(s);
41
42   if (!d)
43     xalloc_die();
44   
45 #if HAVE_MBRTOWC
46   if (MB_CUR_MAX > 1)
47     {
48       mbi_iterator_t i;
49       
50       /* Trim leading whitespaces. */
51       if (how != TRIM_TRAILING) 
52         {
53           mbi_init (i, d, strlen (d));
54       
55           for (; mbi_avail (i) && mb_isspace (mbi_cur (i)); mbi_advance (i))
56             ;
57           
58           memmove (d, mbi_cur_ptr (i), strlen (mbi_cur_ptr (i)) + 1);
59         }
60   
61       /* Trim trailing whitespaces. */
62       if (how != TRIM_LEADING) 
63         {
64           int state = 0;
65           char *r; /* used only while state = 2 */
66           
67           mbi_init (i, d, strlen (d));
68
69           for (; mbi_avail (i); mbi_advance (i)) 
70             {
71               if (state == 0 && mb_isspace (mbi_cur (i))) 
72                 {
73                   state = 0;
74                   continue;
75                 }
76               
77               if (state == 0 && !mb_isspace (mbi_cur (i)))
78                 {
79                   state = 1;
80                   continue;
81                 }
82               
83               if (state == 1 && !mb_isspace (mbi_cur (i)))
84                 {
85                   state = 1;
86                   continue;
87                 }
88               
89               if (state == 1 && mb_isspace (mbi_cur (i))) 
90                 {
91                   state = 2;
92                   r = (char *) mbi_cur_ptr (i);
93                 }
94               else if (state == 2 && mb_isspace (mbi_cur (i))) 
95                 {
96                   state = 2;
97                 } 
98               else 
99                 {
100                   state = 1;
101                 }
102             }
103           
104           if (state == 2) 
105             *r = '\0';
106         }
107     }
108   else
109 #endif /* HAVE_MBRTOWC */
110     {
111       char *p;
112       
113       /* Trim leading whitespaces. */
114       if (how != TRIM_TRAILING) {
115         for (p = d; *p && isspace (*p); p++)
116           ;                     
117
118         memmove (d, p, strlen (p) + 1);
119       }
120
121       /* Trim trailing whitespaces. */
122       if (how != TRIM_LEADING) {
123         for (p = d + strlen (d) - 1; p >= d && isspace (*p); p--)
124           *p = '\0';
125       }
126     }
127   
128   return d;
129 }
130