BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
StringUtils.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/Tools/StringUtils.h
6 //! @brief Defines a few helper functions
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
15 #ifndef BORNAGAIN_FIT_TOOLS_STRINGUTILS_H
16 #define BORNAGAIN_FIT_TOOLS_STRINGUTILS_H
17 
18 #include <iomanip>
19 #include <sstream>
20 #include <string>
21 #include <vector>
22 
23 //! Utility functions to analyze or modify strings.
24 
25 namespace StringUtils
26 {
27 
28 //! Returns true if text matches pattern with wildcards '*' and '?'.
29 bool matchesPattern(const std::string& text, const std::string& wildcardPattern);
30 
31 std::string padRight(const std::string& name, size_t length);
32 
33 //! Split string into vector of string using delimeter.
34 std::vector<std::string> split(const std::string& text, const std::string& delimeter);
35 
36 //! Replaces all occurences of items from string text with delimiter
37 void replaceItemsFromString(std::string& text, const std::vector<std::string>& items,
38  const std::string& replacement = "");
39 
40 //! Returns string obtain by joining vector elements
41 std::string join(const std::vector<std::string>& joinable, const std::string& joint);
42 
43 //! Removes multiple occurences of given substring from a string and returns result.
44 std::string removeSubstring(const std::string& text, const std::string& substr);
45 
46 //! Returns scientific string representing given value of any numeric type.
47 template <typename T> std::string scientific(const T value, int n = 10);
48 
49 //! Returns new string which is lower case of text.
50 std::string to_lower(std::string text);
51 
52 } // namespace StringUtils
53 
54 template <typename T> std::string StringUtils::scientific(const T value, int n)
55 {
56  std::ostringstream out;
57  out << std::scientific << std::setprecision(n) << value;
58  return out.str();
59 }
60 
61 #endif // BORNAGAIN_FIT_TOOLS_STRINGUTILS_H
Utility functions to analyze or modify strings.
Definition: StringUtils.h:26
std::string removeSubstring(const std::string &text, const std::string &substr)
Removes multiple occurences of given substring from a string and returns result.
Definition: StringUtils.cpp:83
std::string join(const std::vector< std::string > &joinable, const std::string &joint)
Returns string obtain by joining vector elements.
Definition: StringUtils.cpp:71
bool matchesPattern(const std::string &text, const std::string &wildcardPattern)
Returns true if text matches pattern with wildcards '*' and '?'.
Definition: StringUtils.cpp:20
std::string to_lower(std::string text)
Returns new string which is lower case of text.
Definition: StringUtils.cpp:92
void replaceItemsFromString(std::string &text, const std::vector< std::string > &items, const std::string &replacement="")
Replaces all occurences of items from string text with delimiter.
Definition: StringUtils.cpp:64
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:54
std::string padRight(const std::string &name, size_t length)
Returns string right-padded with blanks.
Definition: StringUtils.cpp:49
std::vector< std::string > split(const std::string &text, const std::string &delimeter)
Split string into vector of string using delimeter.
Definition: StringUtils.cpp:57