1 //===-- String Writer definition for printf ---------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_WRITER_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_WRITER_H
12 #include "src/__support/CPP/string_view.h"
13 #include "src/string/memory_utils/memcpy_implementations.h"
16 namespace __llvm_libc
{
17 namespace printf_core
{
20 char *__restrict cur_buffer
;
21 size_t available_capacity
;
24 // StringWriter is intended to take a copy of the cur_buffer pointer, as well
25 // as the maximum length of the string. This maximum length should not include
26 // the null terminator, since that's written separately.
27 StringWriter(char *__restrict buffer
, size_t max_len
= ~size_t(0))
28 : cur_buffer(buffer
), available_capacity(max_len
) {}
30 void write(cpp::string_view new_string
);
31 void write(char new_char
, size_t len
);
32 void write(char new_char
);
34 // Terminate should only be called if the original max length passed to
35 // snprintf was greater than 0. It writes a null byte to the end of the
36 // cur_buffer, regardless of available_capacity.
37 void terminate() { *cur_buffer
= '\0'; }
39 // These write functions take a StringWriter as a void* in raw_pointer, and
40 // call the appropriate write function on it.
41 static int write_str(void *raw_pointer
, cpp::string_view new_string
);
42 static int write_chars(void *raw_pointer
, char new_char
, size_t len
);
43 static int write_char(void *raw_pointer
, char new_char
);
46 } // namespace printf_core
47 } // namespace __llvm_libc
49 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_STRING_WRITER_H