Upgrade grep version 2.7 to 2.9 on the vendor branch
[dragonfly.git] / contrib / grep / lib / unistr / u8-uctomb.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Store a character in UTF-8 string.
4    Copyright (C) 2002, 2005-2006, 2009-2011 Free Software Foundation, Inc.
5    Written by Bruno Haible <bruno@clisp.org>, 2002.
6
7    This program is free software: you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published
9    by the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include <config.h>
21
22 #if defined IN_LIBUNISTRING
23 /* Tell unistr.h to declare u8_uctomb as 'extern', not 'static inline'.  */
24 # include "unistring-notinline.h"
25 #endif
26
27 /* Specification.  */
28 #include "unistr.h"
29
30 #if !HAVE_INLINE
31
32 int
33 u8_uctomb (uint8_t *s, ucs4_t uc, int n)
34 {
35   if (uc < 0x80)
36     {
37       if (n > 0)
38         {
39           s[0] = uc;
40           return 1;
41         }
42       /* else return -2, below.  */
43     }
44   else
45     {
46       int count;
47
48       if (uc < 0x800)
49         count = 2;
50       else if (uc < 0x10000)
51         {
52           if (uc < 0xd800 || uc >= 0xe000)
53             count = 3;
54           else
55             return -1;
56         }
57 #if 0
58       else if (uc < 0x200000)
59         count = 4;
60       else if (uc < 0x4000000)
61         count = 5;
62       else if (uc <= 0x7fffffff)
63         count = 6;
64 #else
65       else if (uc < 0x110000)
66         count = 4;
67 #endif
68       else
69         return -1;
70
71       if (n >= count)
72         {
73           switch (count) /* note: code falls through cases! */
74             {
75 #if 0
76             case 6: s[5] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x4000000;
77             case 5: s[4] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x200000;
78 #endif
79             case 4: s[3] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x10000;
80             case 3: s[2] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x800;
81             case 2: s[1] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0xc0;
82           /*case 1:*/ s[0] = uc;
83             }
84           return count;
85         }
86     }
87   return -2;
88 }
89
90 #endif