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 */
16
#include <my_global.h>
20
_dig_vec arrays are public because they are used in several outer places.
22
char NEAR _dig_vec_upper[] =
23
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
24
char NEAR _dig_vec_lower[] =
25
"0123456789abcdefghijklmnopqrstuvwxyz";
29
Convert integer to its string representation in given scale of notation.
33
val - value to convert
34
dst - points to buffer where string representation should be stored
35
radix - radix of scale of notation
36
upcase - set to 1 if we should use upper-case digits
39
Converts the (long) integer value to its character form and moves it to
40
the destination buffer followed by a terminating NUL.
41
If radix is -2..-36, val is taken to be SIGNED, if radix is 2..36, val is
42
taken to be UNSIGNED. That is, val is signed if and only if radix is.
43
All other radixes treated as bad and nothing will be changed in this case.
45
For conversion to decimal representation (radix is -10 or 10) one can use
46
optimized int10_to_str() function.
49
Pointer to ending NUL character or NullS if radix is bad.
53
int2str(register long int val, register char *dst, register int radix,
59
char *dig_vec= upcase ? _dig_vec_upper : _dig_vec_lower;
63
if (radix < -36 || radix > -2)
72
else if (radix > 36 || radix < 2)
76
The slightly contorted code which follows is due to the fact that
77
few machines directly support unsigned long / and %. Certainly
78
the VAX C compiler generates a subroutine call. In the interests
79
of efficiency (hollow laugh) I let this happen for the first digit
80
only; after that "val" will be in range so that signed integer
81
division will do. Sorry 'bout that. CHECK THE CODE PRODUCED BY
82
YOUR C COMPILER. The first % and / should be unsigned, the second
83
% and / signed, but C compilers tend to be extraordinarily
84
sensitive to minor details of style. This works on a VAX, that's
87
p = &buffer[sizeof(buffer)-1];
89
new_val=(ulong) val / (ulong) radix;
90
*--p = dig_vec[(uchar) ((ulong) val- (ulong) new_val*(ulong) radix)];
97
*--p = dig_vec[res.rem];
104
*--p = dig_vec[(uchar) (val-new_val*radix)];
108
while ((*dst++ = *p++) != 0) ;
114
Converts integer to its string representation in decimal notation.
118
val - value to convert
119
dst - points to buffer where string representation should be stored
120
radix - flag that shows whenever val should be taken as signed or not
123
This is version of int2str() function which is optimized for normal case
124
of radix 10/-10. It takes only sign of radix parameter into account and
125
not its absolute value.
128
Pointer to ending NUL character.
131
char *int10_to_str(long int val,char *dst,int radix)
137
if (radix < 0) /* -10 */
146
p = &buffer[sizeof(buffer)-1];
148
new_val= (long) ((unsigned long int) val / 10);
149
*--p = '0'+ (char) ((unsigned long int) val - (unsigned long) new_val * 10);
155
*--p = '0' + (char) (val-new_val*10);
158
while ((*dst++ = *p++) != 0) ;