mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / strings / int2str.c
blob073b3bed69d614c9755d5089b9693426d33f6fae
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>
19 #include "m_string.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.
33 SYNOPSIS
34 int2str()
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
40 DESCRIPTION
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.
50 RETURN VALUE
51 Pointer to ending NUL character or NullS if radix is bad.
54 char *
55 int2str(register long int val, register char *dst, register int radix,
56 int upcase)
58 char buffer[65];
59 register char *p;
60 long int new_val;
61 char *dig_vec= upcase ? _dig_vec_upper : _dig_vec_lower;
62 ulong uval= (ulong) val;
64 if (radix < 0)
66 if (radix < -36 || radix > -2)
67 return NullS;
68 if (val < 0)
70 *dst++ = '-';
71 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
72 uval = (ulong)0 - uval;
74 radix = -radix;
76 else if (radix > 36 || radix < 2)
77 return NullS;
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
89 all I claim for it.
91 p = &buffer[sizeof(buffer)-1];
92 *p = '\0';
93 new_val= uval / (ulong) radix;
94 *--p = dig_vec[(uchar) (uval- (ulong) new_val*(ulong) radix)];
95 val = new_val;
96 #ifdef HAVE_LDIV
97 while (val != 0)
99 ldiv_t res;
100 res=ldiv(val,radix);
101 *--p = dig_vec[res.rem];
102 val= res.quot;
104 #else
105 while (val != 0)
107 new_val=val/radix;
108 *--p = dig_vec[(uchar) (val-new_val*radix)];
109 val= new_val;
111 #endif
112 while ((*dst++ = *p++) != 0) ;
113 return dst-1;
118 Converts integer to its string representation in decimal notation.
120 SYNOPSIS
121 int10_to_str()
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
126 DESCRIPTION
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.
131 RETURN VALUE
132 Pointer to ending NUL character.
135 char *int10_to_str(long int val,char *dst,int radix)
137 char buffer[65];
138 register char *p;
139 long int new_val;
140 unsigned long int uval = (unsigned long int) val;
142 if (radix < 0) /* -10 */
144 if (val < 0)
146 *dst++ = '-';
147 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
148 uval = (unsigned long int)0 - uval;
152 p = &buffer[sizeof(buffer)-1];
153 *p = '\0';
154 new_val= (long) (uval / 10);
155 *--p = '0'+ (char) (uval - (unsigned long) new_val * 10);
156 val = new_val;
158 while (val != 0)
160 new_val=val/10;
161 *--p = '0' + (char) (val-new_val*10);
162 val= new_val;
164 while ((*dst++ = *p++) != 0) ;
165 return dst-1;