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