[HLSL] Implement RWBuffer::operator[] via __builtin_hlsl_resource_getpointer (#117017)
[llvm-project.git] / libc / src / stdio / printf_core / char_converter.h
blob2596cba813c2e9a414fe4a1a30073e0d95a17fe2
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 //===----------------------------------------------------------------------===//
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));
42 return WRITE_OK;
45 } // namespace printf_core
46 } // namespace LIBC_NAMESPACE_DECL
48 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CHAR_CONVERTER_H