~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
/*
152 by Brian Aker
longlong replacement
17
  Defines: int64_t2str();
1 by brian
clean slate
18
152 by Brian Aker
longlong replacement
19
  int64_t2str(dst, radix, val)
20
  converts the (int64_t) integer "val" to character form and moves it to
1 by brian
clean slate
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
461 by Monty Taylor
Removed NullS. bu-bye.
23
  is dud the result will be NULL and nothing will be changed.
1 by brian
clean slate
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
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
40
#include "config.h"
1130.3.26 by Monty Taylor
Removed global.h from headers.
41
1 by brian
clean slate
42
#include "m_string.h"
43
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
44
namespace drizzled
45
{
46
namespace internal
47
{
48
152 by Brian Aker
longlong replacement
49
#if !defined(int64_t2str) && !defined(HAVE_LONGLONG2STR)
1 by brian
clean slate
50
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
51
char _dig_vec_upper[] =
52
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
53
1 by brian
clean slate
54
/*
152 by Brian Aker
longlong replacement
55
  This assumes that int64_t multiplication is faster than int64_t division.
1 by brian
clean slate
56
*/
57
152 by Brian Aker
longlong replacement
58
char *int64_t2str(int64_t val,char *dst,int radix)
1 by brian
clean slate
59
{
60
  char buffer[65];
61
  register char *p;
62
  long long_val;
151 by Brian Aker
Ulonglong to uint64_t
63
  uint64_t uval= (uint64_t) val;
1 by brian
clean slate
64
65
  if (radix < 0)
66
  {
67
    if (radix < -36 || radix > -2) return (char*) 0;
68
    if (val < 0) {
69
      *dst++ = '-';
70
      /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
151 by Brian Aker
Ulonglong to uint64_t
71
      uval = (uint64_t)0 - uval;
1 by brian
clean slate
72
    }
73
    radix = -radix;
74
  }
75
  else
76
  {
77
    if (radix > 36 || radix < 2) return (char*) 0;
78
  }
79
  if (uval == 0)
80
  {
81
    *dst++='0';
82
    *dst='\0';
83
    return dst;
84
  }
85
  p = &buffer[sizeof(buffer)-1];
86
  *p = '\0';
87
151 by Brian Aker
Ulonglong to uint64_t
88
  while (uval > (uint64_t) LONG_MAX)
1 by brian
clean slate
89
  {
895 by Brian Aker
Completion (?) of uint conversion.
90
    uint64_t quo= uval/(uint32_t) radix;
91
    uint32_t rem= (uint32_t) (uval- quo* (uint32_t) radix);
1 by brian
clean slate
92
    *--p = _dig_vec_upper[rem];
93
    uval= quo;
94
  }
95
  long_val= (long) uval;
96
  while (long_val != 0)
97
  {
98
    long quo= long_val/radix;
481 by Brian Aker
Remove all of uchar.
99
    *--p = _dig_vec_upper[(unsigned char) (long_val - quo*radix)];
1 by brian
clean slate
100
    long_val= quo;
101
  }
102
  while ((*dst++ = *p++) != 0) ;
103
  return dst-1;
104
}
105
106
#endif
107
152 by Brian Aker
longlong replacement
108
#ifndef int64_t10_to_str
109
char *int64_t10_to_str(int64_t val,char *dst,int radix)
1 by brian
clean slate
110
{
111
  char buffer[65];
112
  register char *p;
113
  long long_val;
151 by Brian Aker
Ulonglong to uint64_t
114
  uint64_t uval= (uint64_t) val;
1 by brian
clean slate
115
116
  if (radix < 0)
117
  {
118
    if (val < 0)
119
    {
120
      *dst++ = '-';
121
      /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
151 by Brian Aker
Ulonglong to uint64_t
122
      uval = (uint64_t)0 - uval;
1 by brian
clean slate
123
    }
124
  }
125
126
  if (uval == 0)
127
  {
128
    *dst++='0';
129
    *dst='\0';
130
    return dst;
131
  }
132
  p = &buffer[sizeof(buffer)-1];
133
  *p = '\0';
134
151 by Brian Aker
Ulonglong to uint64_t
135
  while (uval > (uint64_t) LONG_MAX)
1 by brian
clean slate
136
  {
895 by Brian Aker
Completion (?) of uint conversion.
137
    uint64_t quo= uval/(uint32_t) 10;
138
    uint32_t rem= (uint32_t) (uval- quo* (uint32_t) 10);
1 by brian
clean slate
139
    *--p = _dig_vec_upper[rem];
140
    uval= quo;
141
  }
142
  long_val= (long) uval;
143
  while (long_val != 0)
144
  {
145
    long quo= long_val/10;
481 by Brian Aker
Remove all of uchar.
146
    *--p = _dig_vec_upper[(unsigned char) (long_val - quo*10)];
1 by brian
clean slate
147
    long_val= quo;
148
  }
149
  while ((*dst++ = *p++) != 0) ;
150
  return dst-1;
151
}
152
#endif
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
153
154
} /* namespace internal */
155
} /* namespace drizzled */