MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
longlong2str.c
1 /* Copyright (c) 2000, 2001, 2004, 2006, 2007 MySQL AB, 2009 Sun Microsystems, Inc.
2  Use is subject to license terms.
3 
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.
7 
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.
12 
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 */
16 
17 /*
18  Defines: longlong2str();
19 
20  longlong2str(dst, radix, val)
21  converts the (longlong) integer "val" to character form and moves it to
22  the destination string "dst" followed by a terminating NUL. The
23  result is normally a pointer to this NUL character, but if the radix
24  is dud the result will be NullS and nothing will be changed.
25 
26  If radix is -2..-36, val is taken to be SIGNED.
27  If radix is 2.. 36, val is taken to be UNSIGNED.
28  That is, val is signed if and only if radix is. You will normally
29  use radix -10 only through itoa and ltoa, for radix 2, 8, or 16
30  unsigned is what you generally want.
31 
32  _dig_vec is public just in case someone has a use for it.
33  The definitions of itoa and ltoa are actually macros in m_string.h,
34  but this is where the code is.
35 
36  Note: The standard itoa() returns a pointer to the argument, when int2str
37  returns the pointer to the end-null.
38  itoa assumes that 10 -base numbers are allways signed and other arn't.
39 */
40 
41 #include <my_global.h>
42 #include "m_string.h"
43 
44 #ifndef ll2str
45 
46 /*
47  This assumes that longlong multiplication is faster than longlong division.
48 */
49 
50 char *ll2str(longlong val,char *dst,int radix, int upcase)
51 {
52  char buffer[65];
53  register char *p;
54  long long_val;
55  char *dig_vec= upcase ? _dig_vec_upper : _dig_vec_lower;
56  ulonglong uval= (ulonglong) val;
57 
58  if (radix < 0)
59  {
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;
65  }
66  radix = -radix;
67  }
68  else
69  {
70  if (radix > 36 || radix < 2) return (char*) 0;
71  }
72  if (uval == 0)
73  {
74  *dst++='0';
75  *dst='\0';
76  return dst;
77  }
78  p = &buffer[sizeof(buffer)-1];
79  *p = '\0';
80 
81  while (uval > (ulonglong) LONG_MAX)
82  {
83  ulonglong quo= uval/(uint) radix;
84  uint rem= (uint) (uval- quo* (uint) radix);
85  *--p= dig_vec[rem];
86  uval= quo;
87  }
88  long_val= (long) uval;
89  while (long_val != 0)
90  {
91  long quo= long_val/radix;
92  *--p= dig_vec[(uchar) (long_val - quo*radix)];
93  long_val= quo;
94  }
95  while ((*dst++ = *p++) != 0) ;
96  return dst-1;
97 }
98 #endif
99 
100 #ifndef longlong10_to_str
101 char *longlong10_to_str(longlong val,char *dst,int radix)
102 {
103  char buffer[65];
104  register char *p;
105  long long_val;
106  ulonglong uval= (ulonglong) val;
107 
108  if (radix < 0)
109  {
110  if (val < 0)
111  {
112  *dst++ = '-';
113  /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
114  uval = (ulonglong)0 - uval;
115  }
116  }
117 
118  if (uval == 0)
119  {
120  *dst++='0';
121  *dst='\0';
122  return dst;
123  }
124  p = &buffer[sizeof(buffer)-1];
125  *p = '\0';
126 
127  while (uval > (ulonglong) LONG_MAX)
128  {
129  ulonglong quo= uval/(uint) 10;
130  uint rem= (uint) (uval- quo* (uint) 10);
131  *--p = _dig_vec_upper[rem];
132  uval= quo;
133  }
134  long_val= (long) uval;
135  while (long_val != 0)
136  {
137  long quo= long_val/10;
138  *--p = _dig_vec_upper[(uchar) (long_val - quo*10)];
139  long_val= quo;
140  }
141  while ((*dst++ = *p++) != 0) ;
142  return dst-1;
143 }
144 #endif