diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt --- a/libc/src/stdio/printf_core/CMakeLists.txt +++ b/libc/src/stdio/printf_core/CMakeLists.txt @@ -20,16 +20,20 @@ libc.src.string.memory_utils.memset_implementation ) -add_header_library( +add_object_library( string_writer + SRCS + string_writer.cpp HDRS string_writer.h DEPENDS libc.src.string.memory_utils.memcpy_implementation ) -add_header_library( +add_object_library( file_writer + SRCS + file_writer.cpp HDRS file_writer.h DEPENDS @@ -62,8 +66,10 @@ ) -add_header_library( +add_object_library( printf_main + SRCS + printf_main.cpp HDRS printf_main.h DEPENDS diff --git a/libc/src/stdio/printf_core/file_writer.h b/libc/src/stdio/printf_core/file_writer.h --- a/libc/src/stdio/printf_core/file_writer.h +++ b/libc/src/stdio/printf_core/file_writer.h @@ -1,4 +1,4 @@ -//===-- FILE Writer class for printf ----------------------------*- C++ -*-===// +//===-- FILE Writer definition for printf -----------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -18,10 +18,7 @@ // write_to_file treats raw_pointer as a File and calls its write // function. void write_to_file(void *raw_pointer, const char *__restrict to_write, - size_t len) { - __llvm_libc::File *file = reinterpret_cast<__llvm_libc::File *>(raw_pointer); - file->write(to_write, len); -} + size_t len); } // namespace printf_core } // namespace __llvm_libc diff --git a/libc/src/stdio/printf_core/file_writer.h b/libc/src/stdio/printf_core/file_writer.cpp copy from libc/src/stdio/printf_core/file_writer.h copy to libc/src/stdio/printf_core/file_writer.cpp --- a/libc/src/stdio/printf_core/file_writer.h +++ b/libc/src/stdio/printf_core/file_writer.cpp @@ -1,4 +1,4 @@ -//===-- FILE Writer class for printf ----------------------------*- C++ -*-===// +//===-- FILE Writer implementation for printf -------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,17 +6,13 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FILE_WRITER_H -#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FILE_WRITER_H - +#include "src/stdio/printf_core/file_writer.h" #include "src/__support/File/file.h" #include namespace __llvm_libc { namespace printf_core { -// write_to_file treats raw_pointer as a File and calls its write -// function. void write_to_file(void *raw_pointer, const char *__restrict to_write, size_t len) { __llvm_libc::File *file = reinterpret_cast<__llvm_libc::File *>(raw_pointer); @@ -25,5 +21,3 @@ } // namespace printf_core } // namespace __llvm_libc - -#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FILE_WRITER_H diff --git a/libc/src/stdio/printf_core/printf_main.h b/libc/src/stdio/printf_core/printf_main.h --- a/libc/src/stdio/printf_core/printf_main.h +++ b/libc/src/stdio/printf_core/printf_main.h @@ -10,9 +10,6 @@ #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H #include "src/__support/arg_list.h" -#include "src/stdio/printf_core/converter.h" -#include "src/stdio/printf_core/core_structs.h" -#include "src/stdio/printf_core/parser.h" #include "src/stdio/printf_core/writer.h" #include @@ -21,19 +18,7 @@ namespace printf_core { int printf_main(Writer *writer, const char *__restrict str, - internal::ArgList &args) { - Parser parser(str, args); - - for (FormatSection cur_section = parser.get_next_section(); - cur_section.raw_len > 0; cur_section = parser.get_next_section()) { - if (cur_section.has_conv) - convert(writer, cur_section); - else - writer->write(cur_section.raw_string, cur_section.raw_len); - } - - return writer->get_chars_written(); -} + internal::ArgList &args); } // namespace printf_core } // namespace __llvm_libc diff --git a/libc/src/stdio/printf_core/printf_main.h b/libc/src/stdio/printf_core/printf_main.cpp copy from libc/src/stdio/printf_core/printf_main.h copy to libc/src/stdio/printf_core/printf_main.cpp --- a/libc/src/stdio/printf_core/printf_main.h +++ b/libc/src/stdio/printf_core/printf_main.cpp @@ -6,8 +6,7 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H -#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H +#include "src/stdio/printf_core/printf_main.h" #include "src/__support/arg_list.h" #include "src/stdio/printf_core/converter.h" @@ -37,5 +36,3 @@ } // namespace printf_core } // namespace __llvm_libc - -#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H diff --git a/libc/src/stdio/printf_core/string_writer.h b/libc/src/stdio/printf_core/string_writer.h --- a/libc/src/stdio/printf_core/string_writer.h +++ b/libc/src/stdio/printf_core/string_writer.h @@ -1,4 +1,4 @@ -//===-- String Writer class for printf -----------------------*- C++ -*-===// +//===-- String Writer definition for printf ---------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -26,16 +26,8 @@ StringWriter(char *__restrict buffer, size_t max_len = ~size_t(0)) : cur_buffer(buffer), available_capacity(max_len) {} - void write(const char *__restrict to_write, size_t len) { - if (len > available_capacity) - len = available_capacity; + void write(const char *__restrict to_write, size_t len); - if (len > 0) { - inline_memcpy(cur_buffer, to_write, len); - cur_buffer += len; - available_capacity -= len; - } - } // Terminate should only be called if the original max length passed to // snprintf was greater than 0. It writes a null byte to the end of the // cur_buffer, regardless of available_capacity. @@ -45,10 +37,7 @@ // write_to_string treats raw_pointer as a StringWriter and calls its write // function. void write_to_string(void *raw_pointer, const char *__restrict to_write, - size_t len) { - StringWriter *string_writer = reinterpret_cast(raw_pointer); - string_writer->write(to_write, len); -} + size_t len); } // namespace printf_core } // namespace __llvm_libc diff --git a/libc/src/stdio/printf_core/string_writer.cpp b/libc/src/stdio/printf_core/string_writer.cpp new file mode 100644 --- /dev/null +++ b/libc/src/stdio/printf_core/string_writer.cpp @@ -0,0 +1,34 @@ +//===-- String Writer implementation for printf -----------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/stdio/printf_core/string_writer.h" +#include "src/string/memory_utils/memcpy_implementations.h" +#include + +namespace __llvm_libc { +namespace printf_core { + +void StringWriter::write(const char *__restrict to_write, size_t len) { + if (len > available_capacity) + len = available_capacity; + + if (len > 0) { + inline_memcpy(cur_buffer, to_write, len); + cur_buffer += len; + available_capacity -= len; + } +} + +void write_to_string(void *raw_pointer, const char *__restrict to_write, + size_t len) { + StringWriter *string_writer = reinterpret_cast(raw_pointer); + string_writer->write(to_write, len); +} + +} // namespace printf_core +} // namespace __llvm_libc