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/__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" 16 17 namespace LIBC_NAMESPACE_DECL { 18 namespace printf_core { 19 20 LIBC_INLINE int convert_char(Writer *writer, const FormatSection &to_conv) { 21 char c = static_cast<char>(to_conv.conv_val_raw); 22 23 constexpr int STRING_LEN = 1; 24 25 size_t padding_spaces = 26 to_conv.min_width > STRING_LEN ? to_conv.min_width - STRING_LEN : 0; 27 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)); 32 } 33 34 RET_IF_RESULT_NEGATIVE(writer->write(c)); 35 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)); 40 } 41 42 return WRITE_OK; 43 } 44 45 } // namespace printf_core 46 } // namespace LIBC_NAMESPACE_DECL 47 48 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H 49