BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
BaseUtils::String Namespace Reference

Description

Utility functions to analyze or modify strings.

Functions

std::string join (const std::vector< std::string > &joinable, const std::string &joint)
 Returns string obtain by joining vector elements. More...
 
std::string padRight (const std::string &name, size_t length)
 Returns string right-padded with blanks. 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 (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 &delimiter)
 Split string into vector of string using delimiter. More...
 
bool startsWith (const std::string &str, const std::string &substr)
 True if the string starts with substr. The comparison is case sensitive. More...
 
bool to_int (const std::string &str, int *result)
 Interprets the contained text as an integer and returns it in result. Space chars at its begin and end will be ignored. If the conversion result is of no interest, but only the convertibility, then you can set result to nullptr. The conversion is assumed to be successful if: 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...
 
std::string trimFront (const std::string &str, const std::string &whitespace=" \t")
 Cuts any of the chars given in whitespace from start. More...
 

Function Documentation

◆ join()

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

Returns string obtain by joining vector elements.

Definition at line 45 of file StringUtils.cpp.

47 {
48  std::string result;
49  size_t n = joinable.size();
50  if (n == 0)
51  return result;
52  for (size_t i = 0; i < n - 1; ++i)
53  result += joinable[i] + joint;
54  result += joinable[n - 1];
55  return result;
56 }

Referenced by Py::Fmt::printArguments(), and Py::Fmt::printImportedSymbols().

◆ padRight()

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

Returns string right-padded with blanks.

Definition at line 21 of file StringUtils.cpp.

22 {
23  std::string result = name;
24  result.resize(length, ' ');
25  return result;
26 }

Referenced by FitPrintService::parameterString().

◆ replaceItemsFromString()

void BaseUtils::String::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 37 of file StringUtils.cpp.

40 {
41  for (size_t i = 0; i < items.size(); ++i)
42  boost::replace_all(text, items[i], replacement);
43 }

◆ scientific()

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

Returns scientific string representing given value of any numeric type.

Definition at line 75 of file StringUtils.h.

76 {
77  std::ostringstream out;
78  out << std::scientific << std::setprecision(n) << value;
79  return out.str();
80 }
std::string scientific(T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:40

References mumufit::stringUtils::scientific().

Referenced by FitPrintService::iterationHeaderString(), FitPrintService::parameterString(), Py::Fmt::printScientificDouble(), AttLimits::toString(), ReadWriteNumpyTXT::write1DRepresentation(), ReadWriteNumpyTXT::write2DRepresentation(), and ReadWriteINT::writeDatafieldDoubles().

Here is the call graph for this function:

◆ split()

std::vector< std::string > BaseUtils::String::split ( const std::string &  text,
const std::string &  delimiter 
)

Split string into vector of string using delimiter.

Returns token vector obtained by splitting string at delimiters.

Definition at line 29 of file StringUtils.cpp.

31 {
32  std::vector<std::string> tokens;
33  boost::split(tokens, text, boost::is_any_of(delimiter));
34  return tokens;
35 }
std::vector< std::string > split(const std::string &text, const std::string &delimiter)
Split string into vector of string using delimiter.
Definition: StringUtils.cpp:29

Referenced by IO::readNicosData(), and mumufit::stringUtils::split().

◆ startsWith()

bool BaseUtils::String::startsWith ( const std::string &  str,
const std::string &  substr 
)

True if the string starts with substr. The comparison is case sensitive.

Definition at line 110 of file StringUtils.cpp.

111 {
112  return str.rfind(substr, 0) == 0;
113 }

Referenced by IO::readNicosData().

◆ to_int()

bool BaseUtils::String::to_int ( const std::string &  str,
int *  result 
)

Interprets the contained text as an integer and returns it in result. Space chars at its begin and end will be ignored. If the conversion result is of no interest, but only the convertibility, then you can set result to nullptr. The conversion is assumed to be successful if:

  • every character was consumed by the conversion
  • the value is not too big for int
  • the text is not empty Returns true if successful. If not successful, the value in result is not changed. Does not throw anything.

Definition at line 64 of file StringUtils.cpp.

65 {
66  const char* first = str.data() + str.find_first_not_of(' ');
67  const char* last = str.data() + str.size();
68  int _result = 0;
69  auto [p, ec] = std::from_chars(first, last, _result);
70 
71  if (ec != std::errc())
72  return false;
73 
74  if (p != last) {
75  // not all was consumed. Check whether only space characters left
76  const size_t pos = p - str.data();
77  const auto hasNonSpaceLeft = str.find_first_not_of(' ', pos) != std::string::npos;
78  if (hasNonSpaceLeft)
79  return false;
80  }
81 
82  if (result != nullptr)
83  *result = _result;
84  return true;
85 }

Referenced by IO::readNicosData().

◆ to_lower()

std::string BaseUtils::String::to_lower ( std::string  text)

Returns new string which is lower case of text.

Definition at line 58 of file StringUtils.cpp.

59 {
60  boost::to_lower(text);
61  return text;
62 }
std::string to_lower(std::string text)
Returns new string which is lower case of text.
Definition: StringUtils.cpp:58

Referenced by BaseUtils::Filesystem::hasExtension().

◆ trim()

std::string BaseUtils::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.

Definition at line 87 of file StringUtils.cpp.

88 {
89  const auto strBegin = str.find_first_not_of(whitespace);
90 
91  if (strBegin == std::string::npos)
92  return "";
93 
94  const auto strEnd = str.find_last_not_of(whitespace);
95  const auto strRange = strEnd - strBegin + 1;
96 
97  return str.substr(strBegin, strRange);
98 }

Referenced by ReadWriteINT::readDatafield(), ReadWriteNumpyTXT::readDatafield(), ReadReflectometry::readDatafield(), and IO::readNicosData().

◆ trimFront()

std::string BaseUtils::String::trimFront ( const std::string &  str,
const std::string &  whitespace = " \t" 
)

Cuts any of the chars given in whitespace from start.

Definition at line 100 of file StringUtils.cpp.

101 {
102  const auto strBegin = str.find_first_not_of(whitespace);
103 
104  if (strBegin == std::string::npos)
105  return "";
106 
107  return str.substr(strBegin);
108 }

Referenced by IO::readNicosData().