1 /* Look at first character in UTF-16 string.
2 Copyright (C) 1999-2000, 2002, 2006-2007, 2009-2021 Free Software
3 Foundation, Inc.
4 Written by Bruno Haible <bruno@clisp.org>, 2002.
5
6 This file is free software.
7 It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
8 You can redistribute it and/or modify it under either
9 - the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 3, or (at your
11 option) any later version, or
12 - the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2, or (at your option)
14 any later version, or
15 - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
16
17 This file is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License and the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License and of the GNU General Public License along with this
25 program. If not, see <https://www.gnu.org/licenses/>. */
26
27 #include <config.h>
28
29 /* Specification. */
30 #include "unistr.h"
31
32 int
33 u16_strmbtouc (ucs4_t *puc, const uint16_t *s)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
34 {
35 /* Keep in sync with unistr.h and u16-mbtouc-aux.c. */
36 uint16_t c = *s;
37
38 if (c < 0xd800 || c >= 0xe000)
39 {
40 *puc = c;
41 return (c != 0 ? 1 : 0);
42 }
43 if (c < 0xdc00)
44 {
45 if (s[1] >= 0xdc00 && s[1] < 0xe000)
46 {
47 *puc = 0x10000 + ((c - 0xd800) << 10) + (s[1] - 0xdc00);
48 return 2;
49 }
50 }
51 /* invalid or incomplete multibyte character */
52 return -1;
53 }