maint: update copyright
[gnulib.git] / lib / unictype / categ_longname.c
1 /* Categories of Unicode characters.
2    Copyright (C) 2002, 2006-2007, 2011-2014 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2011.
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 #include <config.h>
19
20 /* Specification.  */
21 #include "unictype.h"
22
23 static const char u_category_long_name[30][22] =
24 {
25   "Uppercase Letter",
26   "Lowercase Letter",
27   "Titlecase Letter",
28   "Modifier Letter",
29   "Other Letter",
30   "Nonspacing Mark",
31   "Spacing Mark",
32   "Enclosing Mark",
33   "Decimal Number",
34   "Letter Number",
35   "Other Number",
36   "Connector Punctuation",
37   "Dash Punctuation",
38   "Open Punctuation",
39   "Close Punctuation",
40   "Initial Punctuation",
41   "Final Punctuation",
42   "Other Punctuation",
43   "Math Symbol",
44   "Currency Symbol",
45   "Modifier Symbol",
46   "Other Symbol",
47   "Space Separator",
48   "Line Separator",
49   "Paragraph Separator",
50   "Control",
51   "Format",
52   "Surrogate",
53   "Private Use",
54   "Unassigned"
55 };
56
57 const char *
58 uc_general_category_long_name (uc_general_category_t category)
59 {
60   uint32_t bitmask = category.bitmask;
61   /* bitmask should consist of a single bit.  */
62   if (bitmask != 0)
63     {
64       if ((bitmask & (bitmask - 1)) == 0)
65         {
66           int bit;
67           /* Take log2 using a variant of Robert Harley's method.
68              Found by Bruno Haible 1996.  */
69           uint32_t n = bitmask;
70           static const char ord2_tab[64] =
71             {
72               -1,  0,  1, 12,  2,  6, -1, 13,  3, -1,  7, -1, -1, -1, -1, 14,
73               10,  4, -1, -1,  8, -1, -1, 25, -1, -1, -1, -1, -1, 21, 27, 15,
74               31, 11,  5, -1, -1, -1, -1, -1,  9, -1, -1, 24, -1, -1, 20, 26,
75               30, -1, -1, -1, -1, 23, -1, 19, 29, -1, 22, 18, 28, 17, 16, -1
76             };
77           n += n << 4;
78           n += n << 6;
79           n = (n << 16) - n;
80           bit = ord2_tab[n >> 26];
81
82           if (bit < sizeof (u_category_long_name) / sizeof (u_category_long_name[0]))
83             return u_category_long_name[bit];
84         }
85       else
86         {
87           if (bitmask == UC_CATEGORY_MASK_L)
88             return "Letter";
89           if (bitmask == UC_CATEGORY_MASK_LC)
90             return "Cased Letter";
91           if (bitmask == UC_CATEGORY_MASK_M)
92             return "Mark";
93           if (bitmask == UC_CATEGORY_MASK_N)
94             return "Number";
95           if (bitmask == UC_CATEGORY_MASK_P)
96             return "Punctuation";
97           if (bitmask == UC_CATEGORY_MASK_S)
98             return "Symbol";
99           if (bitmask == UC_CATEGORY_MASK_Z)
100             return "Separator";
101           if (bitmask == UC_CATEGORY_MASK_C)
102             return "Other";
103         }
104     }
105   return NULL;
106 }