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