1 //===-- String Writer implementation 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/string_writer.h"
10 #include "src/string/memory_utils/memcpy_implementations.h"
11 #include <stddef.h>
12 
13 namespace __llvm_libc {
14 namespace printf_core {
15 
16 void StringWriter::write(const char *__restrict to_write, size_t len) {
17   if (len > available_capacity)
18     len = available_capacity;
19 
20   if (len > 0) {
21     inline_memcpy(cur_buffer, to_write, len);
22     cur_buffer += len;
23     available_capacity -= len;
24   }
25 }
26 
27 int write_to_string(void *raw_pointer, const char *__restrict to_write,
28                     size_t len) {
29   StringWriter *string_writer = reinterpret_cast<StringWriter *>(raw_pointer);
30   string_writer->write(to_write, len);
31   return 0;
32 }
33 
34 } // namespace printf_core
35 } // namespace __llvm_libc
36