* MODULES.html.sh (File system functions): New module
[gnulib.git] / lib / ucs4-utf16.h
1 /* Conversion UCS-4 to UTF-16.
2    Copyright (C) 2002 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2002.
4
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.
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
13 GNU General Public License for more details.
14
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 Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19
20 #include <stddef.h>
21
22 /* Return the length (number of units) of the UTF-16 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 u16_uctomb_aux (unsigned short *s, unsigned int uc, int n)
27 {
28   if (uc >= 0x10000)
29     {
30       if (uc < 0x110000)
31         {
32           if (n >= 2)
33             {
34               s[0] = 0xd800 + ((uc - 0x10000) >> 10);
35               s[1] = 0xdc00 + ((uc - 0x10000) & 0x3ff);
36               return 2;
37             }
38         }
39       else
40         return -1;
41     }
42   return -2;
43 }
44
45 static inline int
46 u16_uctomb (unsigned short *s, unsigned int uc, int n)
47 {
48   if (uc < 0x10000 && n > 0)
49     {
50       s[0] = uc;
51       return 1;
52     }
53   else
54     return u16_uctomb_aux (s, uc, n);
55 }