~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to strings/longlong2str.c

mergeĀ mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/*
17
 
  Defines: int64_t2str();
 
17
  Defines: longlong2str();
18
18
 
19
 
  int64_t2str(dst, radix, val)
20
 
  converts the (int64_t) integer "val" to character form and moves it to
 
19
  longlong2str(dst, radix, val)
 
20
  converts the (longlong) integer "val" to character form and moves it to
21
21
  the destination string "dst" followed by a terminating NUL.  The
22
22
  result is normally a pointer to this NUL character, but if the radix
23
 
  is dud the result will be NULL and nothing will be changed.
 
23
  is dud the result will be NullS and nothing will be changed.
24
24
 
25
25
  If radix is -2..-36, val is taken to be SIGNED.
26
26
  If radix is  2.. 36, val is taken to be UNSIGNED.
37
37
        itoa assumes that 10 -base numbers are allways signed and other arn't.
38
38
*/
39
39
 
 
40
#include <my_global.h>
40
41
#include "m_string.h"
41
42
 
42
 
#if !defined(int64_t2str) && !defined(HAVE_LONGLONG2STR)
 
43
#if !defined(longlong2str) && !defined(HAVE_LONGLONG2STR)
43
44
 
44
45
/*
45
 
  This assumes that int64_t multiplication is faster than int64_t division.
 
46
  This assumes that longlong multiplication is faster than longlong division.
46
47
*/
47
48
 
48
 
char *int64_t2str(int64_t val,char *dst,int radix)
 
49
char *longlong2str(longlong val,char *dst,int radix)
49
50
{
50
51
  char buffer[65];
51
52
  register char *p;
52
53
  long long_val;
53
 
  uint64_t uval= (uint64_t) val;
 
54
  ulonglong uval= (ulonglong) val;
54
55
 
55
56
  if (radix < 0)
56
57
  {
58
59
    if (val < 0) {
59
60
      *dst++ = '-';
60
61
      /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
61
 
      uval = (uint64_t)0 - uval;
 
62
      uval = (ulonglong)0 - uval;
62
63
    }
63
64
    radix = -radix;
64
65
  }
75
76
  p = &buffer[sizeof(buffer)-1];
76
77
  *p = '\0';
77
78
 
78
 
  while (uval > (uint64_t) LONG_MAX)
 
79
  while (uval > (ulonglong) LONG_MAX)
79
80
  {
80
 
    uint64_t quo= uval/(uint) radix;
81
 
    uint32_t rem= (uint) (uval- quo* (uint) radix);
 
81
    ulonglong quo= uval/(uint) radix;
 
82
    uint rem= (uint) (uval- quo* (uint) radix);
82
83
    *--p = _dig_vec_upper[rem];
83
84
    uval= quo;
84
85
  }
86
87
  while (long_val != 0)
87
88
  {
88
89
    long quo= long_val/radix;
89
 
    *--p = _dig_vec_upper[(unsigned char) (long_val - quo*radix)];
 
90
    *--p = _dig_vec_upper[(uchar) (long_val - quo*radix)];
90
91
    long_val= quo;
91
92
  }
92
93
  while ((*dst++ = *p++) != 0) ;
95
96
 
96
97
#endif
97
98
 
98
 
#ifndef int64_t10_to_str
99
 
char *int64_t10_to_str(int64_t val,char *dst,int radix)
 
99
#ifndef longlong10_to_str
 
100
char *longlong10_to_str(longlong val,char *dst,int radix)
100
101
{
101
102
  char buffer[65];
102
103
  register char *p;
103
104
  long long_val;
104
 
  uint64_t uval= (uint64_t) val;
 
105
  ulonglong uval= (ulonglong) val;
105
106
 
106
107
  if (radix < 0)
107
108
  {
109
110
    {
110
111
      *dst++ = '-';
111
112
      /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
112
 
      uval = (uint64_t)0 - uval;
 
113
      uval = (ulonglong)0 - uval;
113
114
    }
114
115
  }
115
116
 
122
123
  p = &buffer[sizeof(buffer)-1];
123
124
  *p = '\0';
124
125
 
125
 
  while (uval > (uint64_t) LONG_MAX)
 
126
  while (uval > (ulonglong) LONG_MAX)
126
127
  {
127
 
    uint64_t quo= uval/(uint) 10;
128
 
    uint32_t rem= (uint) (uval- quo* (uint) 10);
 
128
    ulonglong quo= uval/(uint) 10;
 
129
    uint rem= (uint) (uval- quo* (uint) 10);
129
130
    *--p = _dig_vec_upper[rem];
130
131
    uval= quo;
131
132
  }
133
134
  while (long_val != 0)
134
135
  {
135
136
    long quo= long_val/10;
136
 
    *--p = _dig_vec_upper[(unsigned char) (long_val - quo*10)];
 
137
    *--p = _dig_vec_upper[(uchar) (long_val - quo*10)];
137
138
    long_val= quo;
138
139
  }
139
140
  while ((*dst++ = *p++) != 0) ;