u8-uctomb.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Store a character in UTF-8 string.
  2. Copyright (C) 2002, 2005-2006, 2009-2012 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2002.
  4. This program is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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 GNU
  11. 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 <http://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. # include "unistring-notinline.h"
  18. #endif
  19. /* Specification. */
  20. #include "unistr.h"
  21. #if !HAVE_INLINE
  22. int
  23. u8_uctomb (uint8_t *s, ucs4_t uc, int n)
  24. {
  25. if (uc < 0x80)
  26. {
  27. if (n > 0)
  28. {
  29. s[0] = uc;
  30. return 1;
  31. }
  32. /* else return -2, below. */
  33. }
  34. else
  35. {
  36. int count;
  37. if (uc < 0x800)
  38. count = 2;
  39. else if (uc < 0x10000)
  40. {
  41. if (uc < 0xd800 || uc >= 0xe000)
  42. count = 3;
  43. else
  44. return -1;
  45. }
  46. #if 0
  47. else if (uc < 0x200000)
  48. count = 4;
  49. else if (uc < 0x4000000)
  50. count = 5;
  51. else if (uc <= 0x7fffffff)
  52. count = 6;
  53. #else
  54. else if (uc < 0x110000)
  55. count = 4;
  56. #endif
  57. else
  58. return -1;
  59. if (n >= count)
  60. {
  61. switch (count) /* note: code falls through cases! */
  62. {
  63. #if 0
  64. case 6: s[5] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x4000000;
  65. case 5: s[4] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x200000;
  66. #endif
  67. case 4: s[3] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x10000;
  68. case 3: s[2] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x800;
  69. case 2: s[1] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0xc0;
  70. /*case 1:*/ s[0] = uc;
  71. }
  72. return count;
  73. }
  74. }
  75. return -2;
  76. }
  77. #endif