1e1c54d4dSMichael Jones //===-- String Writer implementation for printf -----------------*- C++ -*-===//
2e1c54d4dSMichael Jones //
3e1c54d4dSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e1c54d4dSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5e1c54d4dSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e1c54d4dSMichael Jones //
7e1c54d4dSMichael Jones //===----------------------------------------------------------------------===//
8e1c54d4dSMichael Jones 
9e1c54d4dSMichael Jones #include "src/stdio/printf_core/string_writer.h"
10*9e421a16SMichael Jones #include "src/stdio/printf_core/core_structs.h"
11e1c54d4dSMichael Jones #include "src/string/memory_utils/memcpy_implementations.h"
12e1c54d4dSMichael Jones #include <stddef.h>
13e1c54d4dSMichael Jones 
14e1c54d4dSMichael Jones namespace __llvm_libc {
15e1c54d4dSMichael Jones namespace printf_core {
16e1c54d4dSMichael Jones 
write(const char * __restrict to_write,size_t len)17e1c54d4dSMichael Jones void StringWriter::write(const char *__restrict to_write, size_t len) {
18e1c54d4dSMichael Jones   if (len > available_capacity)
19e1c54d4dSMichael Jones     len = available_capacity;
20e1c54d4dSMichael Jones 
21e1c54d4dSMichael Jones   if (len > 0) {
22e1c54d4dSMichael Jones     inline_memcpy(cur_buffer, to_write, len);
23e1c54d4dSMichael Jones     cur_buffer += len;
24e1c54d4dSMichael Jones     available_capacity -= len;
25e1c54d4dSMichael Jones   }
26e1c54d4dSMichael Jones }
27e1c54d4dSMichael Jones 
write_to_string(void * raw_pointer,const char * __restrict to_write,size_t len)282e6eccfeSMichael Jones int write_to_string(void *raw_pointer, const char *__restrict to_write,
29e1c54d4dSMichael Jones                     size_t len) {
30e1c54d4dSMichael Jones   StringWriter *string_writer = reinterpret_cast<StringWriter *>(raw_pointer);
31e1c54d4dSMichael Jones   string_writer->write(to_write, len);
32*9e421a16SMichael Jones   return WRITE_OK;
33e1c54d4dSMichael Jones }
34e1c54d4dSMichael Jones 
35e1c54d4dSMichael Jones } // namespace printf_core
36e1c54d4dSMichael Jones } // namespace __llvm_libc
37