Blame lib/unistr/u8-uctomb.c

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