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 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Utils/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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_BASE_UTILS_STRINGUTILS_H
21 #define BORNAGAIN_BASE_UTILS_STRINGUTILS_H
22 
23 #include <iomanip>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 
28 //! Utility functions to analyze or modify strings.
29 
30 namespace StringUtils {
31 
32 //! Returns true if text matches pattern with wildcards '*' and '?'.
33 bool matchesPattern(const std::string& text, const std::string& wildcardPattern);
34 
35 std::string padRight(const std::string& name, size_t length);
36 
37 //! Split string into vector of string using delimeter.
38 std::vector<std::string> split(const std::string& text, const std::string& delimeter);
39 
40 //! Replaces all occurrences of items from string text with delimiter
41 void replaceItemsFromString(std::string& text, const std::vector<std::string>& items,
42  const std::string& replacement = "");
43 
44 //! Returns string obtain by joining vector elements
45 std::string join(const std::vector<std::string>& joinable, const std::string& joint);
46 
47 //! Removes multiple occurrences of given substring from a string and returns result.
48 std::string removeSubstring(const std::string& text, const std::string& substr);
49 
50 //! Returns scientific string representing given value of any numeric type.
51 template <typename T> std::string scientific(const T value, int n = 10);
52 
53 //! Returns new string which is lower case of text.
54 std::string to_lower(std::string text);
55 
56 //! Cuts any of the chars given in whitespace from start and end of str.
57 std::string trim(const std::string& str, const std::string& whitespace = " \t");
58 
59 } // namespace StringUtils
60 
61 template <typename T> std::string StringUtils::scientific(const T value, int n)
62 {
63  std::ostringstream out;
64  out << std::scientific << std::setprecision(n) << value;
65  return out.str();
66 }
67 
68 #endif // BORNAGAIN_BASE_UTILS_STRINGUTILS_H
69 #endif // USER_API
QString const & name(EShape k)
Definition: particles.cpp:21
Utility functions to analyze or modify strings.
Definition: StringUtils.h:30
std::string removeSubstring(const std::string &text, const std::string &substr)
Removes multiple occurrences 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 trim(const std::string &str, const std::string &whitespace=" \t")
Cuts any of the chars given in whitespace from start and end of str.
Definition: StringUtils.cpp:98
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 occurrences 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:61
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
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:41