New module 'uninorm/u8-normcmp'.
[gnulib.git] / lib / uninorm / u-normcmp.h
1 /* Normalization insensitive comparison of UTF-8 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 int
19 FUNC (const UNIT *s1, size_t n1, const UNIT *s2, size_t n2,
20       uninorm_t nf, int *result)
21 {
22   UNIT *norms1;
23   size_t norms1_length;
24   UNIT *norms2;
25   size_t norms2_length;
26   int cmp;
27
28   /* Normalize S1.  */
29   norms1 = U_NORMALIZE (nf, s1, n1, NULL, &norms1_length);
30   if (norms1 == NULL)
31     return errno;
32
33   /* Normalize S2.  */
34   norms2 = U_NORMALIZE (nf, s2, n2, NULL, &norms2_length);
35   if (norms2 == NULL)
36     {
37       int saved_errno = errno;
38       free (norms1);
39       return saved_errno;
40     }
41
42   /* Compare the normalized strings.  */
43   cmp = U_CMP (norms1, norms2, MIN (norms1_length, norms2_length));
44   if (cmp == 0)
45     {
46       if (norms1_length < norms2_length)
47         cmp = -1;
48       else if (norms1_length > norms2_length)
49         cmp = 1;
50     }
51   else if (cmp > 0)
52     cmp = 1;
53   else if (cmp < 0)
54     cmp = -1;
55
56   free (norms2);
57   free (norms1);
58   *result = cmp;
59   return 0;
60 }