Drizzled Public API Documentation

longlong2str.cc

00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 /*
00017   Defines: int64_t2str();
00018 
00019   int64_t2str(dst, radix, val)
00020   converts the (int64_t) integer "val" to character form and moves it to
00021   the destination string "dst" followed by a terminating NUL.  The
00022   result is normally a pointer to this NUL character, but if the radix
00023   is dud the result will be NULL and nothing will be changed.
00024 
00025   If radix is -2..-36, val is taken to be SIGNED.
00026   If radix is  2.. 36, val is taken to be UNSIGNED.
00027   That is, val is signed if and only if radix is.  You will normally
00028   use radix -10 only through itoa and ltoa, for radix 2, 8, or 16
00029   unsigned is what you generally want.
00030 
00031   _dig_vec is public just in case someone has a use for it.
00032   The definitions of itoa and ltoa are actually macros in m_string.h,
00033   but this is where the code is.
00034 
00035   Note: The standard itoa() returns a pointer to the argument, when int2str
00036   returns the pointer to the end-null.
00037   itoa assumes that 10 -base numbers are allways signed and other arn't.
00038 */
00039 
00040 #include <config.h>
00041 
00042 #include "m_string.h"
00043 
00044 namespace drizzled
00045 {
00046 namespace internal
00047 {
00048 
00049 #if !defined(int64_t2str) && !defined(HAVE_LONGLONG2STR)
00050 
00051 char _dig_vec_upper[] =
00052   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
00053 
00054 /*
00055   This assumes that int64_t multiplication is faster than int64_t division.
00056 */
00057 
00058 char *int64_t2str(int64_t val,char *dst,int radix)
00059 {
00060   char buffer[65];
00061   long long_val;
00062   uint64_t uval= (uint64_t) val;
00063 
00064   if (radix < 0)
00065   {
00066     if (radix < -36 || radix > -2) return (char*) 0;
00067     if (val < 0) {
00068       *dst++ = '-';
00069       /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
00070       uval = (uint64_t)0 - uval;
00071     }
00072     radix = -radix;
00073   }
00074   else
00075   {
00076     if (radix > 36 || radix < 2) return (char*) 0;
00077   }
00078   if (uval == 0)
00079   {
00080     *dst++='0';
00081     *dst='\0';
00082     return dst;
00083   }
00084   char* p = &buffer[sizeof(buffer)-1];
00085   *p = '\0';
00086 
00087   while (uval > (uint64_t) LONG_MAX)
00088   {
00089     uint64_t quo= uval/(uint32_t) radix;
00090     uint32_t rem= (uint32_t) (uval- quo* (uint32_t) radix);
00091     *--p = _dig_vec_upper[rem];
00092     uval= quo;
00093   }
00094   long_val= (long) uval;
00095   while (long_val != 0)
00096   {
00097     long quo= long_val/radix;
00098     *--p = _dig_vec_upper[(unsigned char) (long_val - quo*radix)];
00099     long_val= quo;
00100   }
00101   while ((*dst++ = *p++) != 0) ;
00102   return dst-1;
00103 }
00104 
00105 #endif
00106 
00107 #ifndef int64_t10_to_str
00108 char *int64_t10_to_str(int64_t val,char *dst,int radix)
00109 {
00110   char buffer[65];
00111   long long_val;
00112   uint64_t uval= (uint64_t) val;
00113 
00114   if (radix < 0)
00115   {
00116     if (val < 0)
00117     {
00118       *dst++ = '-';
00119       /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
00120       uval = (uint64_t)0 - uval;
00121     }
00122   }
00123 
00124   if (uval == 0)
00125   {
00126     *dst++='0';
00127     *dst='\0';
00128     return dst;
00129   }
00130   char* p = &buffer[sizeof(buffer)-1];
00131   *p = '\0';
00132 
00133   while (uval > (uint64_t) LONG_MAX)
00134   {
00135     uint64_t quo= uval/(uint32_t) 10;
00136     uint32_t rem= (uint32_t) (uval- quo* (uint32_t) 10);
00137     *--p = _dig_vec_upper[rem];
00138     uval= quo;
00139   }
00140   long_val= (long) uval;
00141   while (long_val != 0)
00142   {
00143     long quo= long_val/10;
00144     *--p = _dig_vec_upper[(unsigned char) (long_val - quo*10)];
00145     long_val= quo;
00146   }
00147   while ((*dst++ = *p++) != 0) ;
00148   return dst-1;
00149 }
00150 #endif
00151 
00152 } /* namespace internal */
00153 } /* namespace drizzled */