1 /* Conversion UCS-4 to UTF-8.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2002.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Return the length (number of units) of the UTF-8 representation of uc,
23 after storing it at S. Return -1 upon failure, -2 if the number of
24 available units, N, is too small. */
26 u8_uctomb_aux (unsigned char *s
, unsigned int uc
, int n
)
34 else if (uc
< 0x10000)
37 else if (uc
< 0x200000)
39 else if (uc
< 0x4000000)
41 else if (uc
<= 0x7fffffff)
44 else if (uc
< 0x110000)
53 switch (count
) /* note: code falls through cases! */
56 case 6: s
[5] = 0x80 | (uc
& 0x3f); uc
= uc
>> 6; uc
|= 0x4000000;
57 case 5: s
[4] = 0x80 | (uc
& 0x3f); uc
= uc
>> 6; uc
|= 0x200000;
59 case 4: s
[3] = 0x80 | (uc
& 0x3f); uc
= uc
>> 6; uc
|= 0x10000;
60 case 3: s
[2] = 0x80 | (uc
& 0x3f); uc
= uc
>> 6; uc
|= 0x800;
61 case 2: s
[1] = 0x80 | (uc
& 0x3f); uc
= uc
>> 6; uc
|= 0xc0;
68 u8_uctomb (unsigned char *s
, unsigned int uc
, int n
)
70 if (uc
< 0x80 && n
> 0)
76 return u8_uctomb_aux (s
, uc
, n
);