xref: /haiku/src/system/libroot/posix/glibc/locale/weight.h (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Written by Ulrich Drepper, <drepper@cygnus.com>.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library 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
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19 
20 /* Find index of weight.  */
21 static inline int32_t
22 findidx (const unsigned char **cpp)
23 {
24   int_fast32_t i = table[*(*cpp)++];
25   const unsigned char *cp;
26   const unsigned char *usrc;
27 
28   if (i >= 0)
29     /* This is an index into the weight table.  Cool.  */
30     return i;
31 
32   /* Oh well, more than one sequence starting with this byte.
33      Search for the correct one.  */
34   cp = &extra[-i];
35   usrc = *cpp;
36   while (1)
37     {
38       size_t nhere;
39 
40       /* The first thing is the index.  */
41       i = *((const int32_t *) cp);
42       cp += sizeof (int32_t);
43 
44       /* Next is the length of the byte sequence.  These are always
45 	 short byte sequences so there is no reason to call any
46 	 function (even if they are inlined).  */
47       nhere = *cp++;
48 
49       if (i >= 0)
50 	{
51 	  /* It is a single character.  If it matches we found our
52 	     index.  Note that at the end of each list there is an
53 	     entry of length zero which represents the single byte
54 	     sequence.  The first (and here only) byte was tested
55 	     already.  */
56 	  size_t cnt;
57 
58 	  for (cnt = 0; cnt < nhere; ++cnt)
59 	    if (cp[cnt] != usrc[cnt])
60 	      break;
61 
62 	  if (cnt == nhere)
63 	    {
64 	      /* Found it.  */
65 	      *cpp += nhere;
66 	      return i;
67 	    }
68 
69 	  /* Up to the next entry.  */
70 	  cp += nhere;
71 	  if ((1 + nhere) % __alignof__ (int32_t) != 0)
72 	    cp += __alignof__ (int32_t) - (1 + nhere) % __alignof__ (int32_t);
73 	}
74       else
75 	{
76 	  /* This is a range of characters.  First decide whether the
77 	     current byte sequence lies in the range.  */
78 	  size_t cnt;
79 	  size_t offset = 0;
80 
81 	  for (cnt = 0; cnt < nhere; ++cnt)
82 	    if (cp[cnt] != usrc[cnt])
83 	      break;
84 
85 	  if (cnt != nhere)
86 	    {
87 	      if (cp[cnt] > usrc[cnt])
88 		{
89 		  /* Cannot be in this range.  */
90 		  cp += 2 * nhere;
91 		  if ((1 + 2 * nhere) % __alignof__ (int32_t) != 0)
92 		    cp += (__alignof__ (int32_t)
93 			   - (1 + 2 * nhere) % __alignof__ (int32_t));
94 		  continue;
95 		}
96 
97 	      /* Test against the end of the range.  */
98 	      for (cnt = 0; cnt < nhere; ++cnt)
99 		if (cp[nhere + cnt] != usrc[cnt])
100 		  break;
101 
102 	      if (cnt != nhere && cp[nhere + cnt] < usrc[cnt])
103 		{
104 		  /* Cannot be in this range.  */
105 		  cp += 2 * nhere;
106 		  if ((1 + 2 * nhere) % __alignof__ (int32_t) != 0)
107 		    cp += (__alignof__ (int32_t)
108 			   - (1 + 2 * nhere) % __alignof__ (int32_t));
109 		  continue;
110 		}
111 
112 	      /* This range matches the next characters.  Now find
113 		 the offset in the indirect table.  */
114 	      for (cnt = 0; cp[cnt] == usrc[cnt]; ++cnt);
115 
116 	      do
117 		{
118 		  offset <<= 8;
119 		  offset += usrc[cnt] - cp[cnt];
120 		}
121 	      while (++cnt < nhere);
122 	    }
123 
124 	  *cpp += nhere;
125 	  return indirect[-i + offset];
126 	}
127     }
128 
129   /* NOTREACHED */
130   return 0x43219876;
131 }
132