Drizzled Public API Documentation

int2str.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 #include <config.h>
17 #include <drizzled/internal/m_string.h>
18 
19 namespace drizzled
20 {
21 namespace internal
22 {
23 
24 /*
25  Converts integer to its string representation in decimal notation.
26 
27  SYNOPSIS
28  int10_to_str()
29  val - value to convert
30  dst - points to buffer where string representation should be stored
31  radix - flag that shows whenever val should be taken as signed or not
32 
33  DESCRIPTION
34  This is version of int2str() function which is optimized for normal case
35  of radix 10/-10. It takes only sign of radix parameter into account and
36  not its absolute value.
37 
38  RETURN VALUE
39  Pointer to ending NUL character.
40 */
41 
42 char *int10_to_str(int32_t val,char *dst,int radix)
43 {
44  char buffer[65];
45  int32_t new_val;
46  uint32_t uval = (uint32_t) val;
47 
48  if (radix < 0) /* -10 */
49  {
50  if (val < 0)
51  {
52  *dst++ = '-';
53  /* Avoid integer overflow in (-val) for INT32_MIN (BUG#31799). */
54  uval = (uint32_t)0 - uval;
55  }
56  }
57 
58  char* p = &buffer[sizeof(buffer)-1];
59  *p = '\0';
60  new_val= (int32_t) (uval / 10);
61  *--p = '0'+ (char) (uval - (uint32_t) new_val * 10);
62  val = new_val;
63 
64  while (val != 0)
65  {
66  new_val=val/10;
67  *--p = '0' + (char) (val-new_val*10);
68  val= new_val;
69  }
70  while ((*dst++ = *p++) != 0) ;
71  return dst-1;
72 }
73 
74 } /* namespace internal */
75 } /* namespace drizzled */