BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
mumufit::stringUtils Namespace Reference

Utility functions to analyze or modify strings. More...

Functions

bool matchesPattern (const std::string &text, const std::string &wildcardPattern)
 Returns true if text matches pattern with wildcards '*' and '?'. More...
 
template<typename T >
std::string scientific (const T value, int n=10)
 Returns scientific string representing given value of any numeric type. More...
 
std::vector< std::string > split (const std::string &text, const std::string &delimeter)
 Split string into vector of string using delimeter. More...
 

Detailed Description

Utility functions to analyze or modify strings.

Function Documentation

◆ matchesPattern()

bool mumufit::stringUtils::matchesPattern ( const std::string &  text,
const std::string &  wildcardPattern 
)

Returns true if text matches pattern with wildcards '*' and '?'.

Definition at line 22 of file StringUtils.cpp.

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 }

◆ scientific()

template<typename T >
std::string mumufit::stringUtils::scientific ( const T  value,
int  n = 10 
)

Returns scientific string representing given value of any numeric type.

Definition at line 41 of file StringUtils.h.

42 {
43  std::ostringstream out;
44  out << std::scientific << std::setprecision(n) << value;
45  return out.str();
46 }
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:41

Referenced by StringUtils::scientific(), MinimizerAdapter::statusMap(), GSLLevenbergMarquardtMinimizer::statusMap(), and Minuit2Minimizer::statusMap().

◆ split()

std::vector< std::string > mumufit::stringUtils::split ( const std::string &  text,
const std::string &  delimeter 
)

Split string into vector of string using delimeter.

Returns token vector obtained by splitting string at delimiters.

Definition at line 51 of file StringUtils.cpp.

52 {
53  std::vector<std::string> tokens;
54  boost::split(tokens, text, boost::is_any_of(delimiter));
55  return tokens;
56 }
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

References StringUtils::split().

Referenced by MinimizerOptions::processCommand(), and MinimizerOptions::setOptionString().

Here is the call graph for this function: