1e1c54d4dSMichael Jones //===-- String Writer definition for printf ---------------------*- C++ -*-===// 2e072a123SMichael Jones // 3e072a123SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e072a123SMichael Jones // See https://llvm.org/LICENSE.txt for license information. 5e072a123SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e072a123SMichael Jones // 7e072a123SMichael Jones //===----------------------------------------------------------------------===// 8e072a123SMichael Jones 9e072a123SMichael Jones #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_WRITER_H 10e072a123SMichael Jones #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_WRITER_H 11e072a123SMichael Jones 12e072a123SMichael Jones #include "src/string/memory_utils/memcpy_implementations.h" 13e072a123SMichael Jones #include <stddef.h> 14e072a123SMichael Jones 15e072a123SMichael Jones namespace __llvm_libc { 16e072a123SMichael Jones namespace printf_core { 17e072a123SMichael Jones 18e072a123SMichael Jones class StringWriter { 19e072a123SMichael Jones char *__restrict cur_buffer; 20e072a123SMichael Jones size_t available_capacity; 21e072a123SMichael Jones 22e072a123SMichael Jones public: 23e072a123SMichael Jones // StringWriter is intended to take a copy of the cur_buffer pointer, as well 24e072a123SMichael Jones // as the maximum length of the string. This maximum length should not include 25e072a123SMichael Jones // the null terminator, since that's written separately. 26e072a123SMichael Jones StringWriter(char *__restrict buffer, size_t max_len = ~size_t(0)) cur_buffer(buffer)27e072a123SMichael Jones : cur_buffer(buffer), available_capacity(max_len) {} 28e072a123SMichael Jones 29e1c54d4dSMichael Jones void write(const char *__restrict to_write, size_t len); 30e072a123SMichael Jones 31e072a123SMichael Jones // Terminate should only be called if the original max length passed to 32e072a123SMichael Jones // snprintf was greater than 0. It writes a null byte to the end of the 33e072a123SMichael Jones // cur_buffer, regardless of available_capacity. terminate()34e072a123SMichael Jones void terminate() { *cur_buffer = '\0'; } 35e072a123SMichael Jones }; 36e072a123SMichael Jones 37e072a123SMichael Jones // write_to_string treats raw_pointer as a StringWriter and calls its write 38e072a123SMichael Jones // function. 39*2e6eccfeSMichael Jones int write_to_string(void *raw_pointer, const char *__restrict to_write, 40e1c54d4dSMichael Jones size_t len); 41e072a123SMichael Jones 42e072a123SMichael Jones } // namespace printf_core 43e072a123SMichael Jones } // namespace __llvm_libc 44e072a123SMichael Jones 45e072a123SMichael Jones #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_WRITER_H 46