Commit | Line | Data |
---|---|---|
24d56127 LC |
1 | /* Iterate over previous character in UTF-8 string. |
2 | Copyright (C) 2002, 2006-2007 Free Software Foundation, Inc. | |
3 | Written by Bruno Haible <bruno@clisp.org>, 2002. | |
4 | ||
5 | This program is free software: you can redistribute it and/or modify it | |
6 | under the terms of the GNU Lesser General Public License as published | |
7 | by the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program 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 License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | #include <config.h> | |
19 | ||
20 | /* Specification. */ | |
21 | #include "unistr.h" | |
22 | ||
23 | const uint8_t * | |
24 | u8_prev (ucs4_t *puc, const uint8_t *s, const uint8_t *start) | |
25 | { | |
26 | /* Keep in sync with unistr.h and utf8-ucs4.c. */ | |
27 | if (s != start) | |
28 | { | |
29 | uint8_t c_1 = s[-1]; | |
30 | ||
31 | if (c_1 < 0x80) | |
1cd4fffc LC |
32 | { |
33 | *puc = c_1; | |
34 | return s - 1; | |
35 | } | |
24d56127 LC |
36 | #if CONFIG_UNICODE_SAFETY |
37 | if ((c_1 ^ 0x80) < 0x40) | |
38 | #endif | |
1cd4fffc LC |
39 | if (s - 1 != start) |
40 | { | |
41 | uint8_t c_2 = s[-2]; | |
24d56127 | 42 | |
1cd4fffc LC |
43 | if (c_2 >= 0xc2 && c_2 < 0xe0) |
44 | { | |
45 | *puc = ((unsigned int) (c_2 & 0x1f) << 6) | |
46 | | (unsigned int) (c_1 ^ 0x80); | |
47 | return s - 2; | |
48 | } | |
24d56127 | 49 | #if CONFIG_UNICODE_SAFETY |
1cd4fffc | 50 | if ((c_2 ^ 0x80) < 0x40) |
24d56127 | 51 | #endif |
1cd4fffc LC |
52 | if (s - 2 != start) |
53 | { | |
54 | uint8_t c_3 = s[-3]; | |
24d56127 | 55 | |
1cd4fffc | 56 | if (c_3 >= 0xe0 && c_3 < 0xf0 |
24d56127 | 57 | #if CONFIG_UNICODE_SAFETY |
1cd4fffc LC |
58 | && (c_3 >= 0xe1 || c_2 >= 0xa0) |
59 | && (c_3 != 0xed || c_2 < 0xa0) | |
24d56127 | 60 | #endif |
1cd4fffc LC |
61 | ) |
62 | { | |
63 | *puc = ((unsigned int) (c_3 & 0x0f) << 12) | |
64 | | ((unsigned int) (c_2 ^ 0x80) << 6) | |
65 | | (unsigned int) (c_1 ^ 0x80); | |
66 | return s - 3; | |
67 | } | |
24d56127 | 68 | #if CONFIG_UNICODE_SAFETY |
1cd4fffc | 69 | if ((c_3 ^ 0x80) < 0x40) |
24d56127 | 70 | #endif |
1cd4fffc LC |
71 | if (s - 3 != start) |
72 | { | |
73 | uint8_t c_4 = s[-4]; | |
24d56127 | 74 | |
1cd4fffc | 75 | if (c_4 >= 0xf0 && c_4 < 0xf8 |
24d56127 | 76 | #if CONFIG_UNICODE_SAFETY |
1cd4fffc LC |
77 | && (c_4 >= 0xf1 || c_3 >= 0x90) |
78 | && (c_4 < 0xf4 || (c_4 == 0xf4 && c_3 < 0x90)) | |
24d56127 | 79 | #endif |
1cd4fffc LC |
80 | ) |
81 | { | |
82 | *puc = ((unsigned int) (c_4 & 0x07) << 18) | |
83 | | ((unsigned int) (c_3 ^ 0x80) << 12) | |
84 | | ((unsigned int) (c_2 ^ 0x80) << 6) | |
85 | | (unsigned int) (c_1 ^ 0x80); | |
86 | return s - 4; | |
87 | } | |
88 | } | |
89 | } | |
90 | } | |
24d56127 LC |
91 | } |
92 | return NULL; | |
93 | } |