~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/my_strtoll10.cc

Merge of Jay

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include <m_string.h>
 
16
#include <drizzled/global.h>
 
17
#include <mystrings/m_string.h>
17
18
#include <errno.h>
18
19
 
19
 
#undef  ULONGLONG_MAX
20
 
#define ULONGLONG_MAX           (~(uint64_t) 0)
21
20
#define MAX_NEGATIVE_NUMBER     ((uint64_t) 0x8000000000000000LL)
22
21
#define INIT_CNT  9
23
22
#define LFACTOR   1000000000ULL
24
23
#define LFACTOR1  10000000000ULL
25
24
#define LFACTOR2  100000000000ULL
26
25
 
27
 
static unsigned long lfactor[9]=
 
26
static uint64_t lfactor[9]=
28
27
{
29
28
  1L, 10L, 100L, 1000L, 10000L, 100000L, 1000000L, 10000000L, 100000000L
30
29
};
31
30
 
32
31
/*
33
 
  Convert a string to an to unsigned long long integer value
34
 
  
 
32
  Convert a string to an to uint64_t integer value
 
33
 
35
34
  SYNOPSYS
36
35
    my_strtoll10()
37
36
      nptr     in       pointer to the string to be converted
38
37
      endptr   in/out   pointer to the end of the string/
39
38
                        pointer to the stop character
40
39
      error    out      returned error code
41
 
 
 
40
 
42
41
  DESCRIPTION
43
42
    This function takes the decimal representation of integer number
44
43
    from string nptr and converts it to an signed or unsigned
45
 
    long long integer value.
 
44
    int64_t value.
46
45
    Space characters and tab are ignored.
47
46
    A sign character might precede the digit characters. The number
48
47
    may have any number of pre-zero digits.
50
49
    The function stops reading the string nptr at the first character
51
50
    that is not a decimal digit. If endptr is not NULL then the function
52
51
    will not read characters after *endptr.
53
 
 
 
52
 
54
53
  RETURN VALUES
55
54
    Value of string as a signed/unsigned int64_t integer
56
55
 
61
60
    -1          Number was an ok negative number
62
61
    0           ok
63
62
    ERANGE      If the the value of the converted number exceeded the
64
 
                maximum negative/unsigned long long integer.
65
 
                In this case the return value is ~0 if value was
66
 
                positive and LONGLONG_MIN if value was negative.
 
63
                maximum negative/uint64_t integer.
 
64
                In this case the return value is UINT64_MAX if value was
 
65
                positive and UINT64_MIN if value was negative.
67
66
    EDOM        If the string didn't contain any digits. In this case
68
67
                the return value is 0.
69
68
 
80
79
  unsigned long i, j, k;
81
80
  uint64_t li;
82
81
  int negative;
83
 
  unsigned long cutoff, cutoff2, cutoff3;
 
82
  uint64_t cutoff, cutoff2, cutoff3;
84
83
 
85
84
  s= nptr;
86
85
  /* If fixed length string */
123
122
      if (++s == end)
124
123
        goto no_conv;
125
124
    }
126
 
    cutoff=  ULONGLONG_MAX / LFACTOR2;
127
 
    cutoff2= ULONGLONG_MAX % LFACTOR2 / 100;
128
 
    cutoff3=  ULONGLONG_MAX % 100;
 
125
    cutoff=  UINT64_MAX / LFACTOR2;
 
126
    cutoff2= UINT64_MAX % LFACTOR2 / 100;
 
127
    cutoff3=  UINT64_MAX % 100;
129
128
  }
130
129
 
131
130
  /* Handle case where we have a lot of pre-zero */
202
201
 
203
202
overflow:                                       /* *endptr is set here */
204
203
  *error= ERANGE;
205
 
  return negative ? INT64_MIN: (int64_t) ULONGLONG_MAX;
 
204
  return negative ? INT64_MIN: INT64_MAX;
206
205
 
207
206
end_i:
208
207
  *endptr= (char*) s;