1 //===-- Writer definition 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 "writer.h" 10 #include "src/__support/CPP/string_view.h" 11 #include "src/__support/macros/config.h" 12 #include "src/stdio/printf_core/core_structs.h" 13 #include "src/string/memory_utils/inline_memset.h" 14 #include <stddef.h> 15 16 namespace LIBC_NAMESPACE_DECL { 17 namespace printf_core { 18 19 int Writer::pad(char new_char, size_t length) { 20 // First, fill as much of the buffer as possible with the padding char. 21 size_t written = 0; 22 const size_t buff_space = wb->buff_len - wb->buff_cur; 23 // ASSERT: length > buff_space 24 if (buff_space > 0) { 25 inline_memset(wb->buff + wb->buff_cur, new_char, buff_space); 26 wb->buff_cur += buff_space; 27 written = buff_space; 28 } 29 30 // Next, overflow write the rest of length using the mini_buff. 31 constexpr size_t MINI_BUFF_SIZE = 64; 32 char mini_buff[MINI_BUFF_SIZE]; 33 inline_memset(mini_buff, new_char, MINI_BUFF_SIZE); 34 cpp::string_view mb_string_view(mini_buff, MINI_BUFF_SIZE); 35 while (written + MINI_BUFF_SIZE < length) { 36 int result = wb->overflow_write(mb_string_view); 37 if (result != WRITE_OK) 38 return result; 39 written += MINI_BUFF_SIZE; 40 } 41 cpp::string_view mb_substr = mb_string_view.substr(0, length - written); 42 return wb->overflow_write(mb_substr); 43 } 44 45 } // namespace printf_core 46 } // namespace LIBC_NAMESPACE_DECL 47