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