1 /* Removes leading and/or trailing whitespaces
2 Copyright (C) 2006-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Davide Angelocola <davide.angelocola@gmail.com> */
29 #if GNULIB_MCEL_PREFER
33 # include "mbuiterf.h"
38 trim2 (const char *s
, int how
)
40 const char *start
= s
;
45 #if GNULIB_MCEL_PREFER
46 /* Skip leading whitespace. */
47 if (how
!= TRIM_TRAILING
)
48 for (mcel_t g
; *start
; start
+= g
.len
)
50 g
= mcel_scanz (start
);
51 if (!c32isspace (g
.ch
))
55 /* Find start of any trailing whitespace. */
56 if (how
!= TRIM_LEADING
)
57 for (const char *p
= end
= start
; *p
; )
59 mcel_t g
= mcel_scanz (p
);
61 if (!c32isspace (g
.ch
))
68 /* Skip leading whitespace. */
69 if (how
!= TRIM_TRAILING
)
70 while (mbuif_avail (state
, start
))
72 mbchar_t cur
= mbuif_next (state
, start
);
73 if (!mb_isspace (cur
))
75 start
+= mb_len (cur
);
78 /* Find start of any trailing whitespace. */
79 if (how
!= TRIM_LEADING
)
80 for (const char *p
= end
= start
; mbuif_avail (state
, p
); )
82 mbchar_t cur
= mbuif_next (state
, p
);
84 if (!mb_isspace (cur
))
91 /* Skip leading whitespace. */
92 if (how
!= TRIM_TRAILING
)
93 while (isspace ((unsigned char) *start
))
96 /* Find start of any trailing whitespace. */
97 if (how
!= TRIM_LEADING
)
98 for (const char *p
= end
= start
; *p
; )
99 if (!isspace ((unsigned char) *p
++))
103 /* Create trimmed copy. */
104 size_t dlen
= how
== TRIM_LEADING
? strlen (start
) : end
- start
;
105 char *d
= malloc (dlen
+ 1);
108 char *d_end
= mempcpy (d
, start
, dlen
);