1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
Defines: longlong2str();
19
longlong2str(dst, radix, val)
20
converts the (longlong) integer "val" to character form and moves it to
21
the destination string "dst" followed by a terminating NUL. The
22
result is normally a pointer to this NUL character, but if the radix
23
is dud the result will be NullS and nothing will be changed.
25
If radix is -2..-36, val is taken to be SIGNED.
26
If radix is 2.. 36, val is taken to be UNSIGNED.
27
That is, val is signed if and only if radix is. You will normally
28
use radix -10 only through itoa and ltoa, for radix 2, 8, or 16
29
unsigned is what you generally want.
31
_dig_vec is public just in case someone has a use for it.
32
The definitions of itoa and ltoa are actually macros in m_string.h,
33
but this is where the code is.
35
Note: The standard itoa() returns a pointer to the argument, when int2str
36
returns the pointer to the end-null.
37
itoa assumes that 10 -base numbers are allways signed and other arn't.
40
#include <my_global.h>
43
#if defined(HAVE_LONG_LONG) && !defined(longlong2str) && !defined(HAVE_LONGLONG2STR)
46
This assumes that longlong multiplication is faster than longlong division.
49
char *longlong2str(longlong val,char *dst,int radix)
54
ulonglong uval= (ulonglong) val;
58
if (radix < -36 || radix > -2) return (char*) 0;
61
/* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
62
uval = (ulonglong)0 - uval;
68
if (radix > 36 || radix < 2) return (char*) 0;
76
p = &buffer[sizeof(buffer)-1];
79
while (uval > (ulonglong) LONG_MAX)
81
ulonglong quo= uval/(uint) radix;
82
uint rem= (uint) (uval- quo* (uint) radix);
83
*--p = _dig_vec_upper[rem];
86
long_val= (long) uval;
89
long quo= long_val/radix;
90
*--p = _dig_vec_upper[(uchar) (long_val - quo*radix)];
93
while ((*dst++ = *p++) != 0) ;
99
#ifndef longlong10_to_str
100
char *longlong10_to_str(longlong val,char *dst,int radix)
105
ulonglong uval= (ulonglong) val;
112
/* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
113
uval = (ulonglong)0 - uval;
123
p = &buffer[sizeof(buffer)-1];
126
while (uval > (ulonglong) LONG_MAX)
128
ulonglong quo= uval/(uint) 10;
129
uint rem= (uint) (uval- quo* (uint) 10);
130
*--p = _dig_vec_upper[rem];
133
long_val= (long) uval;
134
while (long_val != 0)
136
long quo= long_val/10;
137
*--p = _dig_vec_upper[(uchar) (long_val - quo*10)];
140
while ((*dst++ = *p++) != 0) ;