Blame lib/unistring/unistr/u16-mbtoucr.c

Packit aea12f
/* Look at first character in UTF-16 string, returning an error code.
Packit Service 991b93
   Copyright (C) 1999-2002, 2006-2007, 2009-2020 Free Software Foundation, Inc.
Packit aea12f
   Written by Bruno Haible <bruno@clisp.org>, 2001.
Packit aea12f
Packit aea12f
   This program is free software: you can redistribute it and/or
Packit aea12f
   modify it under the terms of either:
Packit aea12f
Packit aea12f
     * the GNU Lesser General Public License as published by the Free
Packit aea12f
       Software Foundation; either version 3 of the License, or (at your
Packit aea12f
       option) any later version.
Packit aea12f
Packit aea12f
   or
Packit aea12f
Packit aea12f
     * the GNU General Public License as published by the Free
Packit aea12f
       Software Foundation; either version 2 of the License, or (at your
Packit aea12f
       option) any later version.
Packit aea12f
Packit aea12f
   or both in parallel, as here.
Packit aea12f
   This program is distributed in the hope that it will be useful,
Packit aea12f
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
   Lesser General Public License for more details.
Packit aea12f
Packit aea12f
   You should have received a copy of the GNU Lesser General Public License
Packit aea12f
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit aea12f
Packit aea12f
#include <config.h>
Packit aea12f
Packit aea12f
/* Specification.  */
Packit aea12f
#include "unistr.h"
Packit aea12f
Packit aea12f
int
Packit aea12f
u16_mbtoucr (ucs4_t *puc, const uint16_t *s, size_t n)
Packit aea12f
{
Packit aea12f
  uint16_t c = *s;
Packit aea12f
Packit aea12f
  if (c < 0xd800 || c >= 0xe000)
Packit aea12f
    {
Packit aea12f
      *puc = c;
Packit aea12f
      return 1;
Packit aea12f
    }
Packit aea12f
  if (c < 0xdc00)
Packit aea12f
    {
Packit aea12f
      if (n >= 2)
Packit aea12f
        {
Packit aea12f
          if (s[1] >= 0xdc00 && s[1] < 0xe000)
Packit aea12f
            {
Packit aea12f
              *puc = 0x10000 + ((c - 0xd800) << 10) + (s[1] - 0xdc00);
Packit aea12f
              return 2;
Packit aea12f
            }
Packit aea12f
          /* invalid multibyte character */
Packit aea12f
        }
Packit aea12f
      else
Packit aea12f
        {
Packit aea12f
          /* incomplete multibyte character */
Packit aea12f
          *puc = 0xfffd;
Packit aea12f
          return -2;
Packit aea12f
        }
Packit aea12f
    }
Packit aea12f
  /* invalid multibyte character */
Packit aea12f
  *puc = 0xfffd;
Packit aea12f
  return -1;
Packit aea12f
}