1 /* Removes leading and/or trailing whitespaces
2 Copyright (C) 2006-2008 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Davide Angelocola <davide.angelocola@gmail.com> */
37 trim2(const char *s, int how)
51 /* Trim leading whitespaces. */
52 if (how != TRIM_TRAILING)
54 mbi_init (i, d, strlen (d));
56 for (; mbi_avail (i) && mb_isspace (mbi_cur (i)); mbi_advance (i))
59 memmove (d, mbi_cur_ptr (i), strlen (mbi_cur_ptr (i)) + 1);
62 /* Trim trailing whitespaces. */
63 if (how != TRIM_LEADING)
66 char *r; /* used only while state = 2 */
68 mbi_init (i, d, strlen (d));
70 for (; mbi_avail (i); mbi_advance (i))
72 if (state == 0 && mb_isspace (mbi_cur (i)))
78 if (state == 0 && !mb_isspace (mbi_cur (i)))
84 if (state == 1 && !mb_isspace (mbi_cur (i)))
90 if (state == 1 && mb_isspace (mbi_cur (i)))
93 r = (char *) mbi_cur_ptr (i);
95 else if (state == 2 && mb_isspace (mbi_cur (i)))
110 #endif /* HAVE_MBRTOWC */
114 /* Trim leading whitespaces. */
115 if (how != TRIM_TRAILING) {
116 for (p = d; *p && isspace ((unsigned char) *p); p++)
119 memmove (d, p, strlen (p) + 1);
122 /* Trim trailing whitespaces. */
123 if (how != TRIM_LEADING) {
124 for (p = d + strlen (d) - 1; p >= d && isspace ((unsigned char) *p); p--)