xref: /haiku/src/system/libroot/posix/glibc/stdio-common/_itoa.h (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /* Internal function for converting integers to ASCII.
2    Copyright (C) 1994, 95, 96, 97, 98, 99, 2002 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19 
20 #ifndef _ITOA_H
21 #define _ITOA_H
22 #include <sys/cdefs.h>
23 
24 /* Convert VALUE into ASCII in base BASE (2..36).
25    Write backwards starting the character just before BUFLIM.
26    Return the address of the first (left-to-right) character in the number.
27    Use upper case letters iff UPPER_CASE is nonzero.  */
28 
29 extern char *_itoa (unsigned long long int value, char *buflim,
30 		    unsigned int base, int upper_case);
31 
32 extern const char _itoa_upper_digits[];
33 extern const char _itoa_upper_digits_internal[] attribute_hidden;
34 extern const char _itoa_lower_digits[];
35 extern const char _itoa_lower_digits_internal[] attribute_hidden;
36 
37 static inline char * __attribute__ ((unused))
38 _itoa_word (unsigned long value, char *buflim,
39 	    unsigned int base, int upper_case)
40 {
41   const char *digits = (upper_case
42 #if !defined NOT_IN_libc || defined IS_IN_rtld
43 			? INTUSE(_itoa_upper_digits)
44 			: INTUSE(_itoa_lower_digits)
45 #else
46 			? _itoa_upper_digits
47 			: _itoa_lower_digits
48 #endif
49 		       );
50 
51   switch (base)
52     {
53 #define SPECIAL(Base)							      \
54     case Base:								      \
55       do								      \
56 	*--buflim = digits[value % Base];				      \
57       while ((value /= Base) != 0);					      \
58       break
59 
60       SPECIAL (10);
61       SPECIAL (16);
62       SPECIAL (8);
63     default:
64       do
65 	*--buflim = digits[value % base];
66       while ((value /= base) != 0);
67     }
68   return buflim;
69 }
70 #undef SPECIAL
71 
72 static inline char * __attribute__ ((unused))
73 _fitoa_word (unsigned long value, char *buf, unsigned int base, int upper_case)
74 {
75   char tmpbuf[sizeof (value) * 4];		/* Worst case length: base 2.  */
76   char *cp = _itoa_word (value, tmpbuf + sizeof (value) * 4, base, upper_case);
77   while (cp < tmpbuf + sizeof (value) * 4)
78     *buf++ = *cp++;
79   return buf;
80 }
81 
82 static inline char * __attribute__ ((unused))
83 _fitoa (unsigned long long value, char *buf, unsigned int base, int upper_case)
84 {
85   char tmpbuf[sizeof (value) * 4];		/* Worst case length: base 2.  */
86   char *cp = _itoa (value, tmpbuf + sizeof (value) * 4, base, upper_case);
87   while (cp < tmpbuf + sizeof (value) * 4)
88     *buf++ = *cp++;
89   return buf;
90 }
91 
92 #endif	/* itoa.h */
93