BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
StringUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/Tools/StringUtils.cpp
6 //! @brief Implements 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 #include "Fit/Tools/StringUtils.h"
16 #include <boost/algorithm/string.hpp>
17 #include <regex>
18 
19 //! Returns true if text matches pattern with wildcards '*' and '?'.
20 bool StringUtils::matchesPattern(const std::string& text, const std::string& wildcardPattern)
21 {
22  // escape all regex special characters, except '?' and '*'
23  std::string mywildcardPattern = wildcardPattern;
24  boost::replace_all(mywildcardPattern, "\\", "\\\\");
25  boost::replace_all(mywildcardPattern, "^", "\\^");
26  boost::replace_all(mywildcardPattern, ".", "\\.");
27  boost::replace_all(mywildcardPattern, "$", "\\$");
28  boost::replace_all(mywildcardPattern, "|", "\\|");
29  boost::replace_all(mywildcardPattern, "(", "\\(");
30  boost::replace_all(mywildcardPattern, ")", "\\)");
31  boost::replace_all(mywildcardPattern, "[", "\\[");
32  boost::replace_all(mywildcardPattern, "]", "\\]");
33  boost::replace_all(mywildcardPattern, "+", "\\+");
34  boost::replace_all(mywildcardPattern, "/", "\\/");
35 
36  // Convert chars '*?' to their regex equivalents
37  boost::replace_all(mywildcardPattern, "?", ".");
38  boost::replace_all(mywildcardPattern, "*", ".*");
39 
40  // constructing regexp pattern
41  mywildcardPattern = "^" + mywildcardPattern + "$";
42  std::regex pattern(mywildcardPattern);
43 
44  // applaying match
45  return std::regex_match(text, pattern);
46 }
47 
48 //! Returns string right-padded with blanks.
49 std::string StringUtils::padRight(const std::string& name, size_t length)
50 {
51  std::string result = name;
52  result.resize(length, ' ');
53  return result;
54 }
55 
56 //! Returns token vector obtained by splitting string at delimiters.
57 std::vector<std::string> StringUtils::split(const std::string& text, const std::string& delimiter)
58 {
59  std::vector<std::string> tokens;
60  boost::split(tokens, text, boost::is_any_of(delimiter));
61  return tokens;
62 }
63 
64 void StringUtils::replaceItemsFromString(std::string& text, const std::vector<std::string>& items,
65  const std::string& replacement)
66 {
67  for (size_t i = 0; i < items.size(); ++i)
68  boost::replace_all(text, items[i], replacement);
69 }
70 
71 std::string StringUtils::join(const std::vector<std::string>& joinable, const std::string& joint)
72 {
73  std::string result;
74  size_t n = joinable.size();
75  if (n == 0)
76  return result;
77  for (size_t i = 0; i < n - 1; ++i)
78  result += joinable[i] + joint;
79  result += joinable[n - 1];
80  return result;
81 }
82 
83 std::string StringUtils::removeSubstring(const std::string& text, const std::string& substr)
84 {
85  std::string result = text;
86  for (std::string::size_type i = result.find(substr); i != std::string::npos;
87  i = result.find(substr))
88  result.erase(i, substr.length());
89  return result;
90 }
91 
92 std::string StringUtils::to_lower(std::string text)
93 {
94  boost::to_lower(text);
95  return text;
96 }
Defines a few helper functions.
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 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