1/* Copyright (C) 1996-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#ifndef _WEIGHT_H_
19#define _WEIGHT_H_ 1
20
21#include <libc-diag.h>
22
23/* Find index of weight. */
24static inline int32_t __attribute__ ((always_inline))
25findidx (const int32_t *table,
26 const int32_t *indirect,
27 const unsigned char *extra,
28 const unsigned char **cpp, size_t len)
29{
30 int_fast32_t i = table[*(*cpp)++];
31 const unsigned char *cp;
32 const unsigned char *usrc;
33
34 if (i >= 0)
35 /* This is an index into the weight table. Cool. */
36 return i;
37
38 /* Oh well, more than one sequence starting with this byte.
39 Search for the correct one. */
40 cp = &extra[-i];
41 usrc = *cpp;
42 --len;
43 while (1)
44 {
45 size_t nhere;
46
47 /* The first thing is the index. */
48 i = *((const int32_t *) cp);
49 cp += sizeof (int32_t);
50
51 /* Next is the length of the byte sequence. These are always
52 short byte sequences so there is no reason to call any
53 function (even if they are inlined). */
54 nhere = *cp++;
55
56 if (i >= 0)
57 {
58 /* It is a single character. If it matches we found our
59 index. Note that at the end of each list there is an
60 entry of length zero which represents the single byte
61 sequence. The first (and here only) byte was tested
62 already. */
63 size_t cnt;
64
65 /* With GCC 5.3 when compiling with -Os the compiler warns
66 that seq2.back_us, which becomes usrc, might be used
67 uninitialized. This can't be true because we pass a length
68 of -1 for len at the same time which means that this loop
69 never executes. */
70 DIAG_PUSH_NEEDS_COMMENT;
71 DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
72 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
73 if (cp[cnt] != usrc[cnt])
74 break;
75 DIAG_POP_NEEDS_COMMENT;
76
77 if (cnt == nhere)
78 {
79 /* Found it. */
80 *cpp += nhere;
81 return i;
82 }
83
84 /* Up to the next entry. */
85 cp += nhere;
86 if (!LOCFILE_ALIGNED_P (1 + nhere))
87 cp += LOCFILE_ALIGN - (1 + nhere) % LOCFILE_ALIGN;
88 }
89 else
90 {
91 /* This is a range of characters. First decide whether the
92 current byte sequence lies in the range. */
93 size_t cnt;
94 size_t offset = 0;
95
96 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
97 if (cp[cnt] != usrc[cnt])
98 break;
99
100 if (cnt != nhere)
101 {
102 if (cnt == len || cp[cnt] > usrc[cnt])
103 {
104 /* Cannot be in this range. */
105 cp += 2 * nhere;
106 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
107 cp += (LOCFILE_ALIGN
108 - (1 + 2 * nhere) % LOCFILE_ALIGN);
109 continue;
110 }
111
112 /* Test against the end of the range. */
113 for (cnt = 0; cnt < nhere; ++cnt)
114 if (cp[nhere + cnt] != usrc[cnt])
115 break;
116
117 if (cnt != nhere && cp[nhere + cnt] < usrc[cnt])
118 {
119 /* Cannot be in this range. */
120 cp += 2 * nhere;
121 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
122 cp += (LOCFILE_ALIGN
123 - (1 + 2 * nhere) % LOCFILE_ALIGN);
124 continue;
125 }
126
127 /* This range matches the next characters. Now find
128 the offset in the indirect table. */
129 for (cnt = 0; cp[cnt] == usrc[cnt]; ++cnt);
130
131 do
132 {
133 offset <<= 8;
134 /* With GCC 7 when compiling with -Os the compiler
135 warns that seq1.back_us and seq2.back_us, which
136 become usrc, might be used uninitialized. This
137 is impossible for the same reason as described
138 above. */
139 DIAG_PUSH_NEEDS_COMMENT;
140 DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
141 offset += usrc[cnt] - cp[cnt];
142 DIAG_POP_NEEDS_COMMENT;
143 }
144 while (++cnt < nhere);
145 }
146
147 *cpp += nhere;
148 return indirect[-i + offset];
149 }
150 }
151
152 /* NOTREACHED */
153 return 0x43219876;
154}
155
156#endif /* weight.h */
157

source code of glibc/locale/weight.h