Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / ucs4-utf8.h
blob0757325c47a7f3f0a27b4e9b8a08bd95af7306a3
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)
8 any later version.
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. */
20 #include <stddef.h>
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. */
25 static int
26 u8_uctomb_aux (unsigned char *s, unsigned int uc, int n)
28 int count;
30 if (uc < 0x80)
31 count = 1;
32 else if (uc < 0x800)
33 count = 2;
34 else if (uc < 0x10000)
35 count = 3;
36 #if 0
37 else if (uc < 0x200000)
38 count = 4;
39 else if (uc < 0x4000000)
40 count = 5;
41 else if (uc <= 0x7fffffff)
42 count = 6;
43 #else
44 else if (uc < 0x110000)
45 count = 4;
46 #endif
47 else
48 return -1;
50 if (n < count)
51 return -2;
53 switch (count) /* note: code falls through cases! */
55 #if 0
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;
58 #endif
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;
62 case 1: s[0] = uc;
64 return count;
67 static inline int
68 u8_uctomb (unsigned char *s, unsigned int uc, int n)
70 if (uc < 0x80 && n > 0)
72 s[0] = uc;
73 return 1;
75 else
76 return u8_uctomb_aux (s, uc, n);