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