1 /* Categories of Unicode characters.
2 Copyright (C) 2002, 2006-2007, 2011-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2002.
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 #include <config.h>
27
28 /* Specification. */
29 #include "unictype.h"
30
31 static const char u_category_name[30][3] =
32 {
33 "Lu", "Ll", "Lt", "Lm", "Lo", "Mn", "Mc", "Me", "Nd", "Nl",
34 "No", "Pc", "Pd", "Ps", "Pe", "Pi", "Pf", "Po", "Sm", "Sc",
35 "Sk", "So", "Zs", "Zl", "Zp", "Cc", "Cf", "Cs", "Co", "Cn"
36 };
37
38 const char *
39 uc_general_category_name (uc_general_category_t category)
/* ![[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)
*/
40 {
41 uint32_t bitmask = category.bitmask;
42 /* bitmask should consist of a single bit. */
43 if (bitmask != 0)
44 {
45 if ((bitmask & (bitmask - 1)) == 0)
46 {
47 int bit;
48 /* Take log2 using a variant of Robert Harley's method.
49 Found by Bruno Haible 1996. */
50 uint32_t n = bitmask;
51 static const char ord2_tab[64] =
52 {
53 -1, 0, 1, 12, 2, 6, -1, 13, 3, -1, 7, -1, -1, -1, -1, 14,
54 10, 4, -1, -1, 8, -1, -1, 25, -1, -1, -1, -1, -1, 21, 27, 15,
55 31, 11, 5, -1, -1, -1, -1, -1, 9, -1, -1, 24, -1, -1, 20, 26,
56 30, -1, -1, -1, -1, 23, -1, 19, 29, -1, 22, 18, 28, 17, 16, -1
57 };
58 n += n << 4;
59 n += n << 6;
60 n = (n << 16) - n;
61 bit = ord2_tab[n >> 26];
62
63 if (bit < sizeof (u_category_name) / sizeof (u_category_name[0]))
64 return u_category_name[bit];
65 }
66 else
67 {
68 if (bitmask == UC_CATEGORY_MASK_L)
69 return "L";
70 if (bitmask == UC_CATEGORY_MASK_LC)
71 return "LC";
72 if (bitmask == UC_CATEGORY_MASK_M)
73 return "M";
74 if (bitmask == UC_CATEGORY_MASK_N)
75 return "N";
76 if (bitmask == UC_CATEGORY_MASK_P)
77 return "P";
78 if (bitmask == UC_CATEGORY_MASK_S)
79 return "S";
80 if (bitmask == UC_CATEGORY_MASK_Z)
81 return "Z";
82 if (bitmask == UC_CATEGORY_MASK_C)
83 return "C";
84 }
85 }
86 return NULL;
87 }