51 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H10#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H11 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 17namespace LIBC_NAMESPACE_DECL {18namespace printf_core {19 20template <WriteMode write_mode>21LIBC_INLINE int convert_char(Writer<write_mode> *writer,22 const FormatSection &to_conv) {23 char c = static_cast<char>(to_conv.conv_val_raw);24 25 constexpr int STRING_LEN = 1;26 27 size_t padding_spaces =28 to_conv.min_width > STRING_LEN ? to_conv.min_width - STRING_LEN : 0;29 30 // If the padding is on the left side, write the spaces first.31 if (padding_spaces > 0 &&32 (to_conv.flags & FormatFlags::LEFT_JUSTIFIED) == 0) {33 RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_spaces));34 }35 36 RET_IF_RESULT_NEGATIVE(writer->write(c));37 38 // If the padding is on the right side, write the spaces last.39 if (padding_spaces > 0 &&40 (to_conv.flags & FormatFlags::LEFT_JUSTIFIED) != 0) {41 RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_spaces));42 }43 44 return WRITE_OK;45}46 47} // namespace printf_core48} // namespace LIBC_NAMESPACE_DECL49 50#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H51