1/* Locale dependent, case and normalization insensitive comparison of Unicode
2   strings.
3   Copyright (C) 2009-2010 Free Software Foundation, Inc.
4   Written by Bruno Haible <bruno@clisp.org>, 2009.
5
6   This program is free software: you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 3 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19int
20FUNC (const UNIT *s1, size_t n1, const UNIT *s2, size_t n2,
21      const char *iso639_language, uninorm_t nf, int *resultp)
22{
23  char buf1[2048];
24  char buf2[2048];
25  char *transformed1;
26  size_t transformed1_length;
27  char *transformed2;
28  size_t transformed2_length;
29  int cmp;
30
31  /* Normalize and transform S1.  */
32  transformed1_length = sizeof (buf1);
33  transformed1 =
34    U_CASEXFRM (s1, n1, iso639_language, nf, buf1, &transformed1_length);
35  if (transformed1 == NULL)
36    /* errno is set here.  */
37    return -1;
38
39  /* Normalize and transform S2.  */
40  transformed2_length = sizeof (buf2);
41  transformed2 =
42    U_CASEXFRM (s2, n2, iso639_language, nf, buf2, &transformed2_length);
43  if (transformed2 == NULL)
44    {
45      if (transformed1 != buf1)
46        {
47          int saved_errno = errno;
48          free (transformed1);
49          errno = saved_errno;
50        }
51      return -1;
52    }
53
54  /* Compare the transformed strings.  */
55  cmp = memcmp2 (transformed1, transformed1_length,
56                 transformed2, transformed2_length);
57  if (cmp < 0)
58    cmp = -1;
59  else if (cmp > 0)
60    cmp = 1;
61
62  if (transformed2 != buf2)
63    free (transformed2);
64  if (transformed1 != buf1)
65    free (transformed1);
66  *resultp = cmp;
67  return 0;
68}
69