exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / wcsxfrm-impl.h
blobf2eb6d148f984e4ecf5a768527d887dc5d358db5
1 /* Transform wide string for comparison using the current locale.
2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2011.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 size_t
19 wcsxfrm (wchar_t *s1, const wchar_t *s2, size_t n)
21 char mbbuf2[1024];
22 char *mbs2;
25 int saved_errno = errno;
26 size_t result;
28 /* Convert s2 to a multibyte string, trying to avoid malloc(). */
30 size_t ret;
32 ret = wcstombs (mbbuf2, s2, sizeof (mbbuf2));
33 if (ret == (size_t)-1)
34 goto failed;
35 if (ret < sizeof (mbbuf2))
36 mbs2 = mbbuf2;
37 else
39 size_t need = wcstombs (NULL, s2, 0);
40 if (need == (size_t)-1)
41 goto failed;
42 mbs2 = (char *) malloc (need + 1);
43 if (mbs2 == NULL)
44 goto out_of_memory;
45 ret = wcstombs (mbs2, s2, need + 1);
46 if (ret != need)
47 abort ();
51 /* Transform the multibyte string. */
52 errno = 0;
53 result = strxfrm ((char *)s1, mbs2, n);
54 if (errno != 0)
56 /* An error occurred. */
57 if (mbs2 != mbbuf2)
58 free (mbs2);
59 return 0;
62 if (result < n)
64 /* Convert the result by mapping char[] -> wchar_t[].
65 Since strcmp() compares the elements as 'unsigned char' values,
66 whereas wcscmp() compares the elements as 'wchar_t' values, we need
67 to map 1 'unsigned char' to 1 'wchar_t'. (We could also map 2
68 consecutive 'unsigned char' values to 1 'wchar_t' value, but this is
69 not needed. */
70 wchar_t *wcp = s1 + n;
71 char *cp = (char *)s1 + n;
73 while (wcp > s1)
74 *--wcp = (wchar_t) (unsigned char) *--cp;
77 if (mbs2 != mbbuf2)
78 free (mbs2);
80 /* No error. */
81 errno = saved_errno;
82 return result;
85 out_of_memory:
86 errno = ENOMEM;
87 return 0;
89 failed:
90 errno = EILSEQ;
91 return 0;