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/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 #include <stddef.h> 17 18 namespace __llvm_libc { 19 namespace printf_core { 20 convert_string(Writer * writer,const FormatSection & to_conv)21int inline convert_string(Writer *writer, const FormatSection &to_conv) { 22 int string_len = 0; 23 24 for (char *cur_str = reinterpret_cast<char *>(to_conv.conv_val_ptr); 25 cur_str[string_len]; ++string_len) { 26 ; 27 } 28 29 if (to_conv.precision >= 0 && to_conv.precision < string_len) 30 string_len = to_conv.precision; 31 32 if (to_conv.min_width > string_len) { 33 if ((to_conv.flags & FormatFlags::LEFT_JUSTIFIED) == 34 FormatFlags::LEFT_JUSTIFIED) { 35 RET_IF_RESULT_NEGATIVE(writer->write( 36 reinterpret_cast<const char *>(to_conv.conv_val_ptr), string_len)); 37 RET_IF_RESULT_NEGATIVE( 38 writer->write_chars(' ', to_conv.min_width - string_len)); 39 40 } else { 41 RET_IF_RESULT_NEGATIVE( 42 writer->write_chars(' ', to_conv.min_width - string_len)); 43 RET_IF_RESULT_NEGATIVE(writer->write( 44 reinterpret_cast<const char *>(to_conv.conv_val_ptr), string_len)); 45 } 46 } else { 47 RET_IF_RESULT_NEGATIVE(writer->write( 48 reinterpret_cast<const char *>(to_conv.conv_val_ptr), string_len)); 49 } 50 return WRITE_OK; 51 } 52 53 } // namespace printf_core 54 } // namespace __llvm_libc 55 56 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_CONVERTER_H 57