//===-- Int type specifier converters for scanf -----------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "src/stdio/scanf_core/int_converter.h" #include "src/__support/CPP/limits.h" #include "src/__support/ctype_utils.h" #include "src/__support/macros/config.h" #include "src/stdio/scanf_core/converter_utils.h" #include "src/stdio/scanf_core/core_structs.h" #include "src/stdio/scanf_core/reader.h" #include <stddef.h> namespace LIBC_NAMESPACE_DECL { namespace scanf_core { // This code is very similar to the code in __support/str_to_integer.h but is // not quite the same. Here is the list of differences and why they exist: // 1) This takes a reader and a format section instead of a char* and the base. // This should be fairly self explanatory. While the char* could be adapted // to a reader and the base could be calculated ahead of time, the // semantics are slightly different, specifically a char* can be indexed // freely (I can read str[2] and then str[0]) whereas a File (which the // reader may contain) cannot. // 2) Because this uses a Reader, this function can only unget once. // This is relevant because scanf specifies it reads the "longest sequence // of input characters which does not exceed any specified field width and // which is, or is a prefix of, a matching input sequence." Whereas the // strtol function accepts "the longest initial subsequence of the input // string (...) that is of the expected form." This is demonstrated by the // differences in how they deal with the string "0xZZZ" when parsing as // hexadecimal. Scanf will read the "0x" as a valid prefix and return 0, // since it reads the first 'Z', sees that it's not a valid hex digit, and // reverses one character. The strtol function on the other hand only // accepts the "0" since that's the longest valid hexadecimal sequence. It // sees the 'Z' after the "0x" and determines that this is not the prefix // to a valid hex string. // 3) This conversion may have a maximum width. // If a maximum width is specified, this conversion is only allowed to // accept a certain number of characters. Strtol doesn't have any such // limitation. int convert_int(Reader *reader, const FormatSection &to_conv) { … } } // namespace scanf_core } // namespace LIBC_NAMESPACE_DECL