~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
/*
17
  Defines: longlong2str();
18
19
  longlong2str(dst, radix, val)
20
  converts the (longlong) integer "val" to character form and moves it to
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
53.2.31 by Monty Taylor
Removed HAVE_LONG_LONG, as this is now assumed.
43
#if !defined(longlong2str) && !defined(HAVE_LONGLONG2STR)
1 by brian
clean slate
44
45
/*
46
  This assumes that longlong multiplication is faster than longlong division.
47
*/
48
49
char *longlong2str(longlong val,char *dst,int radix)
50
{
51
  char buffer[65];
52
  register char *p;
53
  long long_val;
54
  ulonglong uval= (ulonglong) val;
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). */
62
      uval = (ulonglong)0 - uval;
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
79
  while (uval > (ulonglong) LONG_MAX)
80
  {
81
    ulonglong quo= uval/(uint) radix;
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
99
#ifndef longlong10_to_str
100
char *longlong10_to_str(longlong val,char *dst,int radix)
101
{
102
  char buffer[65];
103
  register char *p;
104
  long long_val;
105
  ulonglong uval= (ulonglong) val;
106
107
  if (radix < 0)
108
  {
109
    if (val < 0)
110
    {
111
      *dst++ = '-';
112
      /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
113
      uval = (ulonglong)0 - uval;
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
126
  while (uval > (ulonglong) LONG_MAX)
127
  {
128
    ulonglong quo= uval/(uint) 10;
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