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