Treelite
format_util.h
Go to the documentation of this file.
1 
7 #ifndef TREELITE_COMPILER_COMMON_FORMAT_UTIL_H_
8 #define TREELITE_COMPILER_COMMON_FORMAT_UTIL_H_
9 
10 #include <fmt/format.h>
11 #include <limits>
12 #include <string>
13 #include <sstream>
14 #include <iomanip>
15 
16 namespace treelite {
17 namespace compiler {
18 namespace common_util {
19 
26 inline std::string IndentMultiLineString(const std::string& str,
27  size_t indent = 2) {
28  std::ostringstream oss;
29  if (str[0] != '\n') {
30  oss << std::string(indent, ' ');
31  }
32  bool need_indent = false;
33  // one or more newlines will cause empty spaces to be inserted as indent
34  for (char c : str) { // assume UNIX-style line ending
35  if (c == '\n') {
36  need_indent = true;
37  } else if (need_indent) {
38  oss << std::string(indent, ' ');
39  need_indent = false;
40  }
41  oss << c;
42  }
43  return oss.str();
44 }
45 
52 template <typename T>
53 inline std::string ToStringHighPrecision(T value) {
54  return fmt::format("{:.{}g}", value, std::numeric_limits<T>::max_digits10 + 2);
55 }
56 
60  public:
67  ArrayFormatter(size_t text_width, size_t indent, char delimiter = ',')
68  : oss_(), text_width_(text_width), indent_(indent), delimiter_(delimiter),
69  default_precision_(static_cast<int>(oss_.precision())), line_length_(indent),
70  is_empty_(true) {}
71 
76  template <typename T>
77  inline ArrayFormatter& operator<<(const T& e) {
78  if (is_empty_) {
79  is_empty_ = false;
80  oss_ << std::string(indent_, ' ');
81  }
82  std::ostringstream tmp;
83  tmp << std::setprecision(GetPrecision<T>()) << e << delimiter_ << " ";
84  const std::string token = tmp.str(); // token to be added to wrapped text
85  if (line_length_ + token.length() <= text_width_) {
86  oss_ << token;
87  line_length_ += token.length();
88  } else {
89  oss_ << "\n" << std::string(indent_, ' ') << token;
90  line_length_ = token.length() + indent_;
91  }
92  return *this;
93  }
94 
99  inline std::string str() {
100  return oss_.str();
101  }
102 
103  private:
104  std::ostringstream oss_; // string stream to store wrapped text
105  const size_t text_width_; // maximum length of each line
106  const size_t indent_; // indent level, to indent each line
107  const char delimiter_; // delimiter (defaults to comma)
108  const int default_precision_; // default precision used by string stream
109  size_t line_length_; // width of current line
110  bool is_empty_; // true if no entry has been added yet
111 
112  template <typename T>
113  inline int GetPrecision() {
114  return default_precision_;
115  }
116 };
117 
118 template <>
119 inline int ArrayFormatter::GetPrecision<float>() {
120  return std::numeric_limits<float>::digits10 + 2;
121 }
122 template <>
123 inline int ArrayFormatter::GetPrecision<double>() {
124  return std::numeric_limits<double>::digits10 + 2;
125 }
126 
127 } // namespace common_util
128 } // namespace compiler
129 } // namespace treelite
130 
131 #endif // TREELITE_COMPILER_COMMON_FORMAT_UTIL_H_
std::string IndentMultiLineString(const std::string &str, size_t indent=2)
apply indentation to a multi-line string by inserting spaces at the beginning of each line ...
Definition: format_util.h:26
ArrayFormatter(size_t text_width, size_t indent, char delimiter=',')
constructor
Definition: format_util.h:67
format array as text, wrapped to a given maximum text width. Uses high precision to render floating-p...
Definition: format_util.h:59
std::string str()
obtain formatted text containing the rendered array
Definition: format_util.h:99
std::string ToStringHighPrecision(T value)
obtain a string representation of floating-point value, expressed in high precision ...
Definition: format_util.h:53
ArrayFormatter & operator<<(const T &e)
add an entry (will use high precision for floating-point values)
Definition: format_util.h:77