12
12
You should have received a copy of the GNU General Public License
13
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
17
#include <drizzled/internal/m_string.h>
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include <my_global.h>
17
#include <my_sys.h> /* Needed for MY_ERRNO_ERANGE */
22
Needed under MetroWerks Compiler, since MetroWerks compiler does not
23
properly handle a constant expression containing a mod operator
25
#if defined(__NETWARE__) && defined(__MWERKS__)
26
static uint64_t uint64_t_max= ~(uint64_t) 0;
27
#define ULONGLONG_MAX uint64_t_max
29
#define ULONGLONG_MAX (~(uint64_t) 0)
30
#endif /* __NETWARE__ && __MWERKS__ */
25
31
#define MAX_NEGATIVE_NUMBER ((uint64_t) 0x8000000000000000LL)
27
33
#define LFACTOR 1000000000ULL
28
34
#define LFACTOR1 10000000000ULL
29
35
#define LFACTOR2 100000000000ULL
31
static uint64_t lfactor[9]=
37
static unsigned long lfactor[9]=
33
39
1L, 10L, 100L, 1000L, 10000L, 100000L, 1000000L, 10000000L, 100000000L
37
Convert a string to an to uint64_t integer value
43
Convert a string to an to unsigned long long integer value
41
47
nptr in pointer to the string to be converted
42
48
endptr in/out pointer to the end of the string/
43
49
pointer to the stop character
44
50
error out returned error code
47
53
This function takes the decimal representation of integer number
48
54
from string nptr and converts it to an signed or unsigned
55
long long integer value.
50
56
Space characters and tab are ignored.
51
57
A sign character might precede the digit characters. The number
52
58
may have any number of pre-zero digits.
65
71
-1 Number was an ok negative number
67
73
ERANGE If the the value of the converted number exceeded the
68
maximum negative/uint64_t integer.
69
In this case the return value is UINT64_MAX if value was
70
positive and UINT64_MIN if value was negative.
74
maximum negative/unsigned long long integer.
75
In this case the return value is ~0 if value was
76
positive and LONGLONG_MIN if value was negative.
71
77
EDOM If the string didn't contain any digits. In this case
72
78
the return value is 0.
81
87
const char *s, *end, *start, *n_end, *true_end;
84
90
unsigned long i, j, k;
87
uint64_t cutoff, cutoff2, cutoff3;
93
ulong cutoff, cutoff2, cutoff3;
90
96
/* If fixed length string */
130
cutoff= UINT64_MAX / LFACTOR2;
131
cutoff2= UINT64_MAX % LFACTOR2 / 100;
132
cutoff3= UINT64_MAX % 100;
136
cutoff= ULONGLONG_MAX / LFACTOR2;
137
cutoff2= ULONGLONG_MAX % LFACTOR2 / 100;
138
cutoff3= ULONGLONG_MAX % 100;
135
141
/* Handle case where we have a lot of pre-zero */
205
211
return (int64_t) li;
207
213
overflow: /* *endptr is set here */
209
return negative ? INT64_MIN: INT64_MAX;
214
*error= MY_ERRNO_ERANGE;
215
return negative ? LONGLONG_MIN : (int64_t) ULONGLONG_MAX;
212
218
*endptr= (char*) s;
213
219
return (negative ? ((int64_t) -(long) i) : (int64_t) i);
216
li= (uint64_t) i * lfactor[(unsigned int) (s-start)] + j;
222
li= (uint64_t) i * lfactor[(uint) (s-start)] + j;
217
223
*endptr= (char*) s;
218
224
return (negative ? -((int64_t) li) : (int64_t) li);