/src/wget/lib/mbiter-aux.c
Line | Count | Source |
1 | | /* Auxiliary functions for iterating through multibyte strings. |
2 | | Copyright (C) 2026 Free Software Foundation, Inc. |
3 | | |
4 | | This file is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU Lesser General Public License as |
6 | | published by the Free Software Foundation; either version 2.1 of the |
7 | | License, or (at your option) any later version. |
8 | | |
9 | | This file 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 |
12 | | GNU Lesser General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU Lesser General Public License |
15 | | along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
16 | | |
17 | | /* Written by Bruno Haible <bruno@clisp.org>. */ |
18 | | |
19 | | #include <config.h> |
20 | | |
21 | | /* Specification. */ |
22 | | #include "mbiter-aux.h" |
23 | | |
24 | | #include <uchar.h> |
25 | | |
26 | | bool |
27 | | mbiter_is_utf8 (int *cache) |
28 | 0 | { |
29 | 0 | if (*cache < 0) |
30 | 0 | { |
31 | | /* UTF-8 is the only encoding in use which maps the bytes 0xC4 0x80 |
32 | | to U+0100. (See libiconv/tests/?*.TXT for all the mapping tables.) |
33 | | We can assume that in this case, the char32_t encoding is Unicode |
34 | | (not platform-dependent like for other locale encodings). */ |
35 | 0 | mbstate_t state; mbszero (&state); |
36 | 0 | char32_t wc; |
37 | 0 | *cache = (mbrtoc32 (&wc, "\xc4\x80", 2, &state) == 2 && wc == 0x100); |
38 | 0 | } |
39 | 0 | return *cache; |
40 | 0 | } |
41 | | |
42 | | /* If the current locale encoding is UTF-8 and a preceding |
43 | | mbrtoc32 (&uc, S, N, &state) |
44 | | invocation returned (size_t) -1, this function returns the number of |
45 | | initial bytes that form a maximal subpart in the sense of |
46 | | https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf page 127..129. |
47 | | The result is >= 1, <= N. */ |
48 | | size_t |
49 | | mbiter_utf8_maximal_subpart (const char *s, size_t n) |
50 | 0 | { |
51 | | /* Based on lib/unistr/u8-mbtouc.c. */ |
52 | 0 | if (n >= 2) |
53 | 0 | { |
54 | 0 | unsigned char c = (unsigned char) *s; |
55 | 0 | if (c >= 0xe0) |
56 | 0 | { |
57 | 0 | if (c < 0xf0) |
58 | 0 | { |
59 | 0 | unsigned char c2 = (unsigned char) s[1]; |
60 | 0 | if ((c2 ^ 0x80) < 0x40 |
61 | 0 | && (c >= 0xe1 || c2 >= 0xa0) |
62 | 0 | && (c != 0xed || c2 < 0xa0)) |
63 | 0 | return 2; |
64 | 0 | } |
65 | 0 | else if (c <= 0xf4) |
66 | 0 | { |
67 | 0 | unsigned char c2 = (unsigned char) s[1]; |
68 | 0 | if ((c2 ^ 0x80) < 0x40 |
69 | 0 | && (c >= 0xf1 || c2 >= 0x90) |
70 | 0 | && (c < 0xf4 || (/* c == 0xf4 && */ c2 < 0x90))) |
71 | 0 | { |
72 | 0 | if (n >= 3 && ((unsigned char) s[2] ^ 0x80) < 0x40) |
73 | 0 | return 3; |
74 | 0 | else |
75 | 0 | return 2; |
76 | 0 | } |
77 | 0 | } |
78 | 0 | } |
79 | 0 | } |
80 | 0 | return 1; |
81 | 0 | } |