BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
StringUtils Namespace Reference

Functions

bool matchesPattern (const std::string &text, const std::string &wildcardPattern)
 
std::string padRight (const std::string &name, size_t length)
 
std::vector< std::string > split (const std::string &text, const std::string &delimeter)
 
void replaceItemsFromString (std::string &text, const std::vector< std::string > &items, const std::string &replacement="")
 
std::string join (const std::vector< std::string > &joinable, const std::string &joint)
 
std::string removeSubstring (const std::string &text, const std::string &substr)
 
template<typename T >
std::string scientific (const T value, int n=10)
 
std::string to_lower (std::string text)
 

Detailed Description

Utility functions to analyze or modify strings.

Function Documentation

◆ matchesPattern()

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

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

Definition at line 20 of file StringUtils.cpp.

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 }

Referenced by ParameterPool::getMatchedParameters().

◆ padRight()

std::string StringUtils::padRight ( const std::string &  name,
size_t  length 
)

Returns string right-padded with blanks.

Definition at line 49 of file StringUtils.cpp.

50 {
51  std::string result = name;
52  result.resize(length, ' ');
53  return result;
54 }

References anonymous_namespace{BoxCompositionBuilder.cpp}::length.

Referenced by FitPrintService::parameterString().

◆ split()

std::vector< std::string > 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 57 of file StringUtils.cpp.

58 {
59  std::vector<std::string> tokens;
60  boost::split(tokens, text, boost::is_any_of(delimiter));
61  return tokens;
62 }
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

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

◆ replaceItemsFromString()

void StringUtils::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 at line 64 of file StringUtils.cpp.

66 {
67  for (size_t i = 0; i < items.size(); ++i)
68  boost::replace_all(text, items[i], replacement);
69 }

Referenced by anonymous_namespace{DataFormatUtils.cpp}::getAxisStringRepresentation().

◆ join()

std::string StringUtils::join ( const std::vector< std::string > &  joinable,
const std::string &  joint 
)

Returns string obtain by joining vector elements.

Definition at line 71 of file StringUtils.cpp.

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 }

Referenced by pyfmt2::argumentList(), and anonymous_namespace{Simulation.cpp}::runComputations().

◆ removeSubstring()

std::string StringUtils::removeSubstring ( const std::string &  text,
const std::string &  substr 
)

Removes multiple occurences of given substring from a string and returns result.

Definition at line 83 of file StringUtils.cpp.

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 }

◆ scientific()

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

◆ to_lower()

std::string StringUtils::to_lower ( std::string  text)

Returns new string which is lower case of text.

Definition at line 92 of file StringUtils.cpp.

93 {
94  boost::to_lower(text);
95  return text;
96 }
std::string to_lower(std::string text)
Returns new string which is lower case of text.
Definition: StringUtils.cpp:92