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
18 #include <my_global.h>
22 _dig_vec arrays are public because they are used in several outer places.
24 char NEAR _dig_vec_upper
[] =
25 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
26 char NEAR _dig_vec_lower
[] =
27 "0123456789abcdefghijklmnopqrstuvwxyz";
31 Convert integer to its string representation in given scale of notation.
35 val - value to convert
36 dst - points to buffer where string representation should be stored
37 radix - radix of scale of notation
38 upcase - set to 1 if we should use upper-case digits
41 Converts the (long) integer value to its character form and moves it to
42 the destination buffer followed by a terminating NUL.
43 If radix is -2..-36, val is taken to be SIGNED, if radix is 2..36, val is
44 taken to be UNSIGNED. That is, val is signed if and only if radix is.
45 All other radixes treated as bad and nothing will be changed in this case.
47 For conversion to decimal representation (radix is -10 or 10) one can use
48 optimized int10_to_str() function.
51 Pointer to ending NUL character or NullS if radix is bad.
55 int2str(register long int val
, register char *dst
, register int radix
,
61 char *dig_vec
= upcase
? _dig_vec_upper
: _dig_vec_lower
;
62 ulong uval
= (ulong
) val
;
66 if (radix
< -36 || radix
> -2)
71 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
72 uval
= (ulong
)0 - uval
;
76 else if (radix
> 36 || radix
< 2)
80 The slightly contorted code which follows is due to the fact that
81 few machines directly support unsigned long / and %. Certainly
82 the VAX C compiler generates a subroutine call. In the interests
83 of efficiency (hollow laugh) I let this happen for the first digit
84 only; after that "val" will be in range so that signed integer
85 division will do. Sorry 'bout that. CHECK THE CODE PRODUCED BY
86 YOUR C COMPILER. The first % and / should be unsigned, the second
87 % and / signed, but C compilers tend to be extraordinarily
88 sensitive to minor details of style. This works on a VAX, that's
91 p
= &buffer
[sizeof(buffer
)-1];
93 new_val
= uval
/ (ulong
) radix
;
94 *--p
= dig_vec
[(uchar
) (uval
- (ulong
) new_val
*(ulong
) radix
)];
101 *--p
= dig_vec
[res
.rem
];
108 *--p
= dig_vec
[(uchar
) (val
-new_val
*radix
)];
112 while ((*dst
++ = *p
++) != 0) ;
118 Converts integer to its string representation in decimal notation.
122 val - value to convert
123 dst - points to buffer where string representation should be stored
124 radix - flag that shows whenever val should be taken as signed or not
127 This is version of int2str() function which is optimized for normal case
128 of radix 10/-10. It takes only sign of radix parameter into account and
129 not its absolute value.
132 Pointer to ending NUL character.
135 char *int10_to_str(long int val
,char *dst
,int radix
)
140 unsigned long int uval
= (unsigned long int) val
;
142 if (radix
< 0) /* -10 */
147 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
148 uval
= (unsigned long int)0 - uval
;
152 p
= &buffer
[sizeof(buffer
)-1];
154 new_val
= (long) (uval
/ 10);
155 *--p
= '0'+ (char) (uval
- (unsigned long) new_val
* 10);
161 *--p
= '0' + (char) (val
-new_val
*10);
164 while ((*dst
++ = *p
++) != 0) ;