1 | /* Internal function for converting integers to ASCII. |
2 | Copyright (C) 1994-2024 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #ifndef _ITOA_H |
20 | #define _ITOA_H |
21 | |
22 | #include <limits.h> |
23 | |
24 | /* When long long is different from long, by default, _itoa_word is |
25 | provided to convert long to ASCII and _itoa is provided to convert |
26 | long long. A sysdeps _itoa.h can define _ITOA_NEEDED to 0 and define |
27 | _ITOA_WORD_TYPE to unsigned long long int to override it so that |
28 | _itoa_word is changed to convert long long to ASCII and _itoa is |
29 | mapped to _itoa_word. */ |
30 | |
31 | #ifndef _ITOA_NEEDED |
32 | # define _ITOA_NEEDED (LONG_MAX != LLONG_MAX) |
33 | #endif |
34 | #ifndef _ITOA_WORD_TYPE |
35 | # define _ITOA_WORD_TYPE unsigned long int |
36 | #endif |
37 | |
38 | |
39 | /* Convert VALUE into ASCII in base BASE (2..36). |
40 | Write backwards starting the character just before BUFLIM. |
41 | Return the address of the first (left-to-right) character in the number. |
42 | Use upper case letters iff UPPER_CASE is nonzero. */ |
43 | |
44 | extern char *_itoa (unsigned long long int value, char *buflim, |
45 | unsigned int base, int upper_case) attribute_hidden; |
46 | |
47 | extern const char _itoa_upper_digits[]; |
48 | extern const char _itoa_lower_digits[]; |
49 | #if IS_IN (libc) || IS_IN (rtld) |
50 | hidden_proto (_itoa_upper_digits) |
51 | hidden_proto (_itoa_lower_digits) |
52 | #endif |
53 | |
54 | #if IS_IN (libc) |
55 | extern char *_itoa_word (_ITOA_WORD_TYPE value, char *buflim, |
56 | unsigned int base, |
57 | int upper_case) attribute_hidden; |
58 | #else |
59 | static inline char * __attribute__ ((unused, always_inline)) |
60 | _itoa_word (_ITOA_WORD_TYPE value, char *buflim, |
61 | unsigned int base, int upper_case) |
62 | { |
63 | const char *digits = (upper_case |
64 | ? _itoa_upper_digits |
65 | : _itoa_lower_digits); |
66 | |
67 | switch (base) |
68 | { |
69 | # define SPECIAL(Base) \ |
70 | case Base: \ |
71 | do \ |
72 | *--buflim = digits[value % Base]; \ |
73 | while ((value /= Base) != 0); \ |
74 | break |
75 | |
76 | SPECIAL (10); |
77 | SPECIAL (16); |
78 | SPECIAL (8); |
79 | default: |
80 | do |
81 | *--buflim = digits[value % base]; |
82 | while ((value /= base) != 0); |
83 | } |
84 | return buflim; |
85 | } |
86 | # undef SPECIAL |
87 | #endif |
88 | |
89 | /* Similar to the _itoa functions, but output starts at buf and pointer |
90 | after the last written character is returned. */ |
91 | extern char *_fitoa_word (_ITOA_WORD_TYPE value, char *buf, |
92 | unsigned int base, |
93 | int upper_case) attribute_hidden; |
94 | extern char *_fitoa (unsigned long long value, char *buf, unsigned int base, |
95 | int upper_case) attribute_hidden; |
96 | |
97 | #if !_ITOA_NEEDED |
98 | /* No need for special long long versions. */ |
99 | # define _itoa(value, buf, base, upper_case) \ |
100 | _itoa_word (value, buf, base, upper_case) |
101 | # define _fitoa(value, buf, base, upper_case) \ |
102 | _fitoa_word (value, buf, base, upper_case) |
103 | #endif |
104 | |
105 | #endif /* itoa.h */ |
106 | |