1 //===-- String 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_CHAR_CONVERTER_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H
12 #include "src/__support/macros/config.h"
13 #include "src/stdio/printf_core/converter_utils.h"
14 #include "src/stdio/printf_core/core_structs.h"
15 #include "src/stdio/printf_core/writer.h"
17 namespace LIBC_NAMESPACE_DECL
{
18 namespace printf_core
{
20 LIBC_INLINE
int convert_char(Writer
*writer
, const FormatSection
&to_conv
) {
21 char c
= static_cast<char>(to_conv
.conv_val_raw
);
23 constexpr int STRING_LEN
= 1;
25 size_t padding_spaces
=
26 to_conv
.min_width
> STRING_LEN
? to_conv
.min_width
- STRING_LEN
: 0;
28 // If the padding is on the left side, write the spaces first.
29 if (padding_spaces
> 0 &&
30 (to_conv
.flags
& FormatFlags::LEFT_JUSTIFIED
) == 0) {
31 RET_IF_RESULT_NEGATIVE(writer
->write(' ', padding_spaces
));
34 RET_IF_RESULT_NEGATIVE(writer
->write(c
));
36 // If the padding is on the right side, write the spaces last.
37 if (padding_spaces
> 0 &&
38 (to_conv
.flags
& FormatFlags::LEFT_JUSTIFIED
) != 0) {
39 RET_IF_RESULT_NEGATIVE(writer
->write(' ', padding_spaces
));
45 } // namespace printf_core
46 } // namespace LIBC_NAMESPACE_DECL
48 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H