_itoa.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Internal function for converting integers to ASCII.
  2. Copyright (C) 1994-1999,2002,2003,2007 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 _ITOA_H
  17. #define _ITOA_H
  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 char _itoa_lower_digits[16] = "0123456789abcdef";
  23. static const char _itoa_upper_digits[16] = "0123456789ABCDEF";
  24. static inline char * __attribute__ ((unused, always_inline))
  25. _itoa_word (unsigned long value, char *buflim,
  26. unsigned int base, int upper_case)
  27. {
  28. const char *digits = (upper_case ? _itoa_upper_digits : _itoa_lower_digits);
  29. switch (base)
  30. {
  31. # define SPECIAL(Base) \
  32. case Base: \
  33. do \
  34. *--buflim = digits[value % Base]; \
  35. while ((value /= Base) != 0); \
  36. break
  37. SPECIAL (10);
  38. SPECIAL (16);
  39. SPECIAL (8);
  40. default:
  41. do
  42. *--buflim = digits[value % base];
  43. while ((value /= base) != 0);
  44. }
  45. return buflim;
  46. }
  47. static inline char * __attribute__ ((unused, always_inline))
  48. _itoa (uint64_t value, char *buflim,
  49. unsigned int base, int upper_case)
  50. {
  51. const char *digits = (upper_case ? _itoa_upper_digits : _itoa_lower_digits);
  52. switch (base)
  53. {
  54. SPECIAL (10);
  55. SPECIAL (16);
  56. SPECIAL (8);
  57. default:
  58. do
  59. *--buflim = digits[value % base];
  60. while ((value /= base) != 0);
  61. }
  62. return buflim;
  63. }
  64. # undef SPECIAL
  65. #endif /* itoa.h */