~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
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
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
1130.3.26 by Monty Taylor
Removed global.h from headers.
40
#include "drizzled/global.h"
41
1 by brian
clean slate
42
#include "m_string.h"
43
152 by Brian Aker
longlong replacement
44
#if !defined(int64_t2str) && !defined(HAVE_LONGLONG2STR)
1 by brian
clean slate
45
46
/*
152 by Brian Aker
longlong replacement
47
  This assumes that int64_t multiplication is faster than int64_t division.
1 by brian
clean slate
48
*/
49
152 by Brian Aker
longlong replacement
50
char *int64_t2str(int64_t val,char *dst,int radix)
1 by brian
clean slate
51
{
52
  char buffer[65];
53
  register char *p;
54
  long long_val;
151 by Brian Aker
Ulonglong to uint64_t
55
  uint64_t uval= (uint64_t) val;
1 by brian
clean slate
56
57
  if (radix < 0)
58
  {
59
    if (radix < -36 || radix > -2) return (char*) 0;
60
    if (val < 0) {
61
      *dst++ = '-';
62
      /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
151 by Brian Aker
Ulonglong to uint64_t
63
      uval = (uint64_t)0 - uval;
1 by brian
clean slate
64
    }
65
    radix = -radix;
66
  }
67
  else
68
  {
69
    if (radix > 36 || radix < 2) return (char*) 0;
70
  }
71
  if (uval == 0)
72
  {
73
    *dst++='0';
74
    *dst='\0';
75
    return dst;
76
  }
77
  p = &buffer[sizeof(buffer)-1];
78
  *p = '\0';
79
151 by Brian Aker
Ulonglong to uint64_t
80
  while (uval > (uint64_t) LONG_MAX)
1 by brian
clean slate
81
  {
895 by Brian Aker
Completion (?) of uint conversion.
82
    uint64_t quo= uval/(uint32_t) radix;
83
    uint32_t rem= (uint32_t) (uval- quo* (uint32_t) radix);
1 by brian
clean slate
84
    *--p = _dig_vec_upper[rem];
85
    uval= quo;
86
  }
87
  long_val= (long) uval;
88
  while (long_val != 0)
89
  {
90
    long quo= long_val/radix;
481 by Brian Aker
Remove all of uchar.
91
    *--p = _dig_vec_upper[(unsigned char) (long_val - quo*radix)];
1 by brian
clean slate
92
    long_val= quo;
93
  }
94
  while ((*dst++ = *p++) != 0) ;
95
  return dst-1;
96
}
97
98
#endif
99
152 by Brian Aker
longlong replacement
100
#ifndef int64_t10_to_str
101
char *int64_t10_to_str(int64_t val,char *dst,int radix)
1 by brian
clean slate
102
{
103
  char buffer[65];
104
  register char *p;
105
  long long_val;
151 by Brian Aker
Ulonglong to uint64_t
106
  uint64_t uval= (uint64_t) val;
1 by brian
clean slate
107
108
  if (radix < 0)
109
  {
110
    if (val < 0)
111
    {
112
      *dst++ = '-';
113
      /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
151 by Brian Aker
Ulonglong to uint64_t
114
      uval = (uint64_t)0 - uval;
1 by brian
clean slate
115
    }
116
  }
117
118
  if (uval == 0)
119
  {
120
    *dst++='0';
121
    *dst='\0';
122
    return dst;
123
  }
124
  p = &buffer[sizeof(buffer)-1];
125
  *p = '\0';
126
151 by Brian Aker
Ulonglong to uint64_t
127
  while (uval > (uint64_t) LONG_MAX)
1 by brian
clean slate
128
  {
895 by Brian Aker
Completion (?) of uint conversion.
129
    uint64_t quo= uval/(uint32_t) 10;
130
    uint32_t rem= (uint32_t) (uval- quo* (uint32_t) 10);
1 by brian
clean slate
131
    *--p = _dig_vec_upper[rem];
132
    uval= quo;
133
  }
134
  long_val= (long) uval;
135
  while (long_val != 0)
136
  {
137
    long quo= long_val/10;
481 by Brian Aker
Remove all of uchar.
138
    *--p = _dig_vec_upper[(unsigned char) (long_val - quo*10)];
1 by brian
clean slate
139
    long_val= quo;
140
  }
141
  while ((*dst++ = *p++) != 0) ;
142
  return dst-1;
143
}
144
#endif