1 //===-- Int type specifier converters for scanf -----------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/stdio/scanf_core/int_converter.h"
11 #include "src/__support/CPP/limits.h"
12 #include "src/__support/ctype_utils.h"
13 #include "src/stdio/scanf_core/converter_utils.h"
14 #include "src/stdio/scanf_core/core_structs.h"
15 #include "src/stdio/scanf_core/reader.h"
19 namespace LIBC_NAMESPACE
{
20 namespace scanf_core
{
22 // This code is very similar to the code in __support/str_to_integer.h but is
23 // not quite the same. Here is the list of differences and why they exist:
24 // 1) This takes a reader and a format section instead of a char* and the base.
25 // This should be fairly self explanatory. While the char* could be adapted
26 // to a reader and the base could be calculated ahead of time, the
27 // semantics are slightly different, specifically a char* can be indexed
28 // freely (I can read str[2] and then str[0]) whereas a File (which the
29 // reader may contain) cannot.
30 // 2) Because this uses a Reader, this function can only unget once.
31 // This is relevant because scanf specifies it reads the "longest sequence
32 // of input characters which does not exceed any specified field width and
33 // which is, or is a prefix of, a matching input sequence." Whereas the
34 // strtol function accepts "the longest initial subsequence of the input
35 // string (...) that is of the expected form." This is demonstrated by the
36 // differences in how they deal with the string "0xZZZ" when parsing as
37 // hexadecimal. Scanf will read the "0x" as a valid prefix and return 0,
38 // since it reads the first 'Z', sees that it's not a valid hex digit, and
39 // reverses one character. The strtol function on the other hand only
40 // accepts the "0" since that's the longest valid hexadecimal sequence. It
41 // sees the 'Z' after the "0x" and determines that this is not the prefix
42 // to a valid hex string.
43 // 3) This conversion may have a maximum width.
44 // If a maximum width is specified, this conversion is only allowed to
45 // accept a certain number of characters. Strtol doesn't have any such
47 int convert_int(Reader
*reader
, const FormatSection
&to_conv
) {
48 // %d "Matches an optionally signed decimal integer [...] with the value 10
49 // for the base argument. The corresponding argument shall be a pointer to
52 // %i "Matches an optionally signed integer [...] with the value 0 for the
53 // base argument. The corresponding argument shall be a pointer to signed
56 // %u "Matches an optionally signed decimal integer [...] with the value 10
57 // for the base argument. The corresponding argument shall be a pointer to
60 // %o "Matches an optionally signed octal integer [...] with the value 8 for
61 // the base argument. The corresponding argument shall be a pointer to
64 // %x/X "Matches an optionally signed hexadecimal integer [...] with the value
65 // 16 for the base argument. The corresponding argument shall be a pointer to
68 size_t max_width
= cpp::numeric_limits
<size_t>::max();
69 if (to_conv
.max_width
> 0) {
70 max_width
= to_conv
.max_width
;
74 bool is_number
= false;
75 bool is_signed
= false;
77 if (to_conv
.conv_name
== 'i') {
80 } else if (to_conv
.conv_name
== 'o') {
82 } else if (to_lower(to_conv
.conv_name
) == 'x' || to_conv
.conv_name
== 'p') {
84 } else if (to_conv
.conv_name
== 'd') {
87 } else { // conv_name must be 'u'
91 char cur_char
= reader
->getc();
93 char result_sign
= '+';
94 if (cur_char
== '+' || cur_char
== '-') {
95 result_sign
= cur_char
;
98 cur_char
= reader
->getc();
100 // If the max width has been hit already, then the return value must be 0
101 // since no actual digits of the number have been parsed yet.
102 write_int_with_length(0, to_conv
);
103 return MATCHING_FAILURE
;
106 const bool is_negative
= result_sign
== '-';
108 // Base of 0 means automatically determine the base. Base of 16 may have a
110 if (base
== 0 || base
== 16) {
111 // If the first character is 0, then it could be octal or hex.
112 if (cur_char
== '0') {
115 // Read the next character to check.
118 cur_char
= reader
->getc();
120 write_int_with_length(0, to_conv
);
124 if (to_lower(cur_char
) == 'x') {
125 // This is a valid hex prefix.
129 cur_char
= reader
->getc();
131 write_int_with_length(0, to_conv
);
140 } else if (base
== 0) {
141 if (internal::isdigit(cur_char
)) {
142 // If the first character is a different number, then it's 10.
145 // If the first character isn't a valid digit, then there are no valid
146 // digits at all. The number is 0.
147 reader
->ungetc(cur_char
);
148 write_int_with_length(0, to_conv
);
149 return MATCHING_FAILURE
;
154 constexpr uintmax_t UNSIGNED_MAX
= cpp::numeric_limits
<uintmax_t>::max();
155 constexpr uintmax_t SIGNED_MAX
=
156 static_cast<uintmax_t>(cpp::numeric_limits
<intmax_t>::max());
157 constexpr uintmax_t NEGATIVE_SIGNED_MAX
=
158 static_cast<uintmax_t>(cpp::numeric_limits
<intmax_t>::max()) + 1;
160 const uintmax_t MAX
=
161 (is_signed
? (is_negative
? NEGATIVE_SIGNED_MAX
: SIGNED_MAX
)
164 const uintmax_t max_div_by_base
= MAX
/ base
;
166 if (internal::isalnum(cur_char
) && b36_char_to_int(cur_char
) < base
) {
170 bool has_overflow
= false;
172 for (; i
< max_width
&& internal::isalnum(cur_char
) &&
173 b36_char_to_int(cur_char
) < base
;
174 ++i
, cur_char
= reader
->getc()) {
176 uintmax_t cur_digit
= b36_char_to_int(cur_char
);
181 } else if (result
> max_div_by_base
) {
185 result
= result
* base
;
188 if (result
> MAX
- cur_digit
) {
192 result
= result
+ cur_digit
;
196 // We always read one more character than will be used, so we have to put the
198 reader
->ungetc(cur_char
);
201 write_int_with_length(MAX
, to_conv
);
206 write_int_with_length(result
, to_conv
);
210 return MATCHING_FAILURE
;
214 } // namespace scanf_core
215 } // namespace LIBC_NAMESPACE