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

source code of libc/src/stdio/printf_core/writer.cpp