BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
StringUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Util/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 "Base/Util/StringUtils.h"
16 #include <boost/algorithm/string.hpp>
17 #include <charconv>
18 #include <regex>
19 
20 //! Returns string right-padded with blanks.
21 std::string BaseUtils::String::padRight(const std::string& name, size_t length)
22 {
23  std::string result = name;
24  result.resize(length, ' ');
25  return result;
26 }
27 
28 //! Returns token vector obtained by splitting string at delimiters.
29 std::vector<std::string> BaseUtils::String::split(const std::string& text,
30  const std::string& delimiter)
31 {
32  std::vector<std::string> tokens;
33  boost::split(tokens, text, boost::is_any_of(delimiter));
34  return tokens;
35 }
36 
38  const std::vector<std::string>& items,
39  const std::string& replacement)
40 {
41  for (size_t i = 0; i < items.size(); ++i)
42  boost::replace_all(text, items[i], replacement);
43 }
44 
45 std::string BaseUtils::String::join(const std::vector<std::string>& joinable,
46  const std::string& joint)
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 }
57 
58 std::string BaseUtils::String::to_lower(std::string text)
59 {
60  boost::to_lower(text);
61  return text;
62 }
63 
64 bool BaseUtils::String::to_int(const std::string& str, int* result)
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 }
86 
87 std::string BaseUtils::String::trim(const std::string& str, const std::string& whitespace)
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 }
99 
100 std::string BaseUtils::String::trimFront(const std::string& str, const std::string& whitespace)
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 }
109 
110 bool BaseUtils::String::startsWith(const std::string& str, const std::string& substr)
111 {
112  return str.rfind(substr, 0) == 0;
113 }
Defines a few helper functions.
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
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.
Definition: StringUtils.cpp:37
std::string join(const std::vector< std::string > &joinable, const std::string &joint)
Returns string obtain by joining vector elements.
Definition: StringUtils.cpp:45
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.
Definition: StringUtils.cpp:87
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 en...
Definition: StringUtils.cpp:64
std::string trimFront(const std::string &str, const std::string &whitespace=" \t")
Cuts any of the chars given in whitespace from start.
std::string to_lower(std::string text)
Returns new string which is lower case of text.
Definition: StringUtils.cpp:58
bool startsWith(const std::string &str, const std::string &substr)
True if the string starts with substr. The comparison is case sensitive.
std::string padRight(const std::string &name, size_t length)
Returns string right-padded with blanks.
Definition: StringUtils.cpp:21