]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 1996-2024 Free Software Foundation, Inc. |
6d52618b | 2 | This file is part of the GNU C Library. |
19bc17a9 | 3 | |
6d52618b | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
19bc17a9 | 8 | |
6d52618b UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
19bc17a9 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
19bc17a9 | 17 | |
8c0ab919 RM |
18 | #ifndef _WEIGHT_H_ |
19 | #define _WEIGHT_H_ 1 | |
20 | ||
9090848d | 21 | #include <libc-diag.h> |
bb5badf1 | 22 | |
450bf66e | 23 | /* Find index of weight. */ |
8c0ab919 RM |
24 | static inline int32_t __attribute__ ((always_inline)) |
25 | findidx (const int32_t *table, | |
26 | const int32_t *indirect, | |
27 | const unsigned char *extra, | |
28 | const unsigned char **cpp, size_t len) | |
450bf66e | 29 | { |
c651f9da MJ |
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"); | |
535e935a | 36 | int32_t i = table[*(*cpp)++]; |
c651f9da | 37 | DIAG_POP_NEEDS_COMMENT; |
450bf66e | 38 | const unsigned char *cp; |
dbfdf944 | 39 | const unsigned char *usrc; |
19bc17a9 | 40 | |
450bf66e UD |
41 | if (i >= 0) |
42 | /* This is an index into the weight table. Cool. */ | |
43 | return i; | |
19bc17a9 | 44 | |
450bf66e UD |
45 | /* Oh well, more than one sequence starting with this byte. |
46 | Search for the correct one. */ | |
47 | cp = &extra[-i]; | |
dbfdf944 | 48 | usrc = *cpp; |
f3a6cc0a | 49 | --len; |
450bf66e | 50 | while (1) |
0393dfd6 | 51 | { |
450bf66e | 52 | size_t nhere; |
19bc17a9 | 53 | |
450bf66e | 54 | /* The first thing is the index. */ |
8b6e6767 | 55 | i = *((const int32_t *) cp); |
450bf66e | 56 | cp += sizeof (int32_t); |
19bc17a9 | 57 | |
450bf66e UD |
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++; | |
19bc17a9 | 62 | |
450bf66e | 63 | if (i >= 0) |
19bc17a9 | 64 | { |
450bf66e UD |
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; | |
19bc17a9 | 71 | |
93fe09cb CD |
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"); | |
f3a6cc0a | 79 | for (cnt = 0; cnt < nhere && cnt < len; ++cnt) |
450bf66e UD |
80 | if (cp[cnt] != usrc[cnt]) |
81 | break; | |
93fe09cb | 82 | DIAG_POP_NEEDS_COMMENT; |
19bc17a9 | 83 | |
450bf66e UD |
84 | if (cnt == nhere) |
85 | { | |
86 | /* Found it. */ | |
87 | *cpp += nhere; | |
88 | return i; | |
89 | } | |
19bc17a9 | 90 | |
450bf66e UD |
91 | /* Up to the next entry. */ |
92 | cp += nhere; | |
975569d0 JM |
93 | if (!LOCFILE_ALIGNED_P (1 + nhere)) |
94 | cp += LOCFILE_ALIGN - (1 + nhere) % LOCFILE_ALIGN; | |
19bc17a9 | 95 | } |
450bf66e UD |
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; | |
19bc17a9 | 102 | |
f3a6cc0a | 103 | for (cnt = 0; cnt < nhere && cnt < len; ++cnt) |
450bf66e UD |
104 | if (cp[cnt] != usrc[cnt]) |
105 | break; | |
19bc17a9 | 106 | |
450bf66e UD |
107 | if (cnt != nhere) |
108 | { | |
f3a6cc0a | 109 | if (cnt == len || cp[cnt] > usrc[cnt]) |
450bf66e UD |
110 | { |
111 | /* Cannot be in this range. */ | |
112 | cp += 2 * nhere; | |
975569d0 JM |
113 | if (!LOCFILE_ALIGNED_P (1 + 2 * nhere)) |
114 | cp += (LOCFILE_ALIGN | |
115 | - (1 + 2 * nhere) % LOCFILE_ALIGN); | |
450bf66e UD |
116 | continue; |
117 | } | |
19bc17a9 | 118 | |
450bf66e UD |
119 | /* Test against the end of the range. */ |
120 | for (cnt = 0; cnt < nhere; ++cnt) | |
121 | if (cp[nhere + cnt] != usrc[cnt]) | |
122 | break; | |
19bc17a9 | 123 | |
450bf66e UD |
124 | if (cnt != nhere && cp[nhere + cnt] < usrc[cnt]) |
125 | { | |
126 | /* Cannot be in this range. */ | |
127 | cp += 2 * nhere; | |
975569d0 JM |
128 | if (!LOCFILE_ALIGNED_P (1 + 2 * nhere)) |
129 | cp += (LOCFILE_ALIGN | |
130 | - (1 + 2 * nhere) % LOCFILE_ALIGN); | |
450bf66e UD |
131 | continue; |
132 | } | |
19bc17a9 | 133 | |
450bf66e UD |
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; | |
ce999220 JM |
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"); | |
450bf66e | 148 | offset += usrc[cnt] - cp[cnt]; |
ce999220 | 149 | DIAG_POP_NEEDS_COMMENT; |
450bf66e UD |
150 | } |
151 | while (++cnt < nhere); | |
19bc17a9 | 152 | } |
19bc17a9 | 153 | |
450bf66e | 154 | *cpp += nhere; |
dbfdf944 | 155 | return indirect[-i + offset]; |
450bf66e | 156 | } |
19bc17a9 | 157 | } |
19bc17a9 | 158 | |
450bf66e UD |
159 | /* NOTREACHED */ |
160 | return 0x43219876; | |
161 | } | |
8c0ab919 RM |
162 | |
163 | #endif /* weight.h */ |