Fix vsyslog namespace (bug 18533).
[glibc.git] / locale / weight.h
blob721bf7dabff0e039843919b07962aa3722d8bf94
1 /* Copyright (C) 1996-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Ulrich Drepper, <drepper@cygnus.com>.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _WEIGHT_H_
20 #define _WEIGHT_H_ 1
22 /* Find index of weight. */
23 static inline int32_t __attribute__ ((always_inline))
24 findidx (const int32_t *table,
25 const int32_t *indirect,
26 const unsigned char *extra,
27 const unsigned char **cpp, size_t len)
29 int_fast32_t i = table[*(*cpp)++];
30 const unsigned char *cp;
31 const unsigned char *usrc;
33 if (i >= 0)
34 /* This is an index into the weight table. Cool. */
35 return i;
37 /* Oh well, more than one sequence starting with this byte.
38 Search for the correct one. */
39 cp = &extra[-i];
40 usrc = *cpp;
41 --len;
42 while (1)
44 size_t nhere;
46 /* The first thing is the index. */
47 i = *((const int32_t *) cp);
48 cp += sizeof (int32_t);
50 /* Next is the length of the byte sequence. These are always
51 short byte sequences so there is no reason to call any
52 function (even if they are inlined). */
53 nhere = *cp++;
55 if (i >= 0)
57 /* It is a single character. If it matches we found our
58 index. Note that at the end of each list there is an
59 entry of length zero which represents the single byte
60 sequence. The first (and here only) byte was tested
61 already. */
62 size_t cnt;
64 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
65 if (cp[cnt] != usrc[cnt])
66 break;
68 if (cnt == nhere)
70 /* Found it. */
71 *cpp += nhere;
72 return i;
75 /* Up to the next entry. */
76 cp += nhere;
77 if (!LOCFILE_ALIGNED_P (1 + nhere))
78 cp += LOCFILE_ALIGN - (1 + nhere) % LOCFILE_ALIGN;
80 else
82 /* This is a range of characters. First decide whether the
83 current byte sequence lies in the range. */
84 size_t cnt;
85 size_t offset = 0;
87 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
88 if (cp[cnt] != usrc[cnt])
89 break;
91 if (cnt != nhere)
93 if (cnt == len || cp[cnt] > usrc[cnt])
95 /* Cannot be in this range. */
96 cp += 2 * nhere;
97 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
98 cp += (LOCFILE_ALIGN
99 - (1 + 2 * nhere) % LOCFILE_ALIGN);
100 continue;
103 /* Test against the end of the range. */
104 for (cnt = 0; cnt < nhere; ++cnt)
105 if (cp[nhere + cnt] != usrc[cnt])
106 break;
108 if (cnt != nhere && cp[nhere + cnt] < usrc[cnt])
110 /* Cannot be in this range. */
111 cp += 2 * nhere;
112 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
113 cp += (LOCFILE_ALIGN
114 - (1 + 2 * nhere) % LOCFILE_ALIGN);
115 continue;
118 /* This range matches the next characters. Now find
119 the offset in the indirect table. */
120 for (cnt = 0; cp[cnt] == usrc[cnt]; ++cnt);
124 offset <<= 8;
125 offset += usrc[cnt] - cp[cnt];
127 while (++cnt < nhere);
130 *cpp += nhere;
131 return indirect[-i + offset];
135 /* NOTREACHED */
136 return 0x43219876;
139 #endif /* weight.h */