1 /* Removes leading and/or trailing whitespaces
2 Copyright (C) 2006 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 2, or (at your option)
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; see the file COPYING.
16 If not, write to the Free Software Foundation,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Davide Angelocola <davide.angelocola@gmail.com> */
29 # include "mbuiter.h" /* FIXME: for MB_CUR_MAX */
38 trim2(const char *s, int how)
52 /* Trim leading whitespaces. */
53 if (how != TRIM_TRAILING)
55 mbi_init (i, d, strlen (d));
57 for (; mbi_avail (i) && mb_isspace (mbi_cur (i)); mbi_advance (i))
60 memmove (d, mbi_cur_ptr (i), strlen (mbi_cur_ptr (i)) + 1);
63 /* Trim trailing whitespaces. */
64 if (how != TRIM_LEADING)
69 mbi_init (i, d, strlen (d));
71 for (; mbi_avail (i); mbi_advance (i))
73 if (state == 0 && mb_isspace (mbi_cur (i)))
79 if (state == 0 && !mb_isspace (mbi_cur (i)))
85 if (state == 1 && !mb_isspace (mbi_cur (i)))
91 if (state == 1 && mb_isspace (mbi_cur (i)))
94 r = (char *) mbi_cur_ptr (i);
96 else if (state == 2 && mb_isspace (mbi_cur (i)))
111 #endif /* HAVE_MBRTOWC */
115 /* Trim leading whitespaces. */
116 if (how != TRIM_TRAILING) {
117 for (p = d; *p && isspace (*p); p++)
120 memmove (d, p, strlen (p) + 1);
123 /* Trim trailing whitespaces. */
124 if (how != TRIM_LEADING) {
125 for (p = d + strlen (d) - 1; p >= d && isspace (*p); p--)