1/* Copyright (C) 1996-2024 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 /* With GCC 8 when compiling with -Os the compiler warns that
31 seq1.back_us and seq2.back_us might be used uninitialized.
32 This uninitialized use is impossible for the same reason
33 as described in comments in locale/weightwc.h. */
34 DIAG_PUSH_NEEDS_COMMENT;
35 DIAG_IGNORE_Os_NEEDS_COMMENT (8, "-Wmaybe-uninitialized");
36 int32_t i = table[*(*cpp)++];
37 DIAG_POP_NEEDS_COMMENT;
38 const unsigned char *cp;
39 const unsigned char *usrc;
40
41 if (i >= 0)
42 /* This is an index into the weight table. Cool. */
43 return i;
44
45 /* Oh well, more than one sequence starting with this byte.
46 Search for the correct one. */
47 cp = &extra[-i];
48 usrc = *cpp;
49 --len;
50 while (1)
51 {
52 size_t nhere;
53
54 /* The first thing is the index. */
55 i = *((const int32_t *) cp);
56 cp += sizeof (int32_t);
57
58 /* Next is the length of the byte sequence. These are always
59 short byte sequences so there is no reason to call any
60 function (even if they are inlined). */
61 nhere = *cp++;
62
63 if (i >= 0)
64 {
65 /* It is a single character. If it matches we found our
66 index. Note that at the end of each list there is an
67 entry of length zero which represents the single byte
68 sequence. The first (and here only) byte was tested
69 already. */
70 size_t cnt;
71
72 /* With GCC 5.3 when compiling with -Os the compiler warns
73 that seq2.back_us, which becomes usrc, might be used
74 uninitialized. This can't be true because we pass a length
75 of -1 for len at the same time which means that this loop
76 never executes. */
77 DIAG_PUSH_NEEDS_COMMENT;
78 DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
79 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
80 if (cp[cnt] != usrc[cnt])
81 break;
82 DIAG_POP_NEEDS_COMMENT;
83
84 if (cnt == nhere)
85 {
86 /* Found it. */
87 *cpp += nhere;
88 return i;
89 }
90
91 /* Up to the next entry. */
92 cp += nhere;
93 if (!LOCFILE_ALIGNED_P (1 + nhere))
94 cp += LOCFILE_ALIGN - (1 + nhere) % LOCFILE_ALIGN;
95 }
96 else
97 {
98 /* This is a range of characters. First decide whether the
99 current byte sequence lies in the range. */
100 size_t cnt;
101 size_t offset = 0;
102
103 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
104 if (cp[cnt] != usrc[cnt])
105 break;
106
107 if (cnt != nhere)
108 {
109 if (cnt == len || cp[cnt] > usrc[cnt])
110 {
111 /* Cannot be in this range. */
112 cp += 2 * nhere;
113 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
114 cp += (LOCFILE_ALIGN
115 - (1 + 2 * nhere) % LOCFILE_ALIGN);
116 continue;
117 }
118
119 /* Test against the end of the range. */
120 for (cnt = 0; cnt < nhere; ++cnt)
121 if (cp[nhere + cnt] != usrc[cnt])
122 break;
123
124 if (cnt != nhere && cp[nhere + cnt] < usrc[cnt])
125 {
126 /* Cannot be in this range. */
127 cp += 2 * nhere;
128 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
129 cp += (LOCFILE_ALIGN
130 - (1 + 2 * nhere) % LOCFILE_ALIGN);
131 continue;
132 }
133
134 /* This range matches the next characters. Now find
135 the offset in the indirect table. */
136 for (cnt = 0; cp[cnt] == usrc[cnt]; ++cnt);
137
138 do
139 {
140 offset <<= 8;
141 /* With GCC 7 when compiling with -Os the compiler
142 warns that seq1.back_us and seq2.back_us, which
143 become usrc, might be used uninitialized. This
144 is impossible for the same reason as described
145 above. */
146 DIAG_PUSH_NEEDS_COMMENT;
147 DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
148 offset += usrc[cnt] - cp[cnt];
149 DIAG_POP_NEEDS_COMMENT;
150 }
151 while (++cnt < nhere);
152 }
153
154 *cpp += nhere;
155 return indirect[-i + offset];
156 }
157 }
158
159 /* NOTREACHED */
160 return 0x43219876;
161}
162
163#endif /* weight.h */
164

source code of glibc/locale/weight.h