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 #include "src/stdio/printf_core/core_structs.h"
10 #include "src/stdio/printf_core/writer.h"
11 
12 #include <stddef.h>
13 
14 namespace __llvm_libc {
15 namespace printf_core {
16 
17 void convert_string(Writer *writer, const FormatSection &to_conv) {
18   int string_len = 0;
19 
20   for (char *cur_str = reinterpret_cast<char *>(to_conv.conv_val_ptr);
21        cur_str[string_len]; ++string_len) {
22     ;
23   }
24 
25   if (to_conv.precision >= 0 && to_conv.precision < string_len)
26     string_len = to_conv.precision;
27 
28   if (to_conv.min_width > string_len) {
29     if ((to_conv.flags & FormatFlags::LEFT_JUSTIFIED) ==
30         FormatFlags::LEFT_JUSTIFIED) {
31       writer->write(reinterpret_cast<const char *>(to_conv.conv_val_ptr),
32                     string_len);
33       writer->write_chars(' ', to_conv.min_width - string_len);
34     } else {
35       writer->write_chars(' ', to_conv.min_width - string_len);
36       writer->write(reinterpret_cast<const char *>(to_conv.conv_val_ptr),
37                     string_len);
38     }
39   } else {
40     writer->write(reinterpret_cast<const char *>(to_conv.conv_val_ptr),
41                   string_len);
42   }
43 }
44 
45 } // namespace printf_core
46 } // namespace __llvm_libc
47