1 /* Copyright (c) 2000, 2001, 2004, 2007 MySQL AB
2 Use is subject to license terms.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program 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
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 Defines: longlong2str();
21 longlong2str(dst, radix, val)
22 converts the (longlong) integer "val" to character form and moves it to
23 the destination string "dst" followed by a terminating NUL. The
24 result is normally a pointer to this NUL character, but if the radix
25 is dud the result will be NullS and nothing will be changed.
27 If radix is -2..-36, val is taken to be SIGNED.
28 If radix is 2.. 36, val is taken to be UNSIGNED.
29 That is, val is signed if and only if radix is. You will normally
30 use radix -10 only through itoa and ltoa, for radix 2, 8, or 16
31 unsigned is what you generally want.
33 _dig_vec is public just in case someone has a use for it.
34 The definitions of itoa and ltoa are actually macros in m_string.h,
35 but this is where the code is.
37 Note: The standard itoa() returns a pointer to the argument, when int2str
38 returns the pointer to the end-null.
39 itoa assumes that 10 -base numbers are allways signed and other arn't.
42 #include <my_global.h>
45 #if defined(HAVE_LONG_LONG) && !defined(longlong2str) && !defined(HAVE_LONGLONG2STR)
48 This assumes that longlong multiplication is faster than longlong division.
51 char *longlong2str(longlong val
,char *dst
,int radix
)
56 ulonglong uval
= (ulonglong
) val
;
60 if (radix
< -36 || radix
> -2) return (char*) 0;
63 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
64 uval
= (ulonglong
)0 - uval
;
70 if (radix
> 36 || radix
< 2) return (char*) 0;
78 p
= &buffer
[sizeof(buffer
)-1];
81 while (uval
> (ulonglong
) LONG_MAX
)
83 ulonglong quo
= uval
/(uint
) radix
;
84 uint rem
= (uint
) (uval
- quo
* (uint
) radix
);
85 *--p
= _dig_vec_upper
[rem
];
88 long_val
= (long) uval
;
91 long quo
= long_val
/radix
;
92 *--p
= _dig_vec_upper
[(uchar
) (long_val
- quo
*radix
)];
95 while ((*dst
++ = *p
++) != 0) ;
101 #ifndef longlong10_to_str
102 char *longlong10_to_str(longlong val
,char *dst
,int radix
)
107 ulonglong uval
= (ulonglong
) val
;
114 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
115 uval
= (ulonglong
)0 - uval
;
125 p
= &buffer
[sizeof(buffer
)-1];
128 while (uval
> (ulonglong
) LONG_MAX
)
130 ulonglong quo
= uval
/(uint
) 10;
131 uint rem
= (uint
) (uval
- quo
* (uint
) 10);
132 *--p
= _dig_vec_upper
[rem
];
135 long_val
= (long) uval
;
136 while (long_val
!= 0)
138 long quo
= long_val
/10;
139 *--p
= _dig_vec_upper
[(uchar
) (long_val
- quo
*10)];
142 while ((*dst
++ = *p
++) != 0) ;