xref: /haiku/src/system/libroot/posix/glibc/locale/coll-lookup.c (revision 5ed5c9660237b5f90ecc7304add8b3747e344880)
1*5ed5c966SJérôme Duval /* Copyright (C) 2000, 2003 Free Software Foundation, Inc.
2*5ed5c966SJérôme Duval    This file is part of the GNU C Library.
3*5ed5c966SJérôme Duval    Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
4*5ed5c966SJérôme Duval 
5*5ed5c966SJérôme Duval    The GNU C Library is free software; you can redistribute it and/or
6*5ed5c966SJérôme Duval    modify it under the terms of the GNU Lesser General Public
7*5ed5c966SJérôme Duval    License as published by the Free Software Foundation; either
8*5ed5c966SJérôme Duval    version 2.1 of the License, or (at your option) any later version.
9*5ed5c966SJérôme Duval 
10*5ed5c966SJérôme Duval    The GNU C Library is distributed in the hope that it will be useful,
11*5ed5c966SJérôme Duval    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*5ed5c966SJérôme Duval    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*5ed5c966SJérôme Duval    Lesser General Public License for more details.
14*5ed5c966SJérôme Duval 
15*5ed5c966SJérôme Duval    You should have received a copy of the GNU Lesser General Public
16*5ed5c966SJérôme Duval    License along with the GNU C Library; if not, write to the Free
17*5ed5c966SJérôme Duval    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18*5ed5c966SJérôme Duval    02111-1307 USA.  */
19*5ed5c966SJérôme Duval 
20*5ed5c966SJérôme Duval #include <stdint.h>
21*5ed5c966SJérôme Duval 
22*5ed5c966SJérôme Duval #ifndef internal_function
23*5ed5c966SJérôme Duval #	define internal_function
24*5ed5c966SJérôme Duval #endif
25*5ed5c966SJérôme Duval 
26*5ed5c966SJérôme Duval /* Lookup in a table of int32_t, with default value 0.  */
27*5ed5c966SJérôme Duval int32_t
28*5ed5c966SJérôme Duval internal_function
__collidx_table_lookup(const char * table,uint32_t wc)29*5ed5c966SJérôme Duval __collidx_table_lookup (const char *table, uint32_t wc)
30*5ed5c966SJérôme Duval {
31*5ed5c966SJérôme Duval   uint32_t shift1 = ((const uint32_t *) table)[0];
32*5ed5c966SJérôme Duval   uint32_t index1 = wc >> shift1;
33*5ed5c966SJérôme Duval   uint32_t bound = ((const uint32_t *) table)[1];
34*5ed5c966SJérôme Duval   if (index1 < bound)
35*5ed5c966SJérôme Duval     {
36*5ed5c966SJérôme Duval       uint32_t lookup1 = ((const uint32_t *) table)[5 + index1];
37*5ed5c966SJérôme Duval       if (lookup1 != 0)
38*5ed5c966SJérôme Duval 	{
39*5ed5c966SJérôme Duval 	  uint32_t shift2 = ((const uint32_t *) table)[2];
40*5ed5c966SJérôme Duval 	  uint32_t mask2 = ((const uint32_t *) table)[3];
41*5ed5c966SJérôme Duval 	  uint32_t index2 = (wc >> shift2) & mask2;
42*5ed5c966SJérôme Duval 	  uint32_t lookup2 = ((const uint32_t *)(table + lookup1))[index2];
43*5ed5c966SJérôme Duval 	  if (lookup2 != 0)
44*5ed5c966SJérôme Duval 	    {
45*5ed5c966SJérôme Duval 	      uint32_t mask3 = ((const uint32_t *) table)[4];
46*5ed5c966SJérôme Duval 	      uint32_t index3 = wc & mask3;
47*5ed5c966SJérôme Duval 	      int32_t lookup3 = ((const int32_t *)(table + lookup2))[index3];
48*5ed5c966SJérôme Duval 
49*5ed5c966SJérôme Duval 	      return lookup3;
50*5ed5c966SJérôme Duval 	    }
51*5ed5c966SJérôme Duval 	}
52*5ed5c966SJérôme Duval     }
53*5ed5c966SJérôme Duval   return 0;
54*5ed5c966SJérôme Duval }
55*5ed5c966SJérôme Duval 
56*5ed5c966SJérôme Duval 
57*5ed5c966SJérôme Duval /* Lookup in a table of uint32_t, with default value 0xffffffff.  */
58*5ed5c966SJérôme Duval uint32_t
59*5ed5c966SJérôme Duval internal_function
__collseq_table_lookup(const char * table,uint32_t wc)60*5ed5c966SJérôme Duval __collseq_table_lookup (const char *table, uint32_t wc)
61*5ed5c966SJérôme Duval {
62*5ed5c966SJérôme Duval   uint32_t shift1 = ((const uint32_t *) table)[0];
63*5ed5c966SJérôme Duval   uint32_t index1 = wc >> shift1;
64*5ed5c966SJérôme Duval   uint32_t bound = ((const uint32_t *) table)[1];
65*5ed5c966SJérôme Duval   if (index1 < bound)
66*5ed5c966SJérôme Duval     {
67*5ed5c966SJérôme Duval       uint32_t lookup1 = ((const uint32_t *) table)[5 + index1];
68*5ed5c966SJérôme Duval       if (lookup1 != 0)
69*5ed5c966SJérôme Duval 	{
70*5ed5c966SJérôme Duval 	  uint32_t shift2 = ((const uint32_t *) table)[2];
71*5ed5c966SJérôme Duval 	  uint32_t mask2 = ((const uint32_t *) table)[3];
72*5ed5c966SJérôme Duval 	  uint32_t index2 = (wc >> shift2) & mask2;
73*5ed5c966SJérôme Duval 	  uint32_t lookup2 = ((const uint32_t *)(table + lookup1))[index2];
74*5ed5c966SJérôme Duval 	  if (lookup2 != 0)
75*5ed5c966SJérôme Duval 	    {
76*5ed5c966SJérôme Duval 	      uint32_t mask3 = ((const uint32_t *) table)[4];
77*5ed5c966SJérôme Duval 	      uint32_t index3 = wc & mask3;
78*5ed5c966SJérôme Duval 	      uint32_t lookup3 = ((const uint32_t *)(table + lookup2))[index3];
79*5ed5c966SJérôme Duval 
80*5ed5c966SJérôme Duval 	      return lookup3;
81*5ed5c966SJérôme Duval 	    }
82*5ed5c966SJérôme Duval 	}
83*5ed5c966SJérôme Duval     }
84*5ed5c966SJérôme Duval   return ~((uint32_t) 0);
85*5ed5c966SJérôme Duval }
86