Blame lib/unistr/u8-uctomb.c

Packit Service fdd496
/* Store a character in UTF-8 string.
Packit Service fdd496
   Copyright (C) 2002, 2005-2006, 2009-2017 Free Software Foundation, Inc.
Packit Service fdd496
   Written by Bruno Haible <bruno@clisp.org>, 2002.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify it
Packit Service fdd496
   under the terms of the GNU General Public License as published
Packit Service fdd496
   by the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service fdd496
   General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
#if defined IN_LIBUNISTRING
Packit Service fdd496
/* Tell unistr.h to declare u8_uctomb as 'extern', not 'static inline'.  */
Packit Service fdd496
# include "unistring-notinline.h"
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* Specification.  */
Packit Service fdd496
#include "unistr.h"
Packit Service fdd496
Packit Service fdd496
#if !HAVE_INLINE
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
u8_uctomb (uint8_t *s, ucs4_t uc, int n)
Packit Service fdd496
{
Packit Service fdd496
  if (uc < 0x80)
Packit Service fdd496
    {
Packit Service fdd496
      if (n > 0)
Packit Service fdd496
        {
Packit Service fdd496
          s[0] = uc;
Packit Service fdd496
          return 1;
Packit Service fdd496
        }
Packit Service fdd496
      /* else return -2, below.  */
Packit Service fdd496
    }
Packit Service fdd496
  else
Packit Service fdd496
    {
Packit Service fdd496
      int count;
Packit Service fdd496
Packit Service fdd496
      if (uc < 0x800)
Packit Service fdd496
        count = 2;
Packit Service fdd496
      else if (uc < 0x10000)
Packit Service fdd496
        {
Packit Service fdd496
          if (uc < 0xd800 || uc >= 0xe000)
Packit Service fdd496
            count = 3;
Packit Service fdd496
          else
Packit Service fdd496
            return -1;
Packit Service fdd496
        }
Packit Service fdd496
      else if (uc < 0x110000)
Packit Service fdd496
        count = 4;
Packit Service fdd496
      else
Packit Service fdd496
        return -1;
Packit Service fdd496
Packit Service fdd496
      if (n >= count)
Packit Service fdd496
        {
Packit Service fdd496
          switch (count) /* note: code falls through cases! */
Packit Service fdd496
            {
Packit Service fdd496
            case 4: s[3] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x10000;
Packit Service fdd496
            case 3: s[2] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x800;
Packit Service fdd496
            case 2: s[1] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0xc0;
Packit Service fdd496
          /*case 1:*/ s[0] = uc;
Packit Service fdd496
            }
Packit Service fdd496
          return count;
Packit Service fdd496
        }
Packit Service fdd496
    }
Packit Service fdd496
  return -2;
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
#endif