]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/weightwc.h
setlocale: Use the heap for the copy of the locale argument
[thirdparty/glibc.git] / locale / weightwc.h
CommitLineData
d4697bc9 1/* Copyright (C) 1996-2014 Free Software Foundation, Inc.
9eb157c8
UD
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
41bdb6e2
AJ
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.
9eb157c8
UD
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
41bdb6e2 13 Lesser General Public License for more details.
9eb157c8 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
9eb157c8
UD
18
19/* Find index of weight. */
7090d3ca 20auto inline int32_t
dd9423a6 21__attribute ((always_inline))
f3a6cc0a 22findidx (const wint_t **cpp, size_t len)
9eb157c8 23{
1c99f950
UD
24 wint_t ch = *(*cpp)++;
25 int32_t i = __collidx_table_lookup ((const char *) table, ch);
9eb157c8
UD
26
27 if (i >= 0)
28 /* This is an index into the weight table. Cool. */
29 return i;
30
31 /* Oh well, more than one sequence starting with this byte.
32 Search for the correct one. */
701666b7 33 const int32_t *cp = (const int32_t *) &extra[-i];
f3a6cc0a 34 --len;
9eb157c8
UD
35 while (1)
36 {
37 size_t nhere;
1c99f950 38 const int32_t *usrc = (const int32_t *) *cpp;
9eb157c8
UD
39
40 /* The first thing is the index. */
46661856 41 i = *cp++;
9eb157c8
UD
42
43 /* Next is the length of the byte sequence. These are always
44 short byte sequences so there is no reason to call any
45 function (even if they are inlined). */
46 nhere = *cp++;
47
48 if (i >= 0)
49 {
50 /* It is a single character. If it matches we found our
51 index. Note that at the end of each list there is an
52 entry of length zero which represents the single byte
53 sequence. The first (and here only) byte was tested
54 already. */
55 size_t cnt;
56
f3a6cc0a 57 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
9eb157c8
UD
58 if (cp[cnt] != usrc[cnt])
59 break;
60
61 if (cnt == nhere)
62 {
63 /* Found it. */
64 *cpp += nhere;
65 return i;
66 }
67
68 /* Up to the next entry. */
69 cp += nhere;
70 }
71 else
72 {
73 /* This is a range of characters. First decide whether the
74 current byte sequence lies in the range. */
75 size_t cnt;
740c2239 76 size_t offset;
9eb157c8 77
f3a6cc0a 78 for (cnt = 0; cnt < nhere - 1 && cnt < len; ++cnt)
9eb157c8
UD
79 if (cp[cnt] != usrc[cnt])
80 break;
81
740c2239
UD
82 if (cnt < nhere - 1)
83 {
84 cp += 2 * nhere;
85 continue;
86 }
87
88 if (cp[nhere - 1] > usrc[nhere -1])
9eb157c8 89 {
740c2239
UD
90 cp += 2 * nhere;
91 continue;
9eb157c8
UD
92 }
93
740c2239
UD
94 if (cp[2 * nhere - 1] < usrc[nhere -1])
95 {
96 cp += 2 * nhere;
97 continue;
98 }
99
100 /* This range matches the next characters. Now find
101 the offset in the indirect table. */
102 offset = usrc[nhere - 1] - cp[nhere - 1];
9eb157c8 103 *cpp += nhere;
740c2239
UD
104
105 return indirect[-i + offset];
9eb157c8
UD
106 }
107 }
108
109 /* NOTREACHED */
110 return 0x43219876;
111}