1 //===-- Write integer Converter for printf ----------------------*- 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_PRINTF_CORE_WRITE_INT_CONVERTER_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
12 #include "src/__support/macros/config.h"
13 #include "src/stdio/printf_core/core_structs.h"
14 #include "src/stdio/printf_core/writer.h"
19 namespace LIBC_NAMESPACE_DECL
{
20 namespace printf_core
{
22 LIBC_INLINE
int convert_write_int(Writer
*writer
,
23 const FormatSection
&to_conv
) {
25 #ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
26 // This is an additional check added by LLVM-libc.
27 if (to_conv
.conv_val_ptr
== nullptr)
28 return NULLPTR_WRITE_ERROR
;
29 #endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
31 int written
= writer
->get_chars_written();
33 switch (to_conv
.length_modifier
) {
34 case LengthModifier::none
:
35 *reinterpret_cast<int *>(to_conv
.conv_val_ptr
) = written
;
37 case LengthModifier::l
:
38 *reinterpret_cast<long *>(to_conv
.conv_val_ptr
) = written
;
40 case LengthModifier::ll
:
41 case LengthModifier::L
:
42 *reinterpret_cast<long long *>(to_conv
.conv_val_ptr
) = written
;
44 case LengthModifier::h
:
45 *reinterpret_cast<short *>(to_conv
.conv_val_ptr
) =
46 static_cast<short>(written
);
48 case LengthModifier::hh
:
49 *reinterpret_cast<signed char *>(to_conv
.conv_val_ptr
) =
50 static_cast<signed char>(written
);
52 case LengthModifier::z
:
53 *reinterpret_cast<size_t *>(to_conv
.conv_val_ptr
) = written
;
55 case LengthModifier::t
:
56 *reinterpret_cast<ptrdiff_t *>(to_conv
.conv_val_ptr
) = written
;
58 case LengthModifier::j
:
59 case LengthModifier::w
:
60 case LengthModifier::wf
:
61 *reinterpret_cast<uintmax_t *>(to_conv
.conv_val_ptr
) = written
;
67 } // namespace printf_core
68 } // namespace LIBC_NAMESPACE_DECL
70 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H