BornAgain  1.19.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 reflection and scattering
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 namespace mumufit {
20 
21 //! Returns true if text matches pattern with wildcards '*' and '?'.
22 bool stringUtils::matchesPattern(const std::string& text, const std::string& wildcardPattern)
23 {
24  // escape all regex special characters, except '?' and '*'
25  std::string mywildcardPattern = wildcardPattern;
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  boost::replace_all(mywildcardPattern, "+", "\\+");
36  boost::replace_all(mywildcardPattern, "/", "\\/");
37 
38  // Convert chars '*?' to their regex equivalents
39  boost::replace_all(mywildcardPattern, "?", ".");
40  boost::replace_all(mywildcardPattern, "*", ".*");
41 
42  // constructing regexp pattern
43  mywildcardPattern = "^" + mywildcardPattern + "$";
44  std::regex pattern(mywildcardPattern);
45 
46  // applaying match
47  return std::regex_match(text, pattern);
48 }
49 
50 //! Returns token vector obtained by splitting string at delimiters.
51 std::vector<std::string> stringUtils::split(const std::string& text, const std::string& delimiter)
52 {
53  std::vector<std::string> tokens;
54  boost::split(tokens, text, boost::is_any_of(delimiter));
55  return tokens;
56 }
57 
58 } // namespace mumufit
Defines a few helper functions.
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::vector< std::string > split(const std::string &text, const std::string &delimeter)
Split string into vector of string using delimeter.
Definition: StringUtils.cpp:51
bool matchesPattern(const std::string &text, const std::string &wildcardPattern)
Returns true if text matches pattern with wildcards '*' and '?'.
Definition: StringUtils.cpp:22
The multi-library, multi-algorithm fit wrapper library.