Skip to content

Commit

Permalink
try fmt::format_string()
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb committed Sep 25, 2023
1 parent b07ad20 commit 5137346
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions include/LightGBM/utils/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,8 @@ inline static std::vector<T> StringToArray(const std::string& str, char delimite
* /Users/jlamb/repos/LightGBM/include/LightGBM/utils/common.h:1203:35: error: 'format' is not a constant expression
*/
template <typename T>
inline static void format_to_buf(char* buffer, const size_t buf_len, const char* format, const T value) {
const auto result = fmt::format_to_n(buffer, buf_len, format, value);
inline static void format_to_buf(char* buffer, const size_t buf_len, fmt::format_string<T> format, const T value) {
auto result = fmt::format_to_n(buffer, buf_len, format, value);
if (result.size >= buf_len) {
Log::Fatal("Numerical conversion failed. Buffer is too small.");
}
Expand All @@ -1212,29 +1212,29 @@ inline static void format_to_buf(char* buffer, const size_t buf_len, const char*
template<typename T, bool is_float, bool high_precision>
struct __TToStringHelper {
void operator()(T value, char* buffer, size_t buf_len) const {
format_to_buf(buffer, buf_len, "{}", value);
format_to_buf(buffer, buf_len, fmt::format_string<T>("{}"), value);
}
};

template<typename T>
struct __TToStringHelper<T, true, false> {
void operator()(T value, char* buffer, size_t buf_len) const {
format_to_buf(buffer, buf_len, "{:g}", value);
format_to_buf(buffer, buf_len, fmt::format_string<T>("{:g}"), value);
}
};

template<typename T>
struct __TToStringHelper<T, true, true> {
void operator()(T value, char* buffer, size_t buf_len) const {
format_to_buf(buffer, buf_len, "{:.17g}", value);
format_to_buf(buffer, buf_len, fmt::format_string<T>("{:.17g}"), value);
}
};

/*!
* Converts an array to a string with with values separated by the space character.
* This method replaces Common's ``ArrayToString`` and ``ArrayToStringFast`` functionality
* and is locale-independent.
*
*
* \note If ``high_precision_output`` is set to true,
* floating point values are output with more digits of precision.
*/
Expand Down

0 comments on commit 5137346

Please sign in to comment.