27std::vector<std::string>
Split(std::string_view str, std::string_view delims,
bool skipEmpty )
29 std::vector<std::string> out;
33 while ((end = str.find_first_of(delims, beg)) != std::string::npos) {
34 if (!skipEmpty || end > beg)
35 out.emplace_back(str.substr(beg, end - beg));
38 if (!skipEmpty || str.size() > beg)
39 out.emplace_back(str.substr(beg, str.size() - beg));
44std::pair<std::string_view, std::string_view>
SplitAt(std::string_view str,
char splitter)
46 auto pos = str.find(splitter);
47 if (pos == std::string_view::npos)
49 return {str.substr(0, pos), str.substr(pos + 1)};
66std::string
Round(
double value,
double error,
unsigned int cutoff, std::string_view delim)
69 return std::to_string(
value);
72 int error_exponent_base10_rounded = std::floor(std::log10(error));
73 const auto error_magnitude_base10 = std::pow(10., error_exponent_base10_rounded);
74 const auto error_first_digit =
static_cast<unsigned int>(error / error_magnitude_base10);
75 assert(error_first_digit > 0 && error_first_digit < 10);
76 if (error_first_digit <= cutoff) {
77 const double rescaled_error = error * std::pow(10., -1. * error_exponent_base10_rounded);
78 if (
static_cast<unsigned int>(std::round(rescaled_error * 10) / 10) <= cutoff)
79 error_exponent_base10_rounded--;
80 }
else if (cutoff == 0 && error_first_digit == 9) {
81 const double rounded_rescaled_error = std::round(error * std::pow(10., -1. * error_exponent_base10_rounded));
82 const int rounded_rescaled_error_exponent_base10_rounded = std::floor(std::log10(rounded_rescaled_error));
83 const auto rounded_rescaled_error_magnitude_base10 =
84 std::pow(10., rounded_rescaled_error_exponent_base10_rounded);
85 const auto rounded_rescaled_error_first_digit =
86 static_cast<int>(rounded_rescaled_error / rounded_rescaled_error_magnitude_base10);
87 if (rounded_rescaled_error_first_digit == 1)
88 error_exponent_base10_rounded++;
90 const int factored_out_exponent_base10 = error <= 1
e-3 ?
static_cast<int>(std::floor(std::log10(error) / 3)) * 3
91 :
static_cast<int>(std::log10(error) / 3) * 3;
94 result.setf(std::ios::fixed);
95 if (error_exponent_base10_rounded - factored_out_exponent_base10 < 0) {
96 result.precision(-error_exponent_base10_rounded + factored_out_exponent_base10);
100 if (factored_out_exponent_base10 != 0)
102 result << std::round(
value * std::pow(10., -error_exponent_base10_rounded)) /
103 std::pow(10., -error_exponent_base10_rounded + factored_out_exponent_base10);
105 result << std::round(error * std::pow(10., -error_exponent_base10_rounded)) /
106 std::pow(10., -error_exponent_base10_rounded + factored_out_exponent_base10);
107 if (factored_out_exponent_base10 != 0)
108 result <<
")*1e" << factored_out_exponent_base10;
112std::string
WrapString(std::string_view
string, std::size_t maxLen, std::size_t
indent, std::size_t firstLineMaxLen)
115 throw std::invalid_argument(
"indent cannot be larger than maxLen");
120 auto maxLenUsed = firstLineMaxLen > 0 ? firstLineMaxLen : maxLen -
indent;
125 while (cur <
string.
size()) {
126 auto pos =
string.find_first_of(
" \n\t", cur);
127 if (pos ==
string.npos)
130 const auto wordSize = pos - cur;
132 const bool canWrap = !curLine.empty();
134 if (canWrap && curLine.size() + wordSize > maxLenUsed) {
135 if (curLine.back() ==
' ')
140 curLine.resize(
indent,
' ');
143 curLine +=
string.substr(cur, wordSize);
148 if (
string[pos] ==
'\n' && pos + 1 <
string.
size() &&
string[pos + 1] ==
'\n') {
151 curLine.resize(
indent,
' ');
152 while (pos <
string.
size() &&
string[pos] ==
'\n') {
158 }
else if (!(
result.empty() && curLine.empty())) {
165 const auto wordSize =
string.size() - cur;
166 const bool canWrap = !curLine.empty();
167 if (canWrap && curLine.size() + wordSize > maxLenUsed) {
168 if (curLine.back() ==
' ')
173 curLine.resize(
indent,
' ');
175 if (cur <
string.
size())
176 curLine +=
string.substr(cur);
std::string WrapString(std::string_view string, std::size_t maxLen, std::size_t indent=0, std::size_t firstLineMaxLen=0)
Splits the given string into multiple lines so that each line is at most maxLen wide.
std::string Round(double value, double error, unsigned int cutoff=1, std::string_view delim="#pm")
Convert (round) a value and its uncertainty to string using one or two significant digits of the erro...
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
std::pair< std::string_view, std::string_view > SplitAt(std::string_view str, char splitter)
Given a string str, returns a pair of string views into it: the first containing the substring preced...