1/* Case and normalization insensitive comparison of Unicode strings.
2   Copyright (C) 2009-2010 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
18int
19FUNC (const SRC_UNIT *s1, size_t n1, const SRC_UNIT *s2, size_t n2,
20      const char *iso639_language, uninorm_t nf, int *resultp)
21{
22  UNIT buf1[2048 / sizeof (UNIT)];
23  UNIT buf2[2048 / sizeof (UNIT)];
24  UNIT *norms1;
25  size_t norms1_length;
26  UNIT *norms2;
27  size_t norms2_length;
28  int cmp;
29
30  /* Optimization: There is no need to do canonical composition of each string.
31     Decomposition is enough.  */
32  if (nf != NULL)
33    nf = uninorm_decomposing_form (nf);
34
35  /* Case-fold and normalize S1.  */
36  norms1_length = sizeof (buf1) / sizeof (UNIT);
37  norms1 = U_CASEFOLD (s1, n1, iso639_language, nf, buf1, &norms1_length);
38  if (norms1 == NULL)
39    /* errno is set here.  */
40    return -1;
41
42  /* Case-fold and normalize S2.  */
43  norms2_length = sizeof (buf2) / sizeof (UNIT);
44  norms2 = U_CASEFOLD (s2, n2, iso639_language, nf, buf2, &norms2_length);
45  if (norms2 == NULL)
46    {
47      if (norms1 != buf1)
48        {
49          int saved_errno = errno;
50          free (norms1);
51          errno = saved_errno;
52        }
53      return -1;
54    }
55
56  /* Compare the normalized strings.  */
57  cmp = U_CMP2 (norms1, norms1_length, norms2, norms2_length);
58  if (cmp > 0)
59    cmp = 1;
60  else if (cmp < 0)
61    cmp = -1;
62
63  if (norms2 != buf2)
64    free (norms2);
65  if (norms1 != buf1)
66    free (norms1);
67  *resultp = cmp;
68  return 0;
69}
70