1 /* Normalization insensitive comparison of Unicode strings.
2 Copyright (C) 2009-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5 This file is free software.
6 It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
7 You can redistribute it and/or modify it under either
8 - the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version, or
11 - the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option)
13 any later version, or
14 - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
15
16 This file is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License and the GNU General Public License
20 for more details.
21
22 You should have received a copy of the GNU Lesser General Public
23 License and of the GNU General Public License along with this
24 program. If not, see <https://www.gnu.org/licenses/>. */
25
26 int
27 FUNC (const UNIT *s1, size_t n1, const UNIT *s2, size_t n2,
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
28 uninorm_t nf, int *resultp)
29 {
30 UNIT buf1[2048 / sizeof (UNIT)];
31 UNIT buf2[2048 / sizeof (UNIT)];
32 UNIT *norms1;
33 size_t norms1_length;
34 UNIT *norms2;
35 size_t norms2_length;
36 int cmp;
37
38 /* Normalize S1. */
39 norms1_length = sizeof (buf1) / sizeof (UNIT);
40 norms1 = U_NORMALIZE (nf, s1, n1, buf1, &norms1_length);
41 if (norms1 == NULL)
42 /* errno is set here. */
43 return -1;
44
45 /* Normalize S2. */
46 norms2_length = sizeof (buf2) / sizeof (UNIT);
47 norms2 = U_NORMALIZE (nf, s2, n2, buf2, &norms2_length);
48 if (norms2 == NULL)
49 {
50 if (norms1 != buf1)
51 {
52 int saved_errno = errno;
53 free (norms1);
54 errno = saved_errno;
55 }
56 return -1;
57 }
58
59 /* Compare the normalized strings. */
60 cmp = U_CMP2 (norms1, norms1_length, norms2, norms2_length);
61 if (cmp > 0)
62 cmp = 1;
63 else if (cmp < 0)
64 cmp = -1;
65
66 if (norms2 != buf2)
67 free (norms2);
68 if (norms1 != buf1)
69 free (norms1);
70 *resultp = cmp;
71 return 0;
72 }