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 namespace internal {
00046 
00047 #if !defined(int64_t2str) && !defined(HAVE_LONGLONG2STR)
00048 
00049 char _dig_vec_upper[] =
00050   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
00051 
00052 /*
00053   This assumes that int64_t multiplication is faster than int64_t division.
00054 */
00055 
00056 char *int64_t2str(int64_t val,char *dst,int radix)
00057 {
00058   char buffer[65];
00059   long long_val;
00060   uint64_t uval= (uint64_t) val;
00061 
00062   if (radix < 0)
00063   {
00064     if (radix < -36 || radix > -2) return (char*) 0;
00065     if (val < 0) {
00066       *dst++ = '-';
00067       /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
00068       uval = (uint64_t)0 - uval;
00069     }
00070     radix = -radix;
00071   }
00072   else
00073   {
00074     if (radix > 36 || radix < 2) return (char*) 0;
00075   }
00076   if (uval == 0)
00077   {
00078     *dst++='0';
00079     *dst='\0';
00080     return dst;
00081   }
00082   char* p = &buffer[sizeof(buffer)-1];
00083   *p = '\0';
00084 
00085   while (uval > (uint64_t) LONG_MAX)
00086   {
00087     uint64_t quo= uval/(uint32_t) radix;
00088     uint32_t rem= (uint32_t) (uval- quo* (uint32_t) radix);
00089     *--p = _dig_vec_upper[rem];
00090     uval= quo;
00091   }
00092   long_val= (long) uval;
00093   while (long_val != 0)
00094   {
00095     long quo= long_val/radix;
00096     *--p = _dig_vec_upper[(unsigned char) (long_val - quo*radix)];
00097     long_val= quo;
00098   }
00099   while ((*dst++ = *p++) != 0) ;
00100   return dst-1;
00101 }
00102 
00103 #endif
00104 
00105 #ifndef int64_t10_to_str
00106 char *int64_t10_to_str(int64_t val,char *dst,int radix)
00107 {
00108   char buffer[65];
00109   long long_val;
00110   uint64_t uval= (uint64_t) val;
00111 
00112   if (radix < 0)
00113   {
00114     if (val < 0)
00115     {
00116       *dst++ = '-';
00117       /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
00118       uval = (uint64_t)0 - uval;
00119     }
00120   }
00121 
00122   if (uval == 0)
00123   {
00124     *dst++='0';
00125     *dst='\0';
00126     return dst;
00127   }
00128   char* p = &buffer[sizeof(buffer)-1];
00129   *p = '\0';
00130 
00131   while (uval > (uint64_t) LONG_MAX)
00132   {
00133     uint64_t quo= uval/(uint32_t) 10;
00134     uint32_t rem= (uint32_t) (uval- quo* (uint32_t) 10);
00135     *--p = _dig_vec_upper[rem];
00136     uval= quo;
00137   }
00138   long_val= (long) uval;
00139   while (long_val != 0)
00140   {
00141     long quo= long_val/10;
00142     *--p = _dig_vec_upper[(unsigned char) (long_val - quo*10)];
00143     long_val= quo;
00144   }
00145   while ((*dst++ = *p++) != 0) ;
00146   return dst-1;
00147 }
00148 #endif
00149 
00150 } /* namespace internal */
00151 } /* namespace drizzled */