root/maint/gnulib/lib/unictype/categ_byname.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. uc_general_category_byname

   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 #include <stdlib.h>
  32 #include <string.h>
  33 
  34 /* Indices stored in the 'struct named_category' elements of the perfect hash
  35    table.  We don't use uc_general_category_t values or their addresses
  36    directly, because this would introduce load-time relocations.  */
  37 enum
  38 {
  39   UC_CATEGORY_INDEX_L,
  40   UC_CATEGORY_INDEX_LC,
  41   UC_CATEGORY_INDEX_Lu,
  42   UC_CATEGORY_INDEX_Ll,
  43   UC_CATEGORY_INDEX_Lt,
  44   UC_CATEGORY_INDEX_Lm,
  45   UC_CATEGORY_INDEX_Lo,
  46   UC_CATEGORY_INDEX_M,
  47   UC_CATEGORY_INDEX_Mn,
  48   UC_CATEGORY_INDEX_Mc,
  49   UC_CATEGORY_INDEX_Me,
  50   UC_CATEGORY_INDEX_N,
  51   UC_CATEGORY_INDEX_Nd,
  52   UC_CATEGORY_INDEX_Nl,
  53   UC_CATEGORY_INDEX_No,
  54   UC_CATEGORY_INDEX_P,
  55   UC_CATEGORY_INDEX_Pc,
  56   UC_CATEGORY_INDEX_Pd,
  57   UC_CATEGORY_INDEX_Ps,
  58   UC_CATEGORY_INDEX_Pe,
  59   UC_CATEGORY_INDEX_Pi,
  60   UC_CATEGORY_INDEX_Pf,
  61   UC_CATEGORY_INDEX_Po,
  62   UC_CATEGORY_INDEX_S,
  63   UC_CATEGORY_INDEX_Sm,
  64   UC_CATEGORY_INDEX_Sc,
  65   UC_CATEGORY_INDEX_Sk,
  66   UC_CATEGORY_INDEX_So,
  67   UC_CATEGORY_INDEX_Z,
  68   UC_CATEGORY_INDEX_Zs,
  69   UC_CATEGORY_INDEX_Zl,
  70   UC_CATEGORY_INDEX_Zp,
  71   UC_CATEGORY_INDEX_C,
  72   UC_CATEGORY_INDEX_Cc,
  73   UC_CATEGORY_INDEX_Cf,
  74   UC_CATEGORY_INDEX_Cs,
  75   UC_CATEGORY_INDEX_Co,
  76   UC_CATEGORY_INDEX_Cn
  77 };
  78 
  79 #include "unictype/categ_byname.h"
  80 
  81 uc_general_category_t
  82 uc_general_category_byname (const char *category_name)
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84   size_t len;
  85 
  86   len = strlen (category_name);
  87   if (len <= MAX_WORD_LENGTH)
  88     {
  89       char buf[MAX_WORD_LENGTH + 1];
  90       const struct named_category *found;
  91 
  92       /* Copy category_name into buf, converting '_' and '-' to ' '.  */
  93       {
  94         const char *p = category_name;
  95         char *q = buf;
  96 
  97         for (;; p++, q++)
  98           {
  99             char c = *p;
 100 
 101             if (c == '_' || c == '-')
 102               c = ' ';
 103             *q = c;
 104             if (c == '\0')
 105               break;
 106           }
 107       }
 108       /* Here q == buf + len.  */
 109 
 110       /* Do a hash table lookup, with case-insensitive comparison.  */
 111       found = uc_general_category_lookup (buf, len);
 112       if (found != NULL)
 113         /* Use a 'switch' statement here, because a table would introduce
 114            load-time relocations.  */
 115         switch (found->category_index)
 116           {
 117           case UC_CATEGORY_INDEX_L:
 118             return UC_CATEGORY_L;
 119           case UC_CATEGORY_INDEX_LC:
 120             return UC_CATEGORY_LC;
 121           case UC_CATEGORY_INDEX_Lu:
 122             return UC_CATEGORY_Lu;
 123           case UC_CATEGORY_INDEX_Ll:
 124             return UC_CATEGORY_Ll;
 125           case UC_CATEGORY_INDEX_Lt:
 126             return UC_CATEGORY_Lt;
 127           case UC_CATEGORY_INDEX_Lm:
 128             return UC_CATEGORY_Lm;
 129           case UC_CATEGORY_INDEX_Lo:
 130             return UC_CATEGORY_Lo;
 131           case UC_CATEGORY_INDEX_M:
 132             return UC_CATEGORY_M;
 133           case UC_CATEGORY_INDEX_Mn:
 134             return UC_CATEGORY_Mn;
 135           case UC_CATEGORY_INDEX_Mc:
 136             return UC_CATEGORY_Mc;
 137           case UC_CATEGORY_INDEX_Me:
 138             return UC_CATEGORY_Me;
 139           case UC_CATEGORY_INDEX_N:
 140             return UC_CATEGORY_N;
 141           case UC_CATEGORY_INDEX_Nd:
 142             return UC_CATEGORY_Nd;
 143           case UC_CATEGORY_INDEX_Nl:
 144             return UC_CATEGORY_Nl;
 145           case UC_CATEGORY_INDEX_No:
 146             return UC_CATEGORY_No;
 147           case UC_CATEGORY_INDEX_P:
 148             return UC_CATEGORY_P;
 149           case UC_CATEGORY_INDEX_Pc:
 150             return UC_CATEGORY_Pc;
 151           case UC_CATEGORY_INDEX_Pd:
 152             return UC_CATEGORY_Pd;
 153           case UC_CATEGORY_INDEX_Ps:
 154             return UC_CATEGORY_Ps;
 155           case UC_CATEGORY_INDEX_Pe:
 156             return UC_CATEGORY_Pe;
 157           case UC_CATEGORY_INDEX_Pi:
 158             return UC_CATEGORY_Pi;
 159           case UC_CATEGORY_INDEX_Pf:
 160             return UC_CATEGORY_Pf;
 161           case UC_CATEGORY_INDEX_Po:
 162             return UC_CATEGORY_Po;
 163           case UC_CATEGORY_INDEX_S:
 164             return UC_CATEGORY_S;
 165           case UC_CATEGORY_INDEX_Sm:
 166             return UC_CATEGORY_Sm;
 167           case UC_CATEGORY_INDEX_Sc:
 168             return UC_CATEGORY_Sc;
 169           case UC_CATEGORY_INDEX_Sk:
 170             return UC_CATEGORY_Sk;
 171           case UC_CATEGORY_INDEX_So:
 172             return UC_CATEGORY_So;
 173           case UC_CATEGORY_INDEX_Z:
 174             return UC_CATEGORY_Z;
 175           case UC_CATEGORY_INDEX_Zs:
 176             return UC_CATEGORY_Zs;
 177           case UC_CATEGORY_INDEX_Zl:
 178             return UC_CATEGORY_Zl;
 179           case UC_CATEGORY_INDEX_Zp:
 180             return UC_CATEGORY_Zp;
 181           case UC_CATEGORY_INDEX_C:
 182             return UC_CATEGORY_C;
 183           case UC_CATEGORY_INDEX_Cc:
 184             return UC_CATEGORY_Cc;
 185           case UC_CATEGORY_INDEX_Cf:
 186             return UC_CATEGORY_Cf;
 187           case UC_CATEGORY_INDEX_Cs:
 188             return UC_CATEGORY_Cs;
 189           case UC_CATEGORY_INDEX_Co:
 190             return UC_CATEGORY_Co;
 191           case UC_CATEGORY_INDEX_Cn:
 192             return UC_CATEGORY_Cn;
 193           default:
 194             abort ();
 195           }
 196     }
 197   /* Invalid category name.  */
 198   return _UC_CATEGORY_NONE;
 199 }

/* [previous][next][first][last][top][bottom][index][help] */