Blame lib/unistring/unistr/u8-uctomb.c

Packit aea12f
/* Store a character in UTF-8 string.
Packit Service 991b93
   Copyright (C) 2002, 2005-2006, 2009-2020 Free Software Foundation, Inc.
Packit aea12f
   Written by Bruno Haible <bruno@clisp.org>, 2002.
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
#if defined IN_LIBUNISTRING
Packit aea12f
/* Tell unistr.h to declare u8_uctomb as 'extern', not 'static inline'.  */
Packit aea12f
# include "unistring-notinline.h"
Packit aea12f
#endif
Packit aea12f
Packit aea12f
/* Specification.  */
Packit aea12f
#include "unistr.h"
Packit aea12f
Packit Service 991b93
#include "attribute.h"
Packit Service 991b93
Packit aea12f
#if !HAVE_INLINE
Packit aea12f
Packit aea12f
int
Packit aea12f
u8_uctomb (uint8_t *s, ucs4_t uc, int n)
Packit aea12f
{
Packit aea12f
  if (uc < 0x80)
Packit aea12f
    {
Packit aea12f
      if (n > 0)
Packit aea12f
        {
Packit aea12f
          s[0] = uc;
Packit aea12f
          return 1;
Packit aea12f
        }
Packit aea12f
      /* else return -2, below.  */
Packit aea12f
    }
Packit aea12f
  else
Packit aea12f
    {
Packit aea12f
      int count;
Packit aea12f
Packit aea12f
      if (uc < 0x800)
Packit aea12f
        count = 2;
Packit aea12f
      else if (uc < 0x10000)
Packit aea12f
        {
Packit aea12f
          if (uc < 0xd800 || uc >= 0xe000)
Packit aea12f
            count = 3;
Packit aea12f
          else
Packit aea12f
            return -1;
Packit aea12f
        }
Packit aea12f
      else if (uc < 0x110000)
Packit aea12f
        count = 4;
Packit aea12f
      else
Packit aea12f
        return -1;
Packit aea12f
Packit aea12f
      if (n >= count)
Packit aea12f
        {
Packit aea12f
          switch (count) /* note: code falls through cases! */
Packit aea12f
            {
Packit aea12f
            case 4: s[3] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x10000;
Packit Service 991b93
              FALLTHROUGH;
Packit aea12f
            case 3: s[2] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x800;
Packit Service 991b93
              FALLTHROUGH;
Packit aea12f
            case 2: s[1] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0xc0;
Packit aea12f
          /*case 1:*/ s[0] = uc;
Packit aea12f
            }
Packit aea12f
          return count;
Packit aea12f
        }
Packit aea12f
    }
Packit aea12f
  return -2;
Packit aea12f
}
Packit aea12f
Packit aea12f
#endif