u8-uctomb.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Store a character in UTF-8 string.
  2. Copyright (C) 2002, 2005-2006, 2009-2024 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2002.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #if defined IN_LIBUNISTRING
  16. /* Tell unistr.h to declare u8_uctomb as 'extern', not 'static inline'. */
  17. # error #include "unistring-notinline.h"
  18. #endif
  19. /* Specification. */
  20. #include "unistr.h"
  21. #include "attribute.h"
  22. #if !HAVE_INLINE
  23. int
  24. u8_uctomb (uint8_t *s, ucs4_t uc, ptrdiff_t n)
  25. {
  26. if (uc < 0x80)
  27. {
  28. if (n > 0)
  29. {
  30. s[0] = uc;
  31. return 1;
  32. }
  33. /* else return -2, below. */
  34. }
  35. else
  36. {
  37. int count;
  38. if (uc < 0x800)
  39. count = 2;
  40. else if (uc < 0x10000)
  41. {
  42. if (uc < 0xd800 || uc >= 0xe000)
  43. count = 3;
  44. else
  45. return -1;
  46. }
  47. else if (uc < 0x110000)
  48. count = 4;
  49. else
  50. return -1;
  51. if (n >= count)
  52. {
  53. switch (count) /* note: code falls through cases! */
  54. {
  55. case 4: s[3] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x10000;
  56. FALLTHROUGH;
  57. case 3: s[2] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x800;
  58. FALLTHROUGH;
  59. case 2: s[1] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0xc0;
  60. /*case 1:*/ s[0] = uc;
  61. }
  62. return count;
  63. }
  64. }
  65. return -2;
  66. }
  67. #endif