BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
stringutils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/utils/stringutils.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "mvvm/utils/stringutils.h"
16 #include <algorithm>
17 #include <cmath>
18 #include <iomanip>
19 #include <iterator>
20 #include <sstream>
21 #include <string_view>
22 
23 using namespace ModelView;
24 
25 std::string Utils::DoubleToString(double input, int precision)
26 {
27  std::ostringstream inter;
28  inter << std::setprecision(precision);
29  if (std::abs(input) < std::numeric_limits<double>::epsilon()) {
30  inter << "0.0";
31  return inter.str();
32  }
33  inter << input;
34  if (inter.str().find('e') == std::string::npos && inter.str().find('.') == std::string::npos)
35  inter << ".0";
36  return inter.str();
37 }
38 
39 std::string Utils::ScientificDoubleToString(double input, int precision)
40 {
41  std::ostringstream inter;
42  inter << std::scientific;
43  inter << std::setprecision(precision);
44  inter << input;
45 
46  std::string::size_type pos = inter.str().find('e');
47  if (pos == std::string::npos)
48  return inter.str();
49 
50  std::string part1 = inter.str().substr(0, pos);
51  std::string part2 = inter.str().substr(pos, std::string::npos);
52 
53  part1.erase(part1.find_last_not_of('0') + 1, std::string::npos);
54  if (part1.back() == '.')
55  part1 += "0";
56 
57  return part1 + part2;
58 }
59 
60 std::string Utils::TrimWhitespace(const std::string& str)
61 {
62  const char whitespace[]{" \t\n"};
63  const size_t first = str.find_first_not_of(whitespace);
64  if (std::string::npos == first)
65  return {};
66  const size_t last = str.find_last_not_of(whitespace);
67  return str.substr(first, (last - first + 1));
68 }
69 
70 std::string Utils::RemoveRepeatedSpaces(std::string str)
71 {
72  if (str.empty())
73  return {};
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());
77  return str;
78 }
79 
80 std::optional<double> Utils::StringToDouble(const std::string& str)
81 {
82  std::istringstream iss(Utils::TrimWhitespace(str));
83  iss.imbue(std::locale::classic());
84  double value;
85  iss >> value;
86  return (!iss.fail() && iss.eof()) ? std::optional<double>(value) : std::optional<double>{};
87 }
88 
89 std::optional<int> Utils::StringToInteger(const std::string& str)
90 {
91  std::istringstream iss(Utils::TrimWhitespace(str));
92  int value;
93  iss >> value;
94  return (!iss.fail() && iss.eof()) ? std::optional<int>(value) : std::optional<int>{};
95 }
96 
97 std::vector<std::string> Utils::SplitString(const std::string& str, const std::string& delimeter)
98 {
99  // splitting string following Python's str.split()
100  if (delimeter.empty())
101  throw std::runtime_error("Empty delimeter");
102  if (str.empty())
103  return {};
104 
105  std::vector<std::string> result;
106  std::string_view view(str);
107  size_t pos{0};
108 
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());
112  }
113  result.emplace_back(std::string(view));
114  return result;
115 }
116 
117 std::vector<double> Utils::ParseSpaceSeparatedDoubles(const std::string& str)
118 {
119  std::vector<double> result;
120  ParseSpaceSeparatedDoubles(str, result);
121  return result;
122 }
123 
124 void Utils::ParseSpaceSeparatedDoubles(const std::string& str, std::vector<double>& result)
125 {
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));
130 }
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.
Definition: StringUtils.h:61
Definition: filesystem.h:81
Defines class CLASS?