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 */
19
_dig_vec arrays are public because they are used in several outer places.
21
char _dig_vec_upper[] =
22
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
23
char _dig_vec_lower[] =
24
"0123456789abcdefghijklmnopqrstuvwxyz";
28
Convert integer to its string representation in given scale of notation.
32
val - value to convert
33
dst - points to buffer where string representation should be stored
34
radix - radix of scale of notation
35
upcase - set to 1 if we should use upper-case digits
38
Converts the (long) integer value to its character form and moves it to
39
the destination buffer followed by a terminating NUL.
40
If radix is -2..-36, val is taken to be SIGNED, if radix is 2..36, val is
41
taken to be UNSIGNED. That is, val is signed if and only if radix is.
42
All other radixes treated as bad and nothing will be changed in this case.
44
For conversion to decimal representation (radix is -10 or 10) one can use
45
optimized int10_to_str() function.
48
Pointer to ending NUL character or (char *)0 if radix is bad.
52
int2str(register int32_t val, register char *dst, register int radix,
58
char *dig_vec= upcase ? _dig_vec_upper : _dig_vec_lower;
59
unsigned long uval= (unsigned long) val;
63
if (radix < -36 || radix > -2)
68
/* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
69
uval = (unsigned long)0 - uval;
73
else if (radix > 36 || radix < 2)
77
The slightly contorted code which follows is due to the fact that
78
few machines directly support unsigned long / and %. Certainly
79
the VAX C compiler generates a subroutine call. In the interests
80
of efficiency (hollow laugh) I let this happen for the first digit
81
only; after that "val" will be in range so that signed integer
82
division will do. Sorry 'bout that. CHECK THE CODE PRODUCED BY
83
YOUR C COMPILER. The first % and / should be unsigned, the second
84
% and / signed, but C compilers tend to be extraordinarily
85
sensitive to minor details of style. This works on a VAX, that's
88
p = &buffer[sizeof(buffer)-1];
90
new_val= uval / (unsigned long) radix;
91
*--p = dig_vec[(unsigned char) (uval- (unsigned long) new_val*(unsigned long) radix)];
98
*--p = dig_vec[res.rem];
105
*--p = dig_vec[(unsigned char) (val-new_val*radix)];
109
while ((*dst++ = *p++) != 0) ;
115
Converts integer to its string representation in decimal notation.
119
val - value to convert
120
dst - points to buffer where string representation should be stored
121
radix - flag that shows whenever val should be taken as signed or not
124
This is version of int2str() function which is optimized for normal case
125
of radix 10/-10. It takes only sign of radix parameter into account and
126
not its absolute value.
129
Pointer to ending NUL character.
132
char *int10_to_str(int32_t val,char *dst,int radix)
137
uint32_t uval = (uint32_t) val;
139
if (radix < 0) /* -10 */
144
/* Avoid integer overflow in (-val) for INT32_MIN (BUG#31799). */
145
uval = (uint32_t)0 - uval;
149
p = &buffer[sizeof(buffer)-1];
151
new_val= (int32_t) (uval / 10);
152
*--p = '0'+ (char) (uval - (uint32_t) new_val * 10);
158
*--p = '0' + (char) (val-new_val*10);
161
while ((*dst++ = *p++) != 0) ;