New module 'uninorm/u8-normxfrm'.
[gnulib.git] / lib / uninorm / u-normxfrm.h
1 /* Locale dependent transformation for comparison of Unicode strings.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5    This program is free software: you can redistribute it and/or modify it
6    under the terms of the GNU Lesser General Public License as published
7    by the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 char *
19 FUNC (const UNIT *s, size_t n, uninorm_t nf,
20       char *resultbuf, size_t *lengthp)
21 {
22   UNIT normsbuf[2048 / sizeof (UNIT)];
23   UNIT *norms;
24   size_t norms_length;
25   char convsbuf[2048];
26   char *convs;
27   size_t convs_length;
28   int ret;
29   char *result;
30
31   /* Normalize the Unicode string.  */
32   norms_length = sizeof (normsbuf) / sizeof (UNIT);
33   norms = U_NORMALIZE (nf, s, n, normsbuf, &norms_length);
34   if (norms == NULL)
35     /* errno is set here.  */
36     return NULL;
37
38   /* Convert it to locale encoding.  */
39   convs = convsbuf;
40   convs_length = sizeof (convsbuf) - 1;
41   ret = U_CONV_TO_ENCODING (locale_charset (),
42                             iconveh_error,
43                             norms, norms_length,
44                             NULL,
45                             &convs, &convs_length);
46   if (ret < 0)
47     {
48       if (norms != normsbuf)
49         {
50           int saved_errno = errno;
51           free (norms);
52           errno = saved_errno;
53         }
54       return NULL;
55     }
56
57   if (norms != normsbuf)
58     free (norms);
59
60   /* Ensure one more byte is available.  */
61   if (convs != convsbuf)
62     {
63       char *memory = (char *) realloc (convs, convs_length + 1);
64       if (memory == NULL)
65         {
66           free (convs);
67           errno = ENOMEM;
68           return NULL;
69         }
70       convs = memory;
71     }
72
73   /* Apply locale dependent transformations for comparison.  */
74   result = memxfrm (convs, convs_length, resultbuf, lengthp);
75   if (result == NULL)
76     {
77       if (convs != convsbuf)
78         {
79           int saved_errno = errno;
80           free (convs);
81           errno = saved_errno;
82         }
83       return NULL;
84     }
85
86   if (convs != convsbuf)
87     free (convs);
88   return result;
89 }