Warning: This file is not a C or C++ file. It does not have highlighting.
| 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_STRING_CONVERTER_H |
| 10 | #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_CONVERTER_H |
| 11 | |
| 12 | #include "src/__support/CPP/string_view.h" |
| 13 | #include "src/__support/macros/config.h" |
| 14 | #include "src/stdio/printf_core/converter_utils.h" |
| 15 | #include "src/stdio/printf_core/core_structs.h" |
| 16 | #include "src/stdio/printf_core/writer.h" |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | |
| 20 | namespace LIBC_NAMESPACE_DECL { |
| 21 | namespace printf_core { |
| 22 | |
| 23 | template <WriteMode write_mode> |
| 24 | LIBC_INLINE int convert_string(Writer<write_mode> *writer, |
| 25 | const FormatSection &to_conv) { |
| 26 | size_t string_len = 0; |
| 27 | const char *str_ptr = reinterpret_cast<const char *>(to_conv.conv_val_ptr); |
| 28 | |
| 29 | #ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS |
| 30 | if (str_ptr == nullptr) { |
| 31 | str_ptr = "(null)"; |
| 32 | } |
| 33 | #endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS |
| 34 | |
| 35 | for (const char *cur_str = (str_ptr); cur_str[string_len]; ++string_len) { |
| 36 | ; |
| 37 | } |
| 38 | |
| 39 | if (to_conv.precision >= 0 && |
| 40 | static_cast<size_t>(to_conv.precision) < string_len) |
| 41 | string_len = to_conv.precision; |
| 42 | |
| 43 | size_t padding_spaces = to_conv.min_width > static_cast<int>(string_len) |
| 44 | ? to_conv.min_width - string_len |
| 45 | : 0; |
| 46 | |
| 47 | // If the padding is on the left side, write the spaces first. |
| 48 | if (padding_spaces > 0 && |
| 49 | (to_conv.flags & FormatFlags::LEFT_JUSTIFIED) == 0) { |
| 50 | RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_spaces)); |
| 51 | } |
| 52 | |
| 53 | RET_IF_RESULT_NEGATIVE(writer->write({(str_ptr), string_len})); |
| 54 | |
| 55 | // If the padding is on the right side, write the spaces last. |
| 56 | if (padding_spaces > 0 && |
| 57 | (to_conv.flags & FormatFlags::LEFT_JUSTIFIED) != 0) { |
| 58 | RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_spaces)); |
| 59 | } |
| 60 | return WRITE_OK; |
| 61 | } |
| 62 | |
| 63 | } // namespace printf_core |
| 64 | } // namespace LIBC_NAMESPACE_DECL |
| 65 | |
| 66 | #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_CONVERTER_H |
| 67 |
Warning: This file is not a C or C++ file. It does not have highlighting.
