xref: /haiku/src/system/libroot/posix/glibc/locale/weightwc.h (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /* Copyright (C) 1996-2000, 2001 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 wint_t **cpp)
23 {
24   int32_t i;
25   const wint_t *cp;
26   wint_t ch;
27 
28   ch = *(*cpp)++;
29   i = collidx_table_lookup ((const char *) table, ch);
30 
31   if (i >= 0)
32     /* This is an index into the weight table.  Cool.  */
33     return i;
34 
35   /* Oh well, more than one sequence starting with this byte.
36      Search for the correct one.  */
37   cp = &extra[-i];
38   while (1)
39     {
40       size_t nhere;
41       const wint_t *usrc = *cpp;
42 
43       /* The first thing is the index.  */
44       i = *cp++;
45 
46       /* Next is the length of the byte sequence.  These are always
47 	 short byte sequences so there is no reason to call any
48 	 function (even if they are inlined).  */
49       nhere = *cp++;
50 
51       if (i >= 0)
52 	{
53 	  /* It is a single character.  If it matches we found our
54 	     index.  Note that at the end of each list there is an
55 	     entry of length zero which represents the single byte
56 	     sequence.  The first (and here only) byte was tested
57 	     already.  */
58 	  size_t cnt;
59 
60 	  for (cnt = 0; cnt < nhere; ++cnt)
61 	    if (cp[cnt] != usrc[cnt])
62 	      break;
63 
64 	  if (cnt == nhere)
65 	    {
66 	      /* Found it.  */
67 	      *cpp += nhere;
68 	      return i;
69 	    }
70 
71 	  /* Up to the next entry.  */
72 	  cp += nhere;
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;
80 
81 	  for (cnt = 0; cnt < nhere - 1; ++cnt)
82 	    if (cp[cnt] != usrc[cnt])
83 	      break;
84 
85 	  if (cnt < nhere - 1)
86 	    {
87 	      cp += 2 * nhere;
88 	      continue;
89 	    }
90 
91 	  if (cp[nhere - 1] > usrc[nhere -1])
92 	    {
93 	      cp += 2 * nhere;
94 	      continue;
95 	    }
96 
97 	  if (cp[2 * nhere - 1] < usrc[nhere -1])
98 	    {
99 	      cp += 2 * nhere;
100 	      continue;
101 	    }
102 
103 	  /* This range matches the next characters.  Now find
104 	     the offset in the indirect table.  */
105 	  offset = usrc[nhere - 1] - cp[nhere - 1];
106 	  *cpp += nhere;
107 
108 	  return indirect[-i + offset];
109 	}
110     }
111 
112   /* NOTREACHED */
113   return 0x43219876;
114 }
115