annotate lib/wcscoll-impl.h @ 17464:290d581e2e24

autoupdate
author Karl Berry <karl@freefriends.org>
date Sat, 10 Aug 2013 07:10:55 -0700
parents e542fd46ad6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14293
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Compare two wide strings using the current locale.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 2011-2013 Free Software Foundation, Inc.
14293
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2011.
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 int
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 wcscoll (const wchar_t *s1, const wchar_t *s2)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 {
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 char mbbuf1[1024];
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 char mbbuf2[1024];
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 char *mbs1;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 char *mbs2;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 {
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 int saved_errno = errno;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 /* Convert s1 to a multibyte string, trying to avoid malloc(). */
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 {
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 size_t ret;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 ret = wcstombs (mbbuf1, s1, sizeof (mbbuf1));
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 if (ret == (size_t)-1)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 goto failed1;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 if (ret < sizeof (mbbuf1))
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 mbs1 = mbbuf1;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 else
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 {
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 size_t need = wcstombs (NULL, s1, 0);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 if (need == (size_t)-1)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 goto failed1;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 mbs1 = (char *) malloc (need + 1);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 if (mbs1 == NULL)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 goto out_of_memory1;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 ret = wcstombs (mbs1, s1, need + 1);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 if (ret != need)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 abort ();
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 }
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 }
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 /* Convert s2 to a multibyte string, trying to avoid malloc(). */
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 {
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 size_t ret;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 ret = wcstombs (mbbuf2, s2, sizeof (mbbuf2));
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 if (ret == (size_t)-1)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 goto failed2;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 if (ret < sizeof (mbbuf2))
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 mbs2 = mbbuf2;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 else
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 {
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 size_t need = wcstombs (NULL, s2, 0);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 if (need == (size_t)-1)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 goto failed2;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 mbs2 = (char *) malloc (need + 1);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 if (mbs2 == NULL)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 goto out_of_memory2;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 ret = wcstombs (mbs2, s2, need + 1);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 if (ret != need)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 abort ();
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 }
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 }
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 /* No error so far. */
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 errno = saved_errno;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 }
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 /* Compare the two multibyte strings. */
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 {
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 int result = strcoll (mbs1, mbs2);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 if (mbs1 != mbbuf1)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 {
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 int saved_errno = errno;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 free (mbs1);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 errno = saved_errno;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 }
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 if (mbs2 != mbbuf2)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 {
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 int saved_errno = errno;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 free (mbs2);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 errno = saved_errno;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 }
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 return result;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 }
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 out_of_memory2:
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 if (mbs1 != mbbuf1)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 free (mbs1);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 out_of_memory1:
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 errno = ENOMEM;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 return 0;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 failed2:
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 if (mbs1 != mbbuf1)
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 free (mbs1);
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 failed1:
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 errno = EILSEQ;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 return 0;
13e6946b4e9a New module 'wcscoll'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 }