]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/StdLib/NumericInt.c
058ad04959ba30c8fa3ccc9488d7e431814cd76d
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, 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(__isHexLetter(c
)) { /* If c is one of [A-Fa-f]... */
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
)
189 BOOLEAN Negative
= FALSE
;
191 if((base
< 0) || (base
== 1) || (base
> 36)) {
195 // Skip leading spaces.
196 while(isspace(*nptr
)) ++nptr
;
198 // Process Subject sequence: optional sign followed by digits.
203 else if(*nptr
== '-') {
207 if( (base
== 16) && (*nptr
== '0') && (toupper(nptr
[1]) == 'X')) {
210 while( isalnum(*nptr
) && ((temp
= Digit2Val(*nptr
)) < base
)) {
212 Result
= (Result
* base
) + (long int)temp
;
213 if( Result
<= Previous
) { // Detect Overflow
230 // Save pointer to final sequence
231 if( endptr
!= NULL
) {
232 *endptr
= (char *)nptr
;
237 /** The strtoul function converts the initial portion of the string pointed to
238 by nptr to unsigned long int representation.
240 See the description for strtol for more information.
242 @return The strtoul function returns the converted value, if any. If no
243 conversion could be performed, zero is returned. If the correct
244 value is outside the range of representable values, ULONG_MAX is
245 returned and the value of the macro ERANGE is stored in errno.
248 strtoul(const char * __restrict nptr
, char ** __restrict endptr
, int base
)
250 unsigned long Result
= 0;
251 unsigned long Previous
;
254 if((base
< 0) || (base
== 1) || (base
> 36)) {
258 // Skip leading spaces.
259 while(isspace(*nptr
)) ++nptr
;
261 // Process Subject sequence: optional + sign followed by digits.
265 if( (base
== 16) && (*nptr
== '0') && (toupper(nptr
[1]) == 'X')) {
268 while( isalnum(*nptr
) && ((temp
= Digit2Val(*nptr
)) < base
)) {
270 Result
= (Result
* base
) + (unsigned long)temp
;
271 if( Result
< Previous
) { // If we overflowed
279 // Save pointer to final sequence
280 if( endptr
!= NULL
) {
281 *endptr
= (char *)nptr
;
286 /** The strtoll function converts the initial portion of the string pointed to
287 by nptr to long long int representation.
289 See the description for strtol for more information.
291 @return The strtoll function returns the converted value, if any. If no
292 conversion could be performed, zero is returned. If the correct
293 value is outside the range of representable values, LLONG_MIN or
294 LLONG_MAX is returned (according to the sign of the value, if any),
295 and the value of the macro ERANGE is stored in errno.
298 strtoll(const char * __restrict nptr
, char ** __restrict endptr
, int base
)
300 long long Result
= 0;
303 BOOLEAN Negative
= FALSE
;
305 if((base
< 0) || (base
== 1) || (base
> 36)) {
309 // Skip leading spaces.
310 while(isspace(*nptr
)) ++nptr
;
312 // Process Subject sequence: optional sign followed by digits.
317 else if(*nptr
== '-') {
321 if( (base
== 16) && (*nptr
== '0') && (toupper(nptr
[1]) == 'X')) {
324 while( isalnum(*nptr
) && ((temp
= Digit2Val(*nptr
)) < base
)) {
326 Result
= (Result
* base
) + (long long int)temp
;
327 if( Result
<= Previous
) { // Detect Overflow
344 // Save pointer to final sequence
345 if( endptr
!= NULL
) {
346 *endptr
= (char *)nptr
;
351 /** The strtoull function converts the initial portion of the string pointed to
352 by nptr to unsigned long long int representation.
354 See the description for strtol for more information.
356 @return The strtoull function returns the converted value, if any. If no
357 conversion could be performed, zero is returned. If the correct
358 value is outside the range of representable values, ULLONG_MAX is
359 returned and the value of the macro ERANGE is stored in errno.
362 strtoull(const char * __restrict nptr
, char ** __restrict endptr
, int base
)
364 unsigned long long Result
= 0;
365 unsigned long long Previous
;
368 if((base
< 0) || (base
== 1) || (base
> 36)) {
372 // Skip leading spaces.
373 while(isspace(*nptr
)) ++nptr
;
375 // Process Subject sequence: optional + sign followed by digits.
379 if( (base
== 16) && (*nptr
== '0') && (toupper(nptr
[1]) == 'X')) {
382 while( isalnum(*nptr
) && ((temp
= Digit2Val(*nptr
)) < base
)) {
384 Result
= (Result
* base
) + (unsigned long long)temp
;
385 if( Result
< Previous
) { // If we overflowed
393 // Save pointer to final sequence
394 if( endptr
!= NULL
) {
395 *endptr
= (char *)nptr
;