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