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