diff options
author | Bruno Haible <bruno@clisp.org> | 2007-01-09 14:08:57 +0000 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2007-01-09 14:08:57 +0000 |
commit | c8fd23e82b6ebde9a5cecdbc4f0df849443b9c0e (patch) | |
tree | ce0d864b1faf3715115453acd793af7b47e75909 /lib/unistr/u16-mblen.c | |
parent | ce4bd2777d5180bcce40251e267f957d22f1b568 (diff) | |
download | gnulib-c8fd23e82b6ebde9a5cecdbc4f0df849443b9c0e.tar.gz |
New modules 'unistr/u8-mblen', 'unistr/u16-mblen', 'unistr/u32-mblen'.
Diffstat (limited to 'lib/unistr/u16-mblen.c')
-rw-r--r-- | lib/unistr/u16-mblen.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/unistr/u16-mblen.c b/lib/unistr/u16-mblen.c new file mode 100644 index 0000000000..a581bfc241 --- /dev/null +++ b/lib/unistr/u16-mblen.c @@ -0,0 +1,51 @@ +/* Look at first character in UTF-16 string. + Copyright (C) 1999-2000, 2002, 2006-2007 Free Software Foundation, Inc. + Written by Bruno Haible <bruno@clisp.org>, 2002. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published + by the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + USA. */ + +#include <config.h> + +/* Specification. */ +#include "unistr.h" + +int +u16_mblen (const uint16_t *s, size_t n) +{ + if (n > 0) + { + /* Keep in sync with utf16-ucs4.h and utf16-ucs4.c. */ + uint16_t c = *s; + + if (c < 0xd800 || c >= 0xe000) + return (c != 0 ? 1 : 0); +#if CONFIG_UNICODE_SAFETY + if (c < 0xdc00) + { + if (n >= 2 + && s[1] >= 0xdc00 && s[1] < 0xe000) + return 2; + } +#else + { + if (n >= 2) + return 2; + } +#endif + } + /* invalid or incomplete multibyte character */ + return -1; +} |