Skip to content

Commit

Permalink
Optimizing...
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Jan 13, 2025
1 parent c7d0f42 commit dfedf61
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 87 deletions.
7 changes: 5 additions & 2 deletions src/xtd.core/include/xtd/internal/__print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@
#include <cstdio>
#include <ostream>
#include "../io/io_exception.hpp"
#include "../environment.hpp"
#include "../null_pointer_exception.hpp"

/// @cond
void __xtd_print_with_file_write__(FILE* file, const std::string& s) {
void __xtd_print_with_file_write__(bool new_line, FILE* file, xtd::string&& s) {
if (!file) throw xtd::null_pointer_exception {};
if (new_line) s += xtd::environment::new_line();
if (fwrite(s.c_str(), 1, s.length(), file) != s.length()) {
auto exception = xtd::io::io_exception {};
exception.h_result(errno);
throw exception;
}
}

void __xtd_print_with_ostream_write__(std::ostream& os, const std::string& s) {
void __xtd_print_with_ostream_write__(bool new_line, std::ostream& os, xtd::string&& s) {
if (!os.good()) throw xtd::io::io_exception {};
if (new_line) s += xtd::environment::new_line();
os.write(s.c_str(), s.length());
}
/// @endcond
32 changes: 16 additions & 16 deletions src/xtd.core/include/xtd/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace xtd {
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
template<class arg_t>
void print(FILE* file, arg_t&& value) {
__xtd_print_with_file_write__(file, string::format("{}", value));
__xtd_print_with_file_write__(false, file, string::format("{}", value));
}

/// @cond
template<class type_t>
void print(FILE* file, std::initializer_list<type_t>&& il) {
__xtd_print_with_file_write__(file, string::format("{}", il));
__xtd_print_with_file_write__(false, file, string::format("{}", il));
}
/// @endcond

Expand All @@ -35,7 +35,7 @@ namespace xtd {
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
template<class ...args_t>
void print(FILE* file, const xtd::string& fmt, args_t&& ... values) {
__xtd_print_with_file_write__(file, string::format(fmt, std::forward<args_t>(values)...));
__xtd_print_with_file_write__(false, file, string::format(fmt, std::forward<args_t>(values)...));
}
/// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information.
/// @tparam ...args_t Types of the values to write.
Expand All @@ -45,7 +45,7 @@ namespace xtd {
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
template<class ...args_t>
void print(FILE* file, const char* fmt, args_t&& ... values) {
__xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}
#if defined(__xtd__cpp_lib_char8_t)
/// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information.
Expand All @@ -56,7 +56,7 @@ namespace xtd {
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
template<class ...args_t>
void print(FILE* file, const char8_t* fmt, args_t&& ... values) {
__xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}
#endif
/// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information.
Expand All @@ -67,7 +67,7 @@ namespace xtd {
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
template<class ...args_t>
void print(FILE* file, const char16_t* fmt, args_t&& ... values) {
__xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}
/// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information.
/// @tparam ...args_t Types of the values to write.
Expand All @@ -77,7 +77,7 @@ namespace xtd {
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
template<class ...args_t>
void print(FILE* file, const char32_t* fmt, args_t&& ... values) {
__xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}
/// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information.
/// @tparam ...args_t Types of the values to write.
Expand All @@ -87,7 +87,7 @@ namespace xtd {
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
template<class ...args_t>
void print(FILE* file, const wchar_t* fmt, args_t&& ... values) {
__xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}

/// @brief Writes the text representation of the specified value to the output stream.
Expand All @@ -96,13 +96,13 @@ namespace xtd {
/// @param value The value to write,
template<class arg_t>
void print(std::ostream& os, arg_t&& value) {
__xtd_print_with_ostream_write__(os, string::format("{}", value));
__xtd_print_with_ostream_write__(false, os, string::format("{}", value));
}

/// @cond
template<class type_t>
void print(std::ostream& os, std::initializer_list<type_t>&& il) {
__xtd_print_with_ostream_write__(os, string::format("{}", il));
__xtd_print_with_ostream_write__(false, os, string::format("{}", il));
}
/// @endcond

Expand All @@ -113,7 +113,7 @@ namespace xtd {
/// @param values Values to write,
template<class ...args_t>
void print(std::ostream& os, const xtd::string& fmt, args_t&& ... values) {
__xtd_print_with_ostream_write__(os, string::format(fmt, std::forward<args_t>(values)...));
__xtd_print_with_ostream_write__(false, os, string::format(fmt, std::forward<args_t>(values)...));
}
/// @brief Writes the text representation of the specified list of values to the output stream using the specified format information.
/// @tparam ...args_t Types of the values to write.
Expand All @@ -122,7 +122,7 @@ namespace xtd {
/// @param values Values to write,
template<class ...args_t>
void print(std::ostream& os, const char* fmt, args_t&& ... values) {
__xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}
#if defined(__xtd__cpp_lib_char8_t)
/// @brief Writes the text representation of the specified list of values to the output stream using the specified format information.
Expand All @@ -132,7 +132,7 @@ namespace xtd {
/// @param values Values to write,
template<class ...args_t>
void print(std::ostream& os, const char8_t* fmt, args_t&& ... values) {
__xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}
#endif
/// @brief Writes the text representation of the specified list of values to the output stream using the specified format information.
Expand All @@ -142,7 +142,7 @@ namespace xtd {
/// @param values Values to write,
template<class ...args_t>
void print(std::ostream& os, const char16_t* fmt, args_t&& ... values) {
__xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}
/// @brief Writes the text representation of the specified list of values to the output stream using the specified format information.
/// @tparam ...args_t Types of the values to write.
Expand All @@ -151,7 +151,7 @@ namespace xtd {
/// @param values Values to write,
template<class ...args_t>
void print(std::ostream& os, const char32_t* fmt, args_t&& ... values) {
__xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}
/// @brief Writes the text representation of the specified list of values to the output stream using the specified format information.
/// @tparam ...args_t Types of the values to write.
Expand All @@ -160,7 +160,7 @@ namespace xtd {
/// @param values Values to write,
template<class ...args_t>
void print(std::ostream& os, const wchar_t* fmt, args_t&& ... values) {
__xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
__xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
}

/// @brief Writes the text representation of the specified value to the standard output stream.
Expand Down
Loading

0 comments on commit dfedf61

Please sign in to comment.