52242c5892b43b05e3e4b4be3af071cb6e7ebc2b
[platform/upstream/libunistring.git] / lib / unistr / u8-strmblen.c
1 /* Look at first character in UTF-8 string.
2    Copyright (C) 1999-2000, 2002, 2006-2007, 2009-2010 Free Software
3    Foundation, Inc.
4    Written by Bruno Haible <bruno@clisp.org>, 2002.
5
6    This program is free software: you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as published
8    by the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "unistr.h"
23
24 int
25 u8_strmblen (const uint8_t *s)
26 {
27   /* Keep in sync with unistr.h and utf8-ucs4.c.  */
28   uint8_t c = *s;
29
30   if (c < 0x80)
31     return (c != 0 ? 1 : 0);
32   if (c >= 0xc2)
33     {
34       if (c < 0xe0)
35         {
36 #if CONFIG_UNICODE_SAFETY
37           if ((s[1] ^ 0x80) < 0x40)
38 #else
39           if (s[1] != 0)
40 #endif
41             return 2;
42         }
43       else if (c < 0xf0)
44         {
45 #if CONFIG_UNICODE_SAFETY
46           if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
47               && (c >= 0xe1 || s[1] >= 0xa0)
48               && (c != 0xed || s[1] < 0xa0))
49 #else
50           if (s[1] != 0 && s[2] != 0)
51 #endif
52             return 3;
53         }
54       else if (c < 0xf8)
55         {
56 #if CONFIG_UNICODE_SAFETY
57           if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
58               && (s[3] ^ 0x80) < 0x40
59               && (c >= 0xf1 || s[1] >= 0x90)
60 #if 1
61               && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
62 #endif
63              )
64 #else
65           if (s[1] != 0 && s[2] != 0 && s[3] != 0)
66 #endif
67             return 4;
68         }
69 #if 0
70       else if (c < 0xfc)
71         {
72 #if CONFIG_UNICODE_SAFETY
73           if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
74               && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
75               && (c >= 0xf9 || s[1] >= 0x88))
76 #else
77           if (s[1] != 0 && s[2] != 0 && s[3] != 0 && s[4] != 0)
78 #endif
79             return 5;
80         }
81       else if (c < 0xfe)
82         {
83 #if CONFIG_UNICODE_SAFETY
84           if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
85               && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
86               && (s[5] ^ 0x80) < 0x40
87               && (c >= 0xfd || s[1] >= 0x84))
88 #else
89           if (s[1] != 0 && s[2] != 0 && s[3] != 0 && s[4] != 0 && s[5] != 0)
90 #endif
91             return 6;
92         }
93 #endif
94     }
95   /* invalid or incomplete multibyte character */
96   return -1;
97 }