mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / strings / longlong2str.c
blob6c96e2ce35d2fd6f0f2cca65f44ba87affa43306
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
19 Defines: longlong2str();
21 longlong2str(dst, radix, val)
22 converts the (longlong) integer "val" to character form and moves it to
23 the destination string "dst" followed by a terminating NUL. The
24 result is normally a pointer to this NUL character, but if the radix
25 is dud the result will be NullS and nothing will be changed.
27 If radix is -2..-36, val is taken to be SIGNED.
28 If radix is 2.. 36, val is taken to be UNSIGNED.
29 That is, val is signed if and only if radix is. You will normally
30 use radix -10 only through itoa and ltoa, for radix 2, 8, or 16
31 unsigned is what you generally want.
33 _dig_vec is public just in case someone has a use for it.
34 The definitions of itoa and ltoa are actually macros in m_string.h,
35 but this is where the code is.
37 Note: The standard itoa() returns a pointer to the argument, when int2str
38 returns the pointer to the end-null.
39 itoa assumes that 10 -base numbers are allways signed and other arn't.
42 #include <my_global.h>
43 #include "m_string.h"
45 #if defined(HAVE_LONG_LONG) && !defined(longlong2str) && !defined(HAVE_LONGLONG2STR)
48 This assumes that longlong multiplication is faster than longlong division.
51 char *longlong2str(longlong val,char *dst,int radix)
53 char buffer[65];
54 register char *p;
55 long long_val;
56 ulonglong uval= (ulonglong) val;
58 if (radix < 0)
60 if (radix < -36 || radix > -2) return (char*) 0;
61 if (val < 0) {
62 *dst++ = '-';
63 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
64 uval = (ulonglong)0 - uval;
66 radix = -radix;
68 else
70 if (radix > 36 || radix < 2) return (char*) 0;
72 if (uval == 0)
74 *dst++='0';
75 *dst='\0';
76 return dst;
78 p = &buffer[sizeof(buffer)-1];
79 *p = '\0';
81 while (uval > (ulonglong) LONG_MAX)
83 ulonglong quo= uval/(uint) radix;
84 uint rem= (uint) (uval- quo* (uint) radix);
85 *--p = _dig_vec_upper[rem];
86 uval= quo;
88 long_val= (long) uval;
89 while (long_val != 0)
91 long quo= long_val/radix;
92 *--p = _dig_vec_upper[(uchar) (long_val - quo*radix)];
93 long_val= quo;
95 while ((*dst++ = *p++) != 0) ;
96 return dst-1;
99 #endif
101 #ifndef longlong10_to_str
102 char *longlong10_to_str(longlong val,char *dst,int radix)
104 char buffer[65];
105 register char *p;
106 long long_val;
107 ulonglong uval= (ulonglong) val;
109 if (radix < 0)
111 if (val < 0)
113 *dst++ = '-';
114 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
115 uval = (ulonglong)0 - uval;
119 if (uval == 0)
121 *dst++='0';
122 *dst='\0';
123 return dst;
125 p = &buffer[sizeof(buffer)-1];
126 *p = '\0';
128 while (uval > (ulonglong) LONG_MAX)
130 ulonglong quo= uval/(uint) 10;
131 uint rem= (uint) (uval- quo* (uint) 10);
132 *--p = _dig_vec_upper[rem];
133 uval= quo;
135 long_val= (long) uval;
136 while (long_val != 0)
138 long quo= long_val/10;
139 *--p = _dig_vec_upper[(uchar) (long_val - quo*10)];
140 long_val= quo;
142 while ((*dst++ = *p++) != 0) ;
143 return dst-1;
145 #endif