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

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

Functions

std::string join (const std::vector< std::string > &joinable, const std::string &joint)
 Returns string obtain by joining vector elements. More...
 
bool matchesPattern (const std::string &text, const std::string &wildcardPattern)
 Returns true if text matches pattern with wildcards '*' and '?'. More...
 
std::string padRight (const std::string &name, size_t length)
 Returns string right-padded with blanks. More...
 
std::string removeSubstring (const std::string &text, const std::string &substr)
 Removes multiple occurrences of given substring from a string and returns result. More...
 
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. 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...
 
std::string to_lower (std::string text)
 Returns new string which is lower case of text. More...
 
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. More...
 

Detailed Description

Utility functions to analyze or modify strings.

Function Documentation

◆ 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 pyfmt::printImportedSymbols().

◆ 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 }
QString const & name(EShape k)
Definition: particles.cpp:21

References RealSpace::Particles::name().

Referenced by FitPrintService::parameterString().

Here is the call graph for this function:

◆ removeSubstring()

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

Removes multiple occurrences 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 }

◆ replaceItemsFromString()

void StringUtils::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 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 }

◆ scientific()

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

Returns scientific string representing given value of any numeric type.

Definition at line 61 of file StringUtils.h.

62 {
63  std::ostringstream out;
64  out << std::scientific << std::setprecision(n) << value;
65  return out.str();
66 }
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:41

References mumufit::stringUtils::scientific().

Referenced by FitPrintService::iterationHeaderString(), FitPrintService::parameterString(), pyfmt::printScientificDouble(), AttLimits::toString(), OutputDataReadWriteNumpyTXT::write1DRepresentation(), OutputDataReadWriteNumpyTXT::write2DRepresentation(), and OutputDataReadWriteINT::writeOutputDataDoubles().

Here is the call graph for this function:

◆ 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 mumufit::stringUtils::split().

◆ 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

◆ trim()

std::string StringUtils::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 at line 98 of file StringUtils.cpp.

99 {
100  const auto strBegin = str.find_first_not_of(whitespace);
101 
102  if (strBegin == std::string::npos)
103  return "";
104 
105  const auto strEnd = str.find_last_not_of(whitespace);
106  const auto strRange = strEnd - strBegin + 1;
107 
108  return str.substr(strBegin, strRange);
109 }

Referenced by OutputDataReadReflectometry::readOutputData(), OutputDataReadWriteINT::readOutputData(), and OutputDataReadWriteNumpyTXT::readOutputData().