_itowa.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Internal function for converting integers to ASCII.
  2. Copyright (C) 1994,95,96,97,98,99,2002,2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _ITOWA_H
  17. #define _ITOWA_H 1
  18. /* Convert VALUE into ASCII in base BASE (2..16).
  19. Write backwards starting the character just before BUFLIM.
  20. Return the address of the first (left-to-right) character in the number.
  21. Use upper case letters iff UPPER_CASE is nonzero. */
  22. static const wchar_t _itowa_lower_digits[16] = L_("0123456789abcdef");
  23. static const wchar_t _itowa_upper_digits[16] = L_("0123456789ABCDEF");
  24. static inline wchar_t *
  25. __attribute__ ((unused, always_inline))
  26. _itowa_word (unsigned long value, wchar_t *buflim,
  27. unsigned int base, int upper_case)
  28. {
  29. const wchar_t *digits = (upper_case
  30. ? _itowa_upper_digits : _itowa_lower_digits);
  31. wchar_t *bp = buflim;
  32. switch (base)
  33. {
  34. #define SPECIAL(Base) \
  35. case Base: \
  36. do \
  37. *--bp = digits[value % Base]; \
  38. while ((value /= Base) != 0); \
  39. break
  40. SPECIAL (10);
  41. SPECIAL (16);
  42. SPECIAL (8);
  43. default:
  44. do
  45. *--bp = digits[value % base];
  46. while ((value /= base) != 0);
  47. }
  48. return bp;
  49. }
  50. static inline wchar_t *
  51. __attribute__ ((unused, always_inline))
  52. _itowa (uint64_t value, wchar_t *buflim,
  53. unsigned int base, int upper_case)
  54. {
  55. const wchar_t *digits = (upper_case
  56. ? _itowa_upper_digits : _itowa_lower_digits);
  57. wchar_t *bp = buflim;
  58. switch (base)
  59. {
  60. SPECIAL (10);
  61. SPECIAL (16);
  62. SPECIAL (8);
  63. default:
  64. do
  65. *--bp = digits[value % base];
  66. while ((value /= base) != 0);
  67. }
  68. return bp;
  69. }
  70. #undef SPECIAL
  71. #endif /* itowa.h */