BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
stringutils.h
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.h
6 //! @brief Defines 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 #ifndef BORNAGAIN_MVVM_MODEL_MVVM_UTILS_STRINGUTILS_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_UTILS_STRINGUTILS_H
17 
18 #include "mvvm/model_export.h"
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 namespace ModelView ::Utils {
24 
25 //! Returns string representation of double with given precision.
26 //! Provides additional formatting on top of iomanip, so "double x{0}" becomes "0.0".
27 MVVM_MODEL_EXPORT std::string DoubleToString(double input, int precision = 12);
28 
29 //! Returns string representation of scientific double.
30 //! Provides additional formatting on top of iomanip, so "double x{1}" becomes "1.0e+00".
31 MVVM_MODEL_EXPORT std::string ScientificDoubleToString(double input, int precision = 6);
32 
33 //! Returns string after trimming whitespace surrounding, including tabs and carriage returns.
34 MVVM_MODEL_EXPORT std::string TrimWhitespace(const std::string& str);
35 
36 //! Removes repeating spaces for a string.
37 MVVM_MODEL_EXPORT std::string RemoveRepeatedSpaces(std::string str);
38 
39 //! Converts string to double value using classc locale and returns it in the form of optional.
40 //! Requires that string represents exactly one double and contains no other literals. Empty
41 //! spaces at the beginning and end of the string are still allowed.
42 MVVM_MODEL_EXPORT std::optional<double> StringToDouble(const std::string& str);
43 
44 //! Converts string to integer. Requires that string represents exactly one integer and
45 //! no extra symbols are defined. Empty spaces at the beginning and end of the string are still
46 //! allowed.
47 MVVM_MODEL_EXPORT std::optional<int> StringToInteger(const std::string& str);
48 
49 //! Split string on substring using given delimeter. Reproduces Python's str.split() behavior.
50 MVVM_MODEL_EXPORT std::vector<std::string> SplitString(const std::string& str,
51  const std::string& delimeter);
52 
53 //! Parses string for double values and returns result as a vector.
54 //! All non-numeric symbols are ingored.
55 MVVM_MODEL_EXPORT std::vector<double> ParseSpaceSeparatedDoubles(const std::string& str);
56 
57 //! Parses string for double values and stores result in a vector.
58 //! All non-numeric symbols are ingored.
59 MVVM_MODEL_EXPORT void ParseSpaceSeparatedDoubles(const std::string& str,
60  std::vector<double>& result);
61 
62 } // namespace ModelView::Utils
63 
64 #endif // BORNAGAIN_MVVM_MODEL_MVVM_UTILS_STRINGUTILS_H
MVVM_MODEL_EXPORT void ParseSpaceSeparatedDoubles(const std::string &str, std::vector< double > &result)
Parses string for double values and stores result in a vector.
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::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.