25#include <rocprofiler-sdk/cxx/details/mpl.hpp>
42from_string(
const std::string& str)
44 auto ss = std::stringstream{str};
52from_string(
const char* cstr)
54 auto ss = std::stringstream{cstr};
62template <
typename ContainerT = std::vector<std::
string>,
63 typename ValueT =
typename ContainerT::value_type,
64 typename PredicateT = std::function<ValueT(ValueT&&)>>
67 std::string_view line,
68 std::string_view delimiters =
"\"',;: ",
69 PredicateT&& predicate = [](ValueT&& s) -> ValueT {
return s; })
71 using value_type = ValueT;
75 ContainerT _result = {};
76 if(mpl::reserve(_result, 0))
81 if(delimiters.find(itr) != std::string::npos) ++_nmax;
83 mpl::reserve(_result, _nmax);
85 while(_beginp < line.length() && _delimp < line.length())
88 _beginp = line.find_first_not_of(delimiters, _delimp);
91 if(_beginp == std::string::npos)
break;
93 _delimp = line.find_first_of(delimiters, _beginp);
95 auto _tmp = value_type{};
98 if(_beginp < line.length()) _tmp = line.substr(_beginp, _delimp - _beginp);
103 mpl::emplace(_result, std::forward<PredicateT>(predicate)(std::move(_tmp)));
111template <
typename ContainerT = std::vector<std::
string>,
112 typename DelimT = std::
string_view,
113 typename ValueT =
typename ContainerT::value_type,
114 typename PredicateT = ValueT (*)(DelimT&&)>
117 std::string_view line,
118 const std::vector<DelimT>& delimiters,
119 PredicateT&& predicate = [](DelimT&& s) -> ValueT {
return ValueT{s}; })
121 ContainerT _result = {};
123 size_t _end = std::string::npos;
125 while(_start != std::string::npos)
127 _end = std::string::npos;
130 for(
const auto& itr : delimiters)
132 size_t pos = line.find(itr, _start);
133 if(pos != std::string::npos && (_end == std::string::npos || pos < _end))
140 if(_end != std::string::npos)
142 mpl::emplace(_result,
143 std::forward<PredicateT>(predicate)(line.substr(_start, _end - _start)));
147 for(
const auto& delimiter : delimiters)
149 if(line.compare(_start, delimiter.size(), delimiter) == 0)
151 _start += delimiter.size();
159 mpl::emplace(_result, std::forward<PredicateT>(predicate)(line.substr(_start)));
169template <
typename PredicateT = std::function<std::
string(const std::
string&)>>
171str_transform(std::string_view input,
172 std::string_view _begin,
173 std::string_view _end,
174 PredicateT&& predicate)
178 std::string _result = std::string{input};
179 while(_beg_pos < _result.length() && _end_pos < _result.length())
182 _beg_pos = _result.find(_begin, _end_pos);
185 if(_beg_pos == std::string::npos)
break;
189 _end_pos = _result.find(_end, _beg_pos + 1);
191 _end_pos = _beg_pos + _begin.length();
194 if(_end_pos == std::string::npos)
break;
197 auto _len = _end_pos - _beg_pos;
200 auto _sub = _result.substr(_beg_pos, _len);
203 auto _transformed = predicate(_sub);
206 if(_sub != _transformed)
208 _result = _result.replace(_beg_pos, _len, _transformed);
210 _end_pos = _beg_pos + _transformed.length();
217strip(std::string&& str, std::string_view characters)
219 constexpr auto npos = std::string_view::npos;
221 auto bpos = str.find_first_not_of(characters);
222 auto epos = str.find_last_not_of(characters);
224 if(bpos != npos && epos != npos)
225 return str.substr(bpos, (epos - bpos + 1));
226 else if(bpos != npos)
227 return str.substr(bpos);
228 else if(epos != npos)
229 return str.substr(0, epos + 1);