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 5This program is free software; you can redistribute it and/or modify 6it under the terms of the GNU General Public License as published by 7the Free Software Foundation; either version 2, or (at your option) 8any later version. 9 10This program is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public License for more details. 14 15You should have received a copy of the GNU General Public License 16along with this program; if not, write to the Free Software 17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. */ 25static int 26u16_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 45static inline int 46u16_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} 56