drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
longlong2str.cc
1/* Copyright (C) 2000 MySQL AB
2
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.
6
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.
11
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16/*
17 Defines: int64_t2str();
18
19 int64_t2str(dst, radix, val)
20 converts the (int64_t) integer "val" to character form and moves it to
21 the destination string "dst" followed by a terminating NUL. The
22 result is normally a pointer to this NUL character, but if the radix
23 is dud the result will be NULL and nothing will be changed.
24
25 If radix is -2..-36, val is taken to be SIGNED.
26 If radix is 2.. 36, val is taken to be UNSIGNED.
27 That is, val is signed if and only if radix is. You will normally
28 use radix -10 only through itoa and ltoa, for radix 2, 8, or 16
29 unsigned is what you generally want.
30
31 _dig_vec is public just in case someone has a use for it.
32 The definitions of itoa and ltoa are actually macros in m_string.h,
33 but this is where the code is.
34
35 Note: The standard itoa() returns a pointer to the argument, when int2str
36 returns the pointer to the end-null.
37 itoa assumes that 10 -base numbers are allways signed and other arn't.
38*/
39
40#include <config.h>
41
42#include "m_string.h"
43
44namespace drizzled {
45namespace internal {
46
47#if !defined(int64_t2str) && !defined(HAVE_LONGLONG2STR)
48
49char _dig_vec_upper[] =
50 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
51
52/*
53 This assumes that int64_t multiplication is faster than int64_t division.
54*/
55
56char *int64_t2str(int64_t val,char *dst,int radix)
57{
58 char buffer[65];
59 long long_val;
60 uint64_t uval= (uint64_t) val;
61
62 if (radix < 0)
63 {
64 if (radix < -36 || radix > -2) return (char*) 0;
65 if (val < 0) {
66 *dst++ = '-';
67 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
68 uval = (uint64_t)0 - uval;
69 }
70 radix = -radix;
71 }
72 else
73 {
74 if (radix > 36 || radix < 2) return (char*) 0;
75 }
76 if (uval == 0)
77 {
78 *dst++='0';
79 *dst='\0';
80 return dst;
81 }
82 char* p = &buffer[sizeof(buffer)-1];
83 *p = '\0';
84
85 while (uval > (uint64_t) LONG_MAX)
86 {
87 uint64_t quo= uval/(uint32_t) radix;
88 uint32_t rem= (uint32_t) (uval- quo* (uint32_t) radix);
89 *--p = _dig_vec_upper[rem];
90 uval= quo;
91 }
92 long_val= (long) uval;
93 while (long_val != 0)
94 {
95 long quo= long_val/radix;
96 *--p = _dig_vec_upper[(unsigned char) (long_val - quo*radix)];
97 long_val= quo;
98 }
99 while ((*dst++ = *p++) != 0) ;
100 return dst-1;
101}
102
103#endif
104
105#ifndef int64_t10_to_str
106char *int64_t10_to_str(int64_t val,char *dst,int radix)
107{
108 char buffer[65];
109 long long_val;
110 uint64_t uval= (uint64_t) val;
111
112 if (radix < 0)
113 {
114 if (val < 0)
115 {
116 *dst++ = '-';
117 /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
118 uval = (uint64_t)0 - uval;
119 }
120 }
121
122 if (uval == 0)
123 {
124 *dst++='0';
125 *dst='\0';
126 return dst;
127 }
128 char* p = &buffer[sizeof(buffer)-1];
129 *p = '\0';
130
131 while (uval > (uint64_t) LONG_MAX)
132 {
133 uint64_t quo= uval/(uint32_t) 10;
134 uint32_t rem= (uint32_t) (uval- quo* (uint32_t) 10);
135 *--p = _dig_vec_upper[rem];
136 uval= quo;
137 }
138 long_val= (long) uval;
139 while (long_val != 0)
140 {
141 long quo= long_val/10;
142 *--p = _dig_vec_upper[(unsigned char) (long_val - quo*10)];
143 long_val= quo;
144 }
145 while ((*dst++ = *p++) != 0) ;
146 return dst-1;
147}
148#endif
149
150} /* namespace internal */
151} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.