BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
StringUtils.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Util/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_UTIL_STRINGUTILS_H
21 #define BORNAGAIN_BASE_UTIL_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 BaseUtils::String {
31 
32 std::string padRight(const std::string& name, size_t length);
33 
34 //! Split string into vector of string using delimiter.
35 std::vector<std::string> split(const std::string& text, const std::string& delimiter);
36 
37 //! Replaces all occurrences of items from string text with delimiter
38 void replaceItemsFromString(std::string& text, const std::vector<std::string>& items,
39  const std::string& replacement = "");
40 
41 //! Returns string obtain by joining vector elements
42 std::string join(const std::vector<std::string>& joinable, const std::string& joint);
43 
44 //! Returns scientific string representing given value of any numeric type.
45 template <typename T>
46 std::string scientific(T value, int n = 10);
47 
48 //! Returns new string which is lower case of text.
49 std::string to_lower(std::string text);
50 
51 //! Interprets the contained text as an integer and returns it in \a result. Space chars at its
52 //! begin and end will be ignored. If the conversion result is of no interest, but only the
53 //! convertibility, then you can set \a result to nullptr.
54 //! The conversion is assumed to be successful if:
55 //! * every character was consumed by the conversion
56 //! * the value is not too big for int
57 //! * the text is not empty
58 //! Returns true if successful.
59 //! If not successful, the value in result is not changed.
60 //! Does not throw anything.
61 bool to_int(const std::string& str, int* result);
62 
63 //! Cuts any of the chars given in whitespace from start and end of str.
64 std::string trim(const std::string& str, const std::string& whitespace = " \t");
65 
66 //! Cuts any of the chars given in whitespace from start.
67 std::string trimFront(const std::string& str, const std::string& whitespace = " \t");
68 
69 //! True if the string starts with substr. The comparison is case sensitive
70 bool startsWith(const std::string& str, const std::string& substr);
71 
72 } // namespace BaseUtils::String
73 
74 template <typename T>
75 std::string BaseUtils::String::scientific(const T value, int n)
76 {
77  std::ostringstream out;
78  out << std::scientific << std::setprecision(n) << value;
79  return out.str();
80 }
81 
82 #endif // BORNAGAIN_BASE_UTIL_STRINGUTILS_H
83 #endif // USER_API
Utility functions to analyze or modify strings.
Definition: StringUtils.h:30
std::vector< std::string > split(const std::string &text, const std::string &delimiter)
Split string into vector of string using delimiter.
Definition: StringUtils.cpp:29
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:37
std::string scientific(T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:75
std::string join(const std::vector< std::string > &joinable, const std::string &joint)
Returns string obtain by joining vector elements.
Definition: StringUtils.cpp:45
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:87
bool to_int(const std::string &str, int *result)
Interprets the contained text as an integer and returns it in result. Space chars at its begin and en...
Definition: StringUtils.cpp:64
std::string trimFront(const std::string &str, const std::string &whitespace=" \t")
Cuts any of the chars given in whitespace from start.
std::string to_lower(std::string text)
Returns new string which is lower case of text.
Definition: StringUtils.cpp:58
bool startsWith(const std::string &str, const std::string &substr)
True if the string starts with substr. The comparison is case sensitive.
std::string padRight(const std::string &name, size_t length)
Returns string right-padded with blanks.
Definition: StringUtils.cpp:21
std::string scientific(T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:40