Use Gnulib's `getaddrinfo' module.
[bpt/guile.git] / lib / unistr / u8-prev.c
1 /* Iterate over previous character in UTF-8 string.
2 Copyright (C) 2002, 2006-2007, 2009-2010 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)
32 {
33 *puc = c_1;
34 return s - 1;
35 }
36 #if CONFIG_UNICODE_SAFETY
37 if ((c_1 ^ 0x80) < 0x40)
38 #endif
39 if (s - 1 != start)
40 {
41 uint8_t c_2 = s[-2];
42
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 }
49 #if CONFIG_UNICODE_SAFETY
50 if ((c_2 ^ 0x80) < 0x40)
51 #endif
52 if (s - 2 != start)
53 {
54 uint8_t c_3 = s[-3];
55
56 if (c_3 >= 0xe0 && c_3 < 0xf0
57 #if CONFIG_UNICODE_SAFETY
58 && (c_3 >= 0xe1 || c_2 >= 0xa0)
59 && (c_3 != 0xed || c_2 < 0xa0)
60 #endif
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 }
68 #if CONFIG_UNICODE_SAFETY
69 if ((c_3 ^ 0x80) < 0x40)
70 #endif
71 if (s - 3 != start)
72 {
73 uint8_t c_4 = s[-4];
74
75 if (c_4 >= 0xf0 && c_4 < 0xf8
76 #if CONFIG_UNICODE_SAFETY
77 && (c_4 >= 0xf1 || c_3 >= 0x90)
78 && (c_4 < 0xf4 || (c_4 == 0xf4 && c_3 < 0x90))
79 #endif
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 }
91 }
92 return NULL;
93 }