New modules 'unistr/u8-mblen', 'unistr/u16-mblen', 'unistr/u32-mblen'.
[gnulib.git] / lib / unistr / u8-mblen.c
1 /* Look at first character in UTF-8 string.
2    Copyright (C) 1999-2000, 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 Library General Public License as published
7    by the Free Software Foundation; either version 2, or (at your option)
8    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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18    USA.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include "unistr.h"
24
25 int
26 u8_mblen (const uint8_t *s, size_t n)
27 {
28   if (n > 0)
29     {
30       /* Keep in sync with utf8-ucs4.h and utf8-ucs4.c.  */
31       uint8_t c = *s;
32
33       if (c < 0x80)
34         return (c != 0 ? 1 : 0);
35       if (c >= 0xc2)
36         {
37           if (c < 0xe0)
38             {
39               if (n >= 2
40 #if CONFIG_UNICODE_SAFETY
41                   && (s[1] ^ 0x80) < 0x40
42 #endif
43                  )
44                 return 2;
45             }
46           else if (c < 0xf0)
47             {
48               if (n >= 3
49 #if CONFIG_UNICODE_SAFETY
50                   && (s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
51                   && (c >= 0xe1 || s[1] >= 0xa0)
52                   && (c != 0xed || s[1] < 0xa0)
53 #endif
54                  )
55                 return 3;
56             }
57           else if (c < 0xf8)
58             {
59               if (n >= 4
60 #if CONFIG_UNICODE_SAFETY
61                   && (s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
62                   && (s[3] ^ 0x80) < 0x40
63                   && (c >= 0xf1 || s[1] >= 0x90)
64 #if 1
65                   && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
66 #endif
67 #endif
68                  )
69                 return 4;
70             }
71 #if 0
72           else if (c < 0xfc)
73             {
74               if (n >= 5
75 #if CONFIG_UNICODE_SAFETY
76                   && (s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
77                   && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
78                   && (c >= 0xf9 || s[1] >= 0x88)
79 #endif
80                  )
81                 return 5;
82             }
83           else if (c < 0xfe)
84             {
85               if (n >= 6
86 #if CONFIG_UNICODE_SAFETY
87                   && (s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
88                   && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
89                   && (s[5] ^ 0x80) < 0x40
90                   && (c >= 0xfd || s[1] >= 0x84)
91 #endif
92                  )
93                 return 6;
94             }
95 #endif
96         }
97     }
98   /* invalid or incomplete multibyte character */
99   return -1;
100 }