xref: /aosp_15_r20/external/llvm-libc/src/stdio/printf_core/write_int_converter.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Write integer Converter for printf ----------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
11 
12 #include "src/__support/macros/config.h"
13 #include "src/stdio/printf_core/core_structs.h"
14 #include "src/stdio/printf_core/writer.h"
15 
16 #include <inttypes.h>
17 #include <stddef.h>
18 
19 namespace LIBC_NAMESPACE_DECL {
20 namespace printf_core {
21 
convert_write_int(Writer * writer,const FormatSection & to_conv)22 LIBC_INLINE int convert_write_int(Writer *writer,
23                                   const FormatSection &to_conv) {
24 
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
30 
31   int written = writer->get_chars_written();
32 
33   switch (to_conv.length_modifier) {
34   case LengthModifier::none:
35     *reinterpret_cast<int *>(to_conv.conv_val_ptr) = written;
36     break;
37   case LengthModifier::l:
38     *reinterpret_cast<long *>(to_conv.conv_val_ptr) = written;
39     break;
40   case LengthModifier::ll:
41   case LengthModifier::L:
42     *reinterpret_cast<long long *>(to_conv.conv_val_ptr) = written;
43     break;
44   case LengthModifier::h:
45     *reinterpret_cast<short *>(to_conv.conv_val_ptr) =
46         static_cast<short>(written);
47     break;
48   case LengthModifier::hh:
49     *reinterpret_cast<signed char *>(to_conv.conv_val_ptr) =
50         static_cast<signed char>(written);
51     break;
52   case LengthModifier::z:
53     *reinterpret_cast<size_t *>(to_conv.conv_val_ptr) = written;
54     break;
55   case LengthModifier::t:
56     *reinterpret_cast<ptrdiff_t *>(to_conv.conv_val_ptr) = written;
57     break;
58   case LengthModifier::j:
59   case LengthModifier::w:
60   case LengthModifier::wf:
61     *reinterpret_cast<uintmax_t *>(to_conv.conv_val_ptr) = written;
62     break;
63   }
64   return WRITE_OK;
65 }
66 
67 } // namespace printf_core
68 } // namespace LIBC_NAMESPACE_DECL
69 
70 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
71