| 1 | /* Copyright (C) 1996-2020 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Written by Ulrich Drepper, <drepper@cygnus.com>. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _WEIGHTWC_H_ |
| 20 | #define _WEIGHTWC_H_ 1 |
| 21 | |
| 22 | #include <libc-diag.h> |
| 23 | |
| 24 | /* Find index of weight. */ |
| 25 | static inline int32_t __attribute__ ((always_inline)) |
| 26 | findidx (const int32_t *table, |
| 27 | const int32_t *indirect, |
| 28 | const wint_t *, |
| 29 | const wint_t **cpp, size_t len) |
| 30 | { |
| 31 | /* With GCC 7 when compiling with -Os the compiler warns that |
| 32 | seq1.back_us and seq2.back_us, which become *cpp, might be used |
| 33 | uninitialized. This is impossible as this function cannot be |
| 34 | called except in cases where those fields have been |
| 35 | initialized. */ |
| 36 | DIAG_PUSH_NEEDS_COMMENT; |
| 37 | DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized" ); |
| 38 | wint_t ch = *(*cpp)++; |
| 39 | DIAG_POP_NEEDS_COMMENT; |
| 40 | int32_t i = __collidx_table_lookup ((const char *) table, ch); |
| 41 | |
| 42 | if (i >= 0) |
| 43 | /* This is an index into the weight table. Cool. */ |
| 44 | return i; |
| 45 | |
| 46 | /* Oh well, more than one sequence starting with this byte. |
| 47 | Search for the correct one. */ |
| 48 | const int32_t *cp = (const int32_t *) &extra[-i]; |
| 49 | --len; |
| 50 | while (1) |
| 51 | { |
| 52 | size_t nhere; |
| 53 | const int32_t *usrc = (const int32_t *) *cpp; |
| 54 | |
| 55 | /* The first thing is the index. */ |
| 56 | i = *cp++; |
| 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 | } |
| 94 | else |
| 95 | { |
| 96 | /* This is a range of characters. First decide whether the |
| 97 | current byte sequence lies in the range. */ |
| 98 | size_t cnt; |
| 99 | size_t offset; |
| 100 | |
| 101 | /* With GCC 7 when compiling with -Os the compiler warns |
| 102 | that seq1.back_us and seq2.back_us, which become usrc, |
| 103 | might be used uninitialized. This is impossible for the |
| 104 | same reason as described above. */ |
| 105 | DIAG_PUSH_NEEDS_COMMENT; |
| 106 | DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized" ); |
| 107 | for (cnt = 0; cnt < nhere - 1 && cnt < len; ++cnt) |
| 108 | if (cp[cnt] != usrc[cnt]) |
| 109 | break; |
| 110 | DIAG_POP_NEEDS_COMMENT; |
| 111 | |
| 112 | if (cnt < nhere - 1 || cnt == len) |
| 113 | { |
| 114 | cp += 2 * nhere; |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | /* With GCC 7 when compiling with -Os the compiler warns |
| 119 | that seq1.back_us and seq2.back_us, which become usrc, |
| 120 | might be used uninitialized. This is impossible for the |
| 121 | same reason as described above. */ |
| 122 | DIAG_PUSH_NEEDS_COMMENT; |
| 123 | DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized" ); |
| 124 | if (cp[nhere - 1] > usrc[nhere - 1]) |
| 125 | { |
| 126 | cp += 2 * nhere; |
| 127 | continue; |
| 128 | } |
| 129 | DIAG_POP_NEEDS_COMMENT; |
| 130 | |
| 131 | if (cp[2 * nhere - 1] < usrc[nhere - 1]) |
| 132 | { |
| 133 | cp += 2 * nhere; |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | /* This range matches the next characters. Now find |
| 138 | the offset in the indirect table. */ |
| 139 | offset = usrc[nhere - 1] - cp[nhere - 1]; |
| 140 | *cpp += nhere; |
| 141 | |
| 142 | return indirect[-i + offset]; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* NOTREACHED */ |
| 147 | return 0x43219876; |
| 148 | } |
| 149 | |
| 150 | #endif /* weightwc.h */ |
| 151 | |