1 //===-- Core Structures 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 #ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_CORE_STRUCTS_H
10 #define LLVM_LIBC_SRC_STDIO_SCANF_CORE_CORE_STRUCTS_H
12 #include "src/__support/CPP/bitset.h"
13 #include "src/__support/CPP/string_view.h"
14 #include "src/__support/FPUtil/FPBits.h"
19 namespace LIBC_NAMESPACE
{
20 namespace scanf_core
{
22 // These length modifiers match the length modifiers in the format string, which
23 // is why they are formatted differently from the rest of the file.
24 enum class LengthModifier
{ hh
, h
, l
, ll
, j
, z
, t
, L
, NONE
};
26 enum FormatFlags
: uint8_t {
32 struct FormatSection
{
35 cpp::string_view raw_string
;
37 // Format Specifier Values
38 FormatFlags flags
= FormatFlags::NONE
;
39 LengthModifier length_modifier
= LengthModifier::NONE
;
42 // output_ptr is nullptr if and only if the NO_WRITE flag is set.
43 void *output_ptr
= nullptr;
47 cpp::bitset
<256> scan_set
;
49 bool operator==(const FormatSection
&other
) {
50 if (has_conv
!= other
.has_conv
)
53 if (raw_string
!= other
.raw_string
)
57 if (!((static_cast<uint8_t>(flags
) ==
58 static_cast<uint8_t>(other
.flags
)) &&
59 (max_width
== other
.max_width
) &&
60 (length_modifier
== other
.length_modifier
) &&
61 (conv_name
== other
.conv_name
)))
64 // If the pointers are used, then they should be equal. If the NO_WRITE
65 // flag is set or the conversion is %, then the pointers are not used.
66 // If the pointers are used and they are not equal, return false.
68 if (!(((flags
& FormatFlags::NO_WRITE
) != 0) || (conv_name
== '%') ||
69 (output_ptr
== other
.output_ptr
)))
73 return scan_set
== other
.scan_set
;
79 enum ErrorCodes
: int {
80 // This is the value to be returned by conversions when no error has occurred.
82 // These are the scanf return values for when an error has occurred. They are
83 // all negative, and should be distinct.
85 FILE_STATUS_ERROR
= -2,
86 MATCHING_FAILURE
= -3,
87 ALLOCATION_FAILURE
= -4,
89 } // namespace scanf_core
90 } // namespace LIBC_NAMESPACE
92 #endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_CORE_STRUCTS_H