1 /* Transform wide string for comparison using the current locale.
2 Copyright (C) 2011-2019 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2011.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 wcsxfrm (wchar_t *s1
, const wchar_t *s2
, size_t n
)
25 int saved_errno
= errno
;
28 /* Convert s2 to a multibyte string, trying to avoid malloc(). */
32 ret
= wcstombs (mbbuf2
, s2
, sizeof (mbbuf2
));
33 if (ret
== (size_t)-1)
35 if (ret
< sizeof (mbbuf2
))
39 size_t need
= wcstombs (NULL
, s2
, 0);
40 if (need
== (size_t)-1)
42 mbs2
= (char *) malloc (need
+ 1);
45 ret
= wcstombs (mbs2
, s2
, need
+ 1);
51 /* Transform the multibyte string. */
53 result
= strxfrm ((char *)s1
, mbs2
, n
);
56 /* An error occurred. */
68 /* Convert the result by mapping char[] -> wchar_t[].
69 Since strcmp() compares the elements as 'unsigned char' values,
70 whereas wcscmp() compares the elements as 'wchar_t' values, we need
71 to map 1 'unsigned char' to 1 'wchar_t'. (We could also map 2
72 consecutive 'unsigned char' values to 1 'wchar_t' value, but this is
74 wchar_t *wcp
= s1
+ n
;
75 char *cp
= (char *)s1
+ n
;
78 *--wcp
= (wchar_t) (unsigned char) *--cp
;