]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/weightwc.h
locale/programs/locarchive.c: fix warn unused result
[thirdparty/glibc.git] / locale / weightwc.h
CommitLineData
6d7e8eda 1/* Copyright (C) 1996-2023 Free Software Foundation, Inc.
9eb157c8 2 This file is part of the GNU C Library.
9eb157c8
UD
3
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.
9eb157c8
UD
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
41bdb6e2 12 Lesser General Public License for more details.
9eb157c8 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/>. */
9eb157c8 17
8c0ab919
RM
18#ifndef _WEIGHTWC_H_
19#define _WEIGHTWC_H_ 1
20
9090848d 21#include <libc-diag.h>
bb5badf1 22
9eb157c8 23/* Find index of weight. */
8c0ab919
RM
24static inline int32_t __attribute__ ((always_inline))
25findidx (const int32_t *table,
26 const int32_t *indirect,
27 const wint_t *extra,
28 const wint_t **cpp, size_t len)
9eb157c8 29{
02f2fead
JM
30 /* With GCC 7 when compiling with -Os the compiler warns that
31 seq1.back_us and seq2.back_us, which become *cpp, might be used
32 uninitialized. This is impossible as this function cannot be
33 called except in cases where those fields have been
34 initialized. */
35 DIAG_PUSH_NEEDS_COMMENT;
36 DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
1c99f950 37 wint_t ch = *(*cpp)++;
02f2fead 38 DIAG_POP_NEEDS_COMMENT;
1c99f950 39 int32_t i = __collidx_table_lookup ((const char *) table, ch);
9eb157c8
UD
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. */
701666b7 47 const int32_t *cp = (const int32_t *) &extra[-i];
f3a6cc0a 48 --len;
9eb157c8
UD
49 while (1)
50 {
51 size_t nhere;
1c99f950 52 const int32_t *usrc = (const int32_t *) *cpp;
9eb157c8
UD
53
54 /* The first thing is the index. */
46661856 55 i = *cp++;
9eb157c8
UD
56
57 /* Next is the length of the byte sequence. These are always
58 short byte sequences so there is no reason to call any
59 function (even if they are inlined). */
60 nhere = *cp++;
61
62 if (i >= 0)
63 {
64 /* It is a single character. If it matches we found our
65 index. Note that at the end of each list there is an
66 entry of length zero which represents the single byte
67 sequence. The first (and here only) byte was tested
68 already. */
69 size_t cnt;
70
93fe09cb
CD
71 /* With GCC 5.3 when compiling with -Os the compiler warns
72 that seq2.back_us, which becomes usrc, might be used
73 uninitialized. This can't be true because we pass a length
74 of -1 for len at the same time which means that this loop
75 never executes. */
76 DIAG_PUSH_NEEDS_COMMENT;
77 DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
f3a6cc0a 78 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
9eb157c8
UD
79 if (cp[cnt] != usrc[cnt])
80 break;
93fe09cb 81 DIAG_POP_NEEDS_COMMENT;
9eb157c8
UD
82
83 if (cnt == nhere)
84 {
85 /* Found it. */
86 *cpp += nhere;
87 return i;
88 }
89
90 /* Up to the next entry. */
91 cp += nhere;
92 }
93 else
94 {
95 /* This is a range of characters. First decide whether the
96 current byte sequence lies in the range. */
97 size_t cnt;
740c2239 98 size_t offset;
9eb157c8 99
ce999220
JM
100 /* With GCC 7 when compiling with -Os the compiler warns
101 that seq1.back_us and seq2.back_us, which become usrc,
102 might be used uninitialized. This is impossible for the
103 same reason as described above. */
104 DIAG_PUSH_NEEDS_COMMENT;
105 DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
f3a6cc0a 106 for (cnt = 0; cnt < nhere - 1 && cnt < len; ++cnt)
9eb157c8
UD
107 if (cp[cnt] != usrc[cnt])
108 break;
ce999220 109 DIAG_POP_NEEDS_COMMENT;
9eb157c8 110
9c79cec8 111 if (cnt < nhere - 1 || cnt == len)
740c2239
UD
112 {
113 cp += 2 * nhere;
114 continue;
115 }
116
02f2fead
JM
117 /* With GCC 7 when compiling with -Os the compiler warns
118 that seq1.back_us and seq2.back_us, which become usrc,
119 might be used uninitialized. This is impossible for the
120 same reason as described above. */
121 DIAG_PUSH_NEEDS_COMMENT;
122 DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
9c79cec8 123 if (cp[nhere - 1] > usrc[nhere - 1])
9eb157c8 124 {
740c2239
UD
125 cp += 2 * nhere;
126 continue;
9eb157c8 127 }
02f2fead 128 DIAG_POP_NEEDS_COMMENT;
9eb157c8 129
9c79cec8 130 if (cp[2 * nhere - 1] < usrc[nhere - 1])
740c2239
UD
131 {
132 cp += 2 * nhere;
133 continue;
134 }
135
136 /* This range matches the next characters. Now find
137 the offset in the indirect table. */
138 offset = usrc[nhere - 1] - cp[nhere - 1];
9eb157c8 139 *cpp += nhere;
740c2239
UD
140
141 return indirect[-i + offset];
9eb157c8
UD
142 }
143 }
144
145 /* NOTREACHED */
146 return 0x43219876;
147}
8c0ab919
RM
148
149#endif /* weightwc.h */