~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to strings/longlong2str.c

  • Committer: Monty Taylor
  • Date: 2008-07-11 15:23:55 UTC
  • mto: (77.6.1 glibclient-merge)
  • mto: This revision was merged to the branch mainline in revision 134.
  • Revision ID: monty@inaugust.com-20080711152355-o92cod4mg0u9ck03
More const-correctness.

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