Drizzled Public API Documentation

int2str.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 #include <config.h>
00017 #include <drizzled/internal/m_string.h>
00018 
00019 namespace drizzled
00020 {
00021 namespace internal
00022 {
00023 
00024 /*
00025   Converts integer to its string representation in decimal notation.
00026 
00027   SYNOPSIS
00028     int10_to_str()
00029       val     - value to convert
00030       dst     - points to buffer where string representation should be stored
00031       radix   - flag that shows whenever val should be taken as signed or not
00032 
00033   DESCRIPTION
00034     This is version of int2str() function which is optimized for normal case
00035     of radix 10/-10. It takes only sign of radix parameter into account and
00036     not its absolute value.
00037 
00038   RETURN VALUE
00039     Pointer to ending NUL character.
00040 */
00041 
00042 char *int10_to_str(int32_t val,char *dst,int radix)
00043 {
00044   char buffer[65];
00045   int32_t new_val;
00046   uint32_t uval = (uint32_t) val;
00047 
00048   if (radix < 0)        /* -10 */
00049   {
00050     if (val < 0)
00051     {
00052       *dst++ = '-';
00053       /* Avoid integer overflow in (-val) for INT32_MIN (BUG#31799). */
00054       uval = (uint32_t)0 - uval;
00055     }
00056   }
00057 
00058   char* p = &buffer[sizeof(buffer)-1];
00059   *p = '\0';
00060   new_val= (int32_t) (uval / 10);
00061   *--p = '0'+ (char) (uval - (uint32_t) new_val * 10);
00062   val = new_val;
00063 
00064   while (val != 0)
00065   {
00066     new_val=val/10;
00067     *--p = '0' + (char) (val-new_val*10);
00068     val= new_val;
00069   }
00070   while ((*dst++ = *p++) != 0) ;
00071   return dst-1;
00072 }
00073 
00074 } /* namespace internal */
00075 } /* namespace drizzled */