1
/* Copyright (c) 2000, 2001, 2004, 2006, 2007 MySQL AB, 2009 Sun Microsystems, Inc.
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 */
18
Defines: longlong2str();
20
longlong2str(dst, radix, val)
21
converts the (longlong) integer "val" to character form and moves it to
22
the destination string "dst" followed by a terminating NUL. The
23
result is normally a pointer to this NUL character, but if the radix
24
is dud the result will be NullS and nothing will be changed.
26
If radix is -2..-36, val is taken to be SIGNED.
27
If radix is 2.. 36, val is taken to be UNSIGNED.
28
That is, val is signed if and only if radix is. You will normally
29
use radix -10 only through itoa and ltoa, for radix 2, 8, or 16
30
unsigned is what you generally want.
32
_dig_vec is public just in case someone has a use for it.
33
The definitions of itoa and ltoa are actually macros in m_string.h,
34
but this is where the code is.
36
Note: The standard itoa() returns a pointer to the argument, when int2str
37
returns the pointer to the end-null.
38
itoa assumes that 10 -base numbers are allways signed and other arn't.
41
#include <my_global.h>
47
This assumes that longlong multiplication is faster than longlong division.
50
char *ll2str(longlong val,char *dst,int radix, int upcase)
55
char *dig_vec= upcase ? _dig_vec_upper : _dig_vec_lower;
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);
88
long_val= (long) uval;
91
long quo= long_val/radix;
92
*--p= dig_vec[(uchar) (long_val - quo*radix)];
95
while ((*dst++ = *p++) != 0) ;
100
#ifndef longlong10_to_str
101
char *longlong10_to_str(longlong val,char *dst,int radix)
106
ulonglong uval= (ulonglong) val;
113
/* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
114
uval = (ulonglong)0 - uval;
124
p = &buffer[sizeof(buffer)-1];
127
while (uval > (ulonglong) LONG_MAX)
129
ulonglong quo= uval/(uint) 10;
130
uint rem= (uint) (uval- quo* (uint) 10);
131
*--p = _dig_vec_upper[rem];
134
long_val= (long) uval;
135
while (long_val != 0)
137
long quo= long_val/10;
138
*--p = _dig_vec_upper[(uchar) (long_val - quo*10)];
141
while ((*dst++ = *p++) != 0) ;