21 #include <string_view>
27 std::ostringstream inter;
28 inter << std::setprecision(precision);
29 if (std::abs(input) < std::numeric_limits<double>::epsilon()) {
34 if (inter.str().find(
'e') == std::string::npos && inter.str().find(
'.') == std::string::npos)
41 std::ostringstream inter;
43 inter << std::setprecision(precision);
46 std::string::size_type pos = inter.str().find(
'e');
47 if (pos == std::string::npos)
50 std::string part1 = inter.str().substr(0, pos);
51 std::string part2 = inter.str().substr(pos, std::string::npos);
53 part1.erase(part1.find_last_not_of(
'0') + 1, std::string::npos);
54 if (part1.back() ==
'.')
62 const char whitespace[]{
" \t\n"};
63 const size_t first = str.find_first_not_of(whitespace);
64 if (std::string::npos == first)
66 const size_t last = str.find_last_not_of(whitespace);
67 return str.substr(first, (last - first + 1));
74 auto it = std::unique(str.begin(), str.end(),
75 [](
auto x,
auto y) { return x == y && std::isspace(x); });
76 str.erase(it, str.end());
83 iss.imbue(std::locale::classic());
86 return (!iss.fail() && iss.eof()) ? std::optional<double>(value) :
std::optional<double>{};
94 return (!iss.fail() && iss.eof()) ? std::optional<int>(value) :
std::optional<int>{};
97 std::vector<std::string>
Utils::SplitString(
const std::string& str,
const std::string& delimeter)
100 if (delimeter.empty())
101 throw std::runtime_error(
"Empty delimeter");
105 std::vector<std::string> result;
106 std::string_view view(str);
109 while ((pos = view.find(delimeter)) != std::string::npos) {
110 result.emplace_back(std::string(view.substr(0, pos)));
111 view.remove_prefix(pos + delimeter.length());
113 result.emplace_back(std::string(view));
119 std::vector<double> result;
126 std::istringstream iss(str);
127 iss.imbue(std::locale::classic());
128 std::copy(std::istream_iterator<double>(iss), std::istream_iterator<double>(),
129 back_inserter(result));
MVVM_MODEL_EXPORT std::string ScientificDoubleToString(double input, int precision=6)
Returns string representation of scientific double.
MVVM_MODEL_EXPORT std::string RemoveRepeatedSpaces(std::string str)
Removes repeating spaces for a string.
MVVM_MODEL_EXPORT std::vector< double > ParseSpaceSeparatedDoubles(const std::string &str)
Parses string for double values and returns result as a vector.
MVVM_MODEL_EXPORT std::string TrimWhitespace(const std::string &str)
Returns string after trimming whitespace surrounding, including tabs and carriage returns.
MVVM_MODEL_EXPORT std::string DoubleToString(double input, int precision=12)
Returns string representation of double with given precision.
MVVM_MODEL_EXPORT std::vector< std::string > SplitString(const std::string &str, const std::string &delimeter)
Split string on substring using given delimeter. Reproduces Python's str.split() behavior.
MVVM_MODEL_EXPORT std::optional< int > StringToInteger(const std::string &str)
Converts string to integer.
MVVM_MODEL_EXPORT std::optional< double > StringToDouble(const std::string &str)
Converts string to double value using classc locale and returns it in the form of optional.
materialitems.h Collection of materials to populate MaterialModel.
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.