7 #ifndef TREELITE_COMPILER_COMMON_FORMAT_UTIL_H_ 8 #define TREELITE_COMPILER_COMMON_FORMAT_UTIL_H_ 10 #include <fmt/format.h> 18 namespace common_util {
28 std::ostringstream oss;
30 oss << std::string(indent,
' ');
32 bool need_indent =
false;
37 }
else if (need_indent) {
38 oss << std::string(indent,
' ');
54 return fmt::format(
"{:.{}g}", value, std::numeric_limits<T>::max_digits10 + 2);
68 : oss_(), text_width_(text_width), indent_(indent), delimiter_(delimiter),
69 default_precision_(static_cast<int>(oss_.precision())), line_length_(indent),
80 oss_ << std::string(indent_,
' ');
82 std::ostringstream tmp;
83 tmp << std::setprecision(GetPrecision<T>()) << e << delimiter_ <<
" ";
84 const std::string token = tmp.str();
85 if (line_length_ + token.length() <= text_width_) {
87 line_length_ += token.length();
89 oss_ <<
"\n" << std::string(indent_,
' ') << token;
90 line_length_ = token.length() + indent_;
99 inline std::string
str() {
104 std::ostringstream oss_;
105 const size_t text_width_;
106 const size_t indent_;
107 const char delimiter_;
108 const int default_precision_;
112 template <
typename T>
113 inline int GetPrecision() {
114 return default_precision_;
119 inline int ArrayFormatter::GetPrecision<float>() {
120 return std::numeric_limits<float>::digits10 + 2;
123 inline int ArrayFormatter::GetPrecision<double>() {
124 return std::numeric_limits<double>::digits10 + 2;
131 #endif // TREELITE_COMPILER_COMMON_FORMAT_UTIL_H_