Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strtoull.c File Reference
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
Include dependency graph for strtoull.c:

Go to the source code of this file.

Functions

unsigned long long strtoull (const char *__restrict nptr, char **__restrict endptr, int base)
 Interprets an unsigned long long value in a byte string pointed to by str. More...
 

Function Documentation

◆ strtoull()

unsigned long long strtoull ( const char *__restrict  str,
char **__restrict  str_end,
int  base 
)

Interprets an unsigned long long value in a byte string pointed to by str.

Interprets an unsigned long long value in a byte string pointed to by str. Discards any whitespace characters (as identified by calling

See also
isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) long number representation and converts them to an unsigned long long value. The valid long value consists of the following parts:
 1) (optional) plus or minus sign
 2) (optional) prefix (0) indicating octal base (applies only when the base is 8 or ​0​)
 3) (optional) prefix (0x or 0X) indicating hexadecimal base (applies only when the base is
16 or ​0​) 4) a sequence of digits

The set of valid values for base is {0,2,3,...,36}. The set of valid digits for base-2 integers is {0,1}, for base-3 integers is {0,1,2}, and so on. For bases larger than 10, valid digits include alphabetic characters, starting from Aa for base-11 integer, to Zz for base-36 integer. The case of the characters is ignored.

If the value of base is ​0​, the numeric base is auto-detected: if the prefix is 0, the base is octal, if the prefix is 0x or 0X, the base is hexadecimal, otherwise the base is decimal.

If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if by unary minus in the result type.

If the str is empty or does not have the expected form, no conversion is performed, and (if str_end is not NULL) the value of str is stored in the object pointed to by str_end.

Parameters
strpointer to the null-terminated byte string to be interpreted
str_endpointer to a pointer to character.
basebase of the interpreted integer value
Returns
returns the result of the conversion, unless the value would underflow or overflow. If the converted value falls out of range of corresponding return type, a range error occurs (setting errno to ERANGE) and LONG_MAX, LONG_MIN, LLONG_MAX or LLONG_MIN is returned. If no conversion can be performed, ​0​ is returned.

Definition at line 44 of file strtoull.c.

45 {
46  const char* s;
47  unsigned long long acc;
48  char c;
49  unsigned long long cutoff;
50  int neg, any, cutlim;
51 
52  /*
53  * See strtoq for comments as to the logic used.
54  */
55  s = nptr;
56  do
57  {
58  c = *s++;
59  } while(isspace((unsigned char)c));
60  if(c == '-')
61  {
62  neg = 1;
63  c = *s++;
64  }
65  else
66  {
67  neg = 0;
68  if(c == '+')
69  {
70  {
71  c = *s++;
72  }
73  }
74  }
75  if((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X'))
76  {
77  c = s[1];
78  s += 2;
79  base = 16;
80  }
81  if(base == 0)
82  {
83  {
84  base = c == '0' ? 8 : 10;
85  }
86  }
87  acc = any = 0;
88  if(base < 2 || base > 36)
89  {
90  {
91  goto noconv;
92  }
93  }
94 
95  cutoff = ULLONG_MAX / (unsigned long long)base;
96  cutlim = (int)(ULLONG_MAX % (unsigned long long)base);
97  for(;; c = *s++)
98  {
99  if(c >= '0' && c <= '9')
100  {
101  {
102  c -= '0';
103  }
104  }
105  else if(c >= 'A' && c <= 'Z')
106  {
107  {
108  c -= 'A' - 10;
109  }
110  }
111  else if(c >= 'a' && c <= 'z')
112  {
113  {
114  c -= 'a' - 10;
115  }
116  }
117  else
118  {
119  {
120  break;
121  }
122  }
123  if(c >= base)
124  {
125  {
126  break;
127  }
128  }
129  if(any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
130  {
131  {
132  any = -1;
133  }
134  }
135  else
136  {
137  any = 1;
138  acc *= (unsigned long)base;
139  acc += (unsigned long)c;
140  }
141  }
142  if(any < 0)
143  {
144  acc = ULLONG_MAX;
145  // errno = ERANGE;
146  }
147  else if(!any)
148  {
149  noconv:
150  // PJ: nonstandard, but since no errno, acc = 0
151  acc = 0;
152  // errno = EINVAL;
153  }
154  else if(neg)
155  {
156  acc = -acc;
157  }
158  if(endptr != NULL)
159  {
160  *endptr = (char*)(uintptr_t)(any ? s - 1 : nptr);
161  }
162  return (acc);
163 }
#define NULL
Definition: stddef.h:15
int isspace(int ch)
Checks if the given character is a whitespace character.
Definition: isspace.c:5
#define ULLONG_MAX
Definition: limits.h:67

References isspace(), NULL, and ULLONG_MAX.