_itoa.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Internal function for converting integers to ASCII.
  2. Copyright (C) 1994-2019 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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _ITOA_H
  16. #define _ITOA_H
  17. #include <limits.h>
  18. /* When long long is different from long, by default, _itoa_word is
  19. provided to convert long to ASCII and _itoa is provided to convert
  20. long long. A sysdeps _itoa.h can define _ITOA_NEEDED to 0 and define
  21. _ITOA_WORD_TYPE to unsigned long long int to override it so that
  22. _itoa_word is changed to convert long long to ASCII and _itoa is
  23. mapped to _itoa_word. */
  24. #ifndef _ITOA_NEEDED
  25. # define _ITOA_NEEDED (LONG_MAX != LLONG_MAX)
  26. #endif
  27. #ifndef _ITOA_WORD_TYPE
  28. # define _ITOA_WORD_TYPE unsigned long int
  29. #endif
  30. /* Convert VALUE into ASCII in base BASE (2..36).
  31. Write backwards starting the character just before BUFLIM.
  32. Return the address of the first (left-to-right) character in the number.
  33. Use upper case letters iff UPPER_CASE is nonzero. */
  34. extern char *_itoa (unsigned long long int value, char *buflim,
  35. unsigned int base, int upper_case) attribute_hidden;
  36. extern const char _itoa_upper_digits[];
  37. extern const char _itoa_lower_digits[];
  38. #if IS_IN (libc) || (IS_IN (rtld) && !defined NO_RTLD_HIDDEN)
  39. hidden_proto (_itoa_upper_digits)
  40. hidden_proto (_itoa_lower_digits)
  41. #endif
  42. #if IS_IN (libc)
  43. extern char *_itoa_word (_ITOA_WORD_TYPE value, char *buflim,
  44. unsigned int base,
  45. int upper_case) attribute_hidden;
  46. #else
  47. static inline char * __attribute__ ((unused, always_inline))
  48. _itoa_word (_ITOA_WORD_TYPE value, char *buflim,
  49. unsigned int base, int upper_case)
  50. {
  51. const char *digits = (upper_case
  52. ? _itoa_upper_digits
  53. : _itoa_lower_digits);
  54. switch (base)
  55. {
  56. # define SPECIAL(Base) \
  57. case Base: \
  58. do \
  59. *--buflim = digits[value % Base]; \
  60. while ((value /= Base) != 0); \
  61. break
  62. SPECIAL (10);
  63. SPECIAL (16);
  64. SPECIAL (8);
  65. default:
  66. do
  67. *--buflim = digits[value % base];
  68. while ((value /= base) != 0);
  69. }
  70. return buflim;
  71. }
  72. # undef SPECIAL
  73. #endif
  74. /* Similar to the _itoa functions, but output starts at buf and pointer
  75. after the last written character is returned. */
  76. extern char *_fitoa_word (_ITOA_WORD_TYPE value, char *buf,
  77. unsigned int base,
  78. int upper_case) attribute_hidden;
  79. extern char *_fitoa (unsigned long long value, char *buf, unsigned int base,
  80. int upper_case) attribute_hidden;
  81. #if !_ITOA_NEEDED
  82. /* No need for special long long versions. */
  83. # define _itoa(value, buf, base, upper_case) \
  84. _itoa_word (value, buf, base, upper_case)
  85. # define _fitoa(value, buf, base, upper_case) \
  86. _fitoa_word (value, buf, base, upper_case)
  87. #endif
  88. #endif /* itoa.h */