]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/StdLib/NumericInt.c
2 Integer Numeric Conversion Functions.
4 The atoi, atol, and atoll functions convert the initial portion of the string
5 pointed to by nptr to int, long int, and long long int representation,
6 respectively. They are equivalent to:
7 - atoi: (int)strtol(nptr, (char **)NULL, 10)
8 - atol: strtol(nptr, (char **)NULL, 10)
9 - atoll: strtoll(nptr, (char **)NULL, 10)
11 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
12 This program and the accompanying materials are licensed and made available under
13 the terms and conditions of the BSD License that accompanies this distribution.
14 The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php.
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Library/BaseLib.h>
23 #include <LibConfig.h>
30 /** The atoi function converts the initial portion of the string pointed to by
31 nptr to int representation. Except for the behavior on error, it is
33 - (int)strtol(nptr, (char **)NULL, 10)
35 @return The atoi function returns the converted value.
38 atoi(const char *nptr
)
41 BOOLEAN Negative
= FALSE
;
43 while(isspace((const unsigned char)*nptr
)) ++nptr
; // Skip leading spaces
49 else if(*nptr
== '-') {
53 Retval
= (int)AsciiStrDecimalToUintn(nptr
);
60 /** The atol function converts the initial portion of the string pointed to by
61 nptr to long int representation. Except for the behavior on error, it is
63 - strtol(nptr, (char **)NULL, 10)
65 @return The atol function returns the converted value.
68 atol(const char *nptr
)
71 BOOLEAN Negative
= FALSE
;
73 while(isspace(*nptr
)) ++nptr
; // Skip leading spaces
79 else if(*nptr
== '-') {
83 Retval
= (long int)AsciiStrDecimalToUint64(nptr
);
90 /** The atoll function converts the initial portion of the string pointed to by
91 nptr to long long int representation. Except for the behavior on error, it
93 - strtoll(nptr, (char **)NULL, 10)
95 @return The atoll function returns the converted value.
98 atoll(const char *nptr
)
100 long long int Retval
;
101 BOOLEAN Negative
= FALSE
;
103 while(isspace(*nptr
)) ++nptr
; // Skip leading spaces
109 else if(*nptr
== '-') {
113 Retval
= (long long int)AsciiStrDecimalToUint64(nptr
);
123 if(isalpha(c
)) { /* If c is one of [A-Za-z]... */
124 c
= toupper(c
) - 7; // Adjust so 'A' is ('9' + 1)
126 return c
- '0'; // Value returned is between 0 and 35, inclusive.
129 /** The strtol, strtoll, strtoul, and strtoull functions convert the initial
130 portion of the string pointed to by nptr to long int, long long int,
131 unsigned long int, and unsigned long long int representation, respectively.
132 First, they decompose the input string into three parts: an initial,
133 possibly empty, sequence of white-space characters (as specified by the
134 isspace function), a subject sequence resembling an integer represented in
135 some radix determined by the value of base, and a final string of one or
136 more unrecognized characters, including the terminating null character of
137 the input string. Then, they attempt to convert the subject sequence to an
138 integer, and return the result.
140 If the value of base is zero, the expected form of the subject sequence is
141 that of an integer constant, optionally preceded
142 by a plus or minus sign, but not including an integer suffix. If the value
143 of base is between 2 and 36 (inclusive), the expected form of the subject
144 sequence is a sequence of letters and digits representing an integer with
145 the radix specified by base, optionally preceded by a plus or minus sign,
146 but not including an integer suffix. The letters from a (or A) through z
147 (or Z) are ascribed the values 10 through 35; only letters and digits whose
148 ascribed values are less than that of base are permitted. If the value of
149 base is 16, the characters 0x or 0X may optionally precede the sequence of
150 letters and digits, following the sign if present.
152 The subject sequence is defined as the longest initial subsequence of the
153 input string, starting with the first non-white-space character, that is of
154 the expected form. The subject sequence contains no characters if the input
155 string is empty or consists entirely of white space, or if the first
156 non-white-space character is other than a sign or a permissible letter or digit.
158 If the subject sequence has the expected form and the value of base is
159 zero, the sequence of characters starting with the first digit is
160 interpreted as an integer constant. If the subject sequence has the
161 expected form and the value of base is between 2 and 36, it is used as the
162 base for conversion, ascribing to each letter its value as given above. If
163 the subject sequence begins with a minus sign, the value resulting from the
164 conversion is negated (in the return type). A pointer to the final string
165 is stored in the object pointed to by endptr, provided that endptr is
168 In other than the "C" locale, additional locale-specific subject sequence
169 forms may be accepted.
171 If the subject sequence is empty or does not have the expected form, no
172 conversion is performed; the value of nptr is stored in the object pointed
173 to by endptr, provided that endptr is not a null pointer.
175 @return The strtol, strtoll, strtoul, and strtoull functions return the
176 converted value, if any. If no conversion could be performed, zero
177 is returned. If the correct value is outside the range of
178 representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX,
179 ULONG_MAX, or ULLONG_MAX is returned (according to the return type
180 and sign of the value, if any), and the value of the macro ERANGE
184 strtol(const char * __restrict nptr
, char ** __restrict endptr
, int base
)
190 BOOLEAN Negative
= FALSE
;
194 if((base
< 0) || (base
== 1) || (base
> 36)) {
200 // Skip leading spaces.
201 while(isspace(*nptr
)) ++nptr
;
203 // Process Subject sequence: optional sign followed by digits.
208 else if(*nptr
== '-') {
213 if(*nptr
== '0') { /* Might be Octal or Hex */
214 if(toupper(nptr
[1]) == 'X') { /* Looks like Hex */
215 if((base
== 0) || (base
== 16)) {
216 nptr
+= 2; /* Skip the "0X" */
217 base
= 16; /* In case base was 0 */
220 else { /* Looks like Octal */
221 if((base
== 0) || (base
== 8)) {
222 ++nptr
; /* Skip the leading "0" */
223 base
= 8; /* In case base was 0 */
227 if(base
== 0) { /* If still zero then must be decimal */
231 for( ; *nptr
== '0'; ++nptr
); /* Skip any remaining leading zeros */
235 while( isalnum(*nptr
) && ((temp
= Digit2Val(*nptr
)) < base
)) {
237 Result
= (Result
* base
) + (long int)temp
;
238 if( Result
<= Previous
) { // Detect Overflow
255 // Save pointer to final sequence
257 *endptr
= (char *)pEnd
;
262 /** The strtoul function converts the initial portion of the string pointed to
263 by nptr to unsigned long int representation.
265 See the description for strtol for more information.
267 @return The strtoul function returns the converted value, if any. If no
268 conversion could be performed, zero is returned. If the correct
269 value is outside the range of representable values, ULONG_MAX is
270 returned and the value of the macro ERANGE is stored in errno.
273 strtoul(const char * __restrict nptr
, char ** __restrict endptr
, int base
)
276 unsigned long Result
= 0;
277 unsigned long Previous
;
282 if((base
< 0) || (base
== 1) || (base
> 36)) {
288 // Skip leading spaces.
289 while(isspace(*nptr
)) ++nptr
;
291 // Process Subject sequence: optional + sign followed by digits.
296 if(*nptr
== '0') { /* Might be Octal or Hex */
297 if(toupper(nptr
[1]) == 'X') { /* Looks like Hex */
298 if((base
== 0) || (base
== 16)) {
299 nptr
+= 2; /* Skip the "0X" */
300 base
= 16; /* In case base was 0 */
303 else { /* Looks like Octal */
304 if((base
== 0) || (base
== 8)) {
305 ++nptr
; /* Skip the leading "0" */
306 base
= 8; /* In case base was 0 */
310 if(base
== 0) { /* If still zero then must be decimal */
314 for( ; *nptr
== '0'; ++nptr
); /* Skip any remaining leading zeros */
318 while( isalnum(*nptr
) && ((temp
= Digit2Val(*nptr
)) < base
)) {
320 Result
= (Result
* base
) + (unsigned long)temp
;
321 if( Result
< Previous
) { // If we overflowed
329 // Save pointer to final sequence
331 *endptr
= (char *)pEnd
;
336 /** The strtoll function converts the initial portion of the string pointed to
337 by nptr to long long int representation.
339 See the description for strtol for more information.
341 @return The strtoll function returns the converted value, if any. If no
342 conversion could be performed, zero is returned. If the correct
343 value is outside the range of representable values, LLONG_MIN or
344 LLONG_MAX is returned (according to the sign of the value, if any),
345 and the value of the macro ERANGE is stored in errno.
348 strtoll(const char * __restrict nptr
, char ** __restrict endptr
, int base
)
351 long long Result
= 0;
354 BOOLEAN Negative
= FALSE
;
358 if((base
< 0) || (base
== 1) || (base
> 36)) {
364 // Skip leading spaces.
365 while(isspace(*nptr
)) ++nptr
;
367 // Process Subject sequence: optional sign followed by digits.
372 else if(*nptr
== '-') {
377 if(*nptr
== '0') { /* Might be Octal or Hex */
378 if(toupper(nptr
[1]) == 'X') { /* Looks like Hex */
379 if((base
== 0) || (base
== 16)) {
380 nptr
+= 2; /* Skip the "0X" */
381 base
= 16; /* In case base was 0 */
384 else { /* Looks like Octal */
385 if((base
== 0) || (base
== 8)) {
386 ++nptr
; /* Skip the leading "0" */
387 base
= 8; /* In case base was 0 */
391 if(base
== 0) { /* If still zero then must be decimal */
395 for( ; *nptr
== '0'; ++nptr
); /* Skip any remaining leading zeros */
399 while( isalnum(*nptr
) && ((temp
= Digit2Val(*nptr
)) < base
)) {
401 Result
= (Result
* base
) + (long long int)temp
;
402 if( Result
<= Previous
) { // Detect Overflow
419 // Save pointer to final sequence
421 *endptr
= (char *)pEnd
;
426 /** The strtoull function converts the initial portion of the string pointed to
427 by nptr to unsigned long long int representation.
429 See the description for strtol for more information.
431 @return The strtoull function returns the converted value, if any. If no
432 conversion could be performed, zero is returned. If the correct
433 value is outside the range of representable values, ULLONG_MAX is
434 returned and the value of the macro ERANGE is stored in errno.
437 strtoull(const char * __restrict nptr
, char ** __restrict endptr
, int base
)
440 unsigned long long Result
= 0;
441 unsigned long long Previous
;
446 if((base
< 0) || (base
== 1) || (base
> 36)) {
452 // Skip leading spaces.
453 while(isspace(*nptr
)) ++nptr
;
455 // Process Subject sequence: optional + sign followed by digits.
460 if(*nptr
== '0') { /* Might be Octal or Hex */
461 if(toupper(nptr
[1]) == 'X') { /* Looks like Hex */
462 if((base
== 0) || (base
== 16)) {
463 nptr
+= 2; /* Skip the "0X" */
464 base
= 16; /* In case base was 0 */
467 else { /* Looks like Octal */
468 if((base
== 0) || (base
== 8)) {
469 ++nptr
; /* Skip the leading "0" */
470 base
= 8; /* In case base was 0 */
474 if(base
== 0) { /* If still zero then must be decimal */
478 for( ; *nptr
== '0'; ++nptr
); /* Skip any remaining leading zeros */
482 while( isalnum(*nptr
) && ((temp
= Digit2Val(*nptr
)) < base
)) {
484 Result
= (Result
* base
) + (unsigned long long)temp
;
485 if( Result
< Previous
) { // If we overflowed
493 // Save pointer to final sequence
495 *endptr
= (char *)pEnd
;