parse Namespace Reference

parse Namespace Reference#

Rocprofiler SDK Developer API: rocprofiler::sdk::parse Namespace Reference
Rocprofiler SDK Developer API 0.4.0
ROCm Profiling API and tools
rocprofiler::sdk::parse Namespace Reference

Functions

template<typename Tp >
Tp from_string (const std::string &str)
 
template<typename Tp >
Tp from_string (const char *cstr)
 
template<typename ContainerT = std::vector<std::string>, typename ValueT = typename ContainerT::value_type, typename PredicateT = std::function<ValueT(ValueT&&)>>
ContainerT tokenize (std::string_view line, std::string_view delimiters="\"',;: ", PredicateT &&predicate=[](ValueT &&s) -> ValueT { return s;})
 tokenize a string into a set
 
template<typename PredicateT = std::function<std::string(const std::string&)>>
std::string str_transform (std::string_view input, std::string_view _begin, std::string_view _end, PredicateT &&predicate)
 apply a string transformation to substring in between a common delimiter.
 

Function Documentation

◆ from_string() [1/2]

template<typename Tp >
Tp rocprofiler::sdk::parse::from_string ( const char *  cstr)
inline

Definition at line 51 of file delimit.hpp.

52{
53 auto ss = std::stringstream{cstr};
54 auto val = Tp{};
55 ss >> val;
56 return val;
57}

◆ from_string() [2/2]

template<typename Tp >
Tp rocprofiler::sdk::parse::from_string ( const std::string &  str)
inline

Definition at line 41 of file delimit.hpp.

42{
43 auto ss = std::stringstream{str};
44 auto val = Tp{};
45 ss >> val;
46 return val;
47}

◆ str_transform()

template<typename PredicateT = std::function<std::string(const std::string&)>>
std::string rocprofiler::sdk::parse::str_transform ( std::string_view  input,
std::string_view  _begin,
std::string_view  _end,
PredicateT &&  predicate 
)
inline

apply a string transformation to substring in between a common delimiter.

Definition at line 112 of file delimit.hpp.

116{
117 size_t _beg_pos = 0; // position that is the beginning of the new string
118 size_t _end_pos = 0; // position of the delimiter in the string
119 std::string _result = std::string{input};
120 while(_beg_pos < _result.length() && _end_pos < _result.length())
121 {
122 // find the first sequence of characters after the end-position
123 _beg_pos = _result.find(_begin, _end_pos);
124
125 // if sequence wasn't found, we are done
126 if(_beg_pos == std::string::npos) break;
127
128 // starting after the position of the first delimiter, find the end sequence
129 if(!_end.empty())
130 _end_pos = _result.find(_end, _beg_pos + 1);
131 else
132 _end_pos = _beg_pos + _begin.length();
133
134 // break if not found
135 if(_end_pos == std::string::npos) break;
136
137 // length of the substr being operated on
138 auto _len = _end_pos - _beg_pos;
139
140 // get the substring between the two delimiters (including first delimiter)
141 auto _sub = _result.substr(_beg_pos, _len);
142
143 // apply the transform
144 auto _transformed = predicate(_sub);
145
146 // only replace if necessary
147 if(_sub != _transformed)
148 {
149 _result = _result.replace(_beg_pos, _len, _transformed);
150 // move end to the end of transformed string
151 _end_pos = _beg_pos + _transformed.length();
152 }
153 }
154 return _result;
155}

◆ tokenize()

template<typename ContainerT = std::vector<std::string>, typename ValueT = typename ContainerT::value_type, typename PredicateT = std::function<ValueT(ValueT&&)>>
ContainerT rocprofiler::sdk::parse::tokenize ( std::string_view  line,
std::string_view  delimiters = "\"',;: ",
PredicateT &&  predicate = [](ValueT&& s) -> ValueT { return s; } 
)
inline

tokenize a string into a set

Definition at line 65 of file delimit.hpp.

67 : ",
68 PredicateT&& predicate = [](ValueT&& s) -> ValueT { return s; })
69{
70 using value_type = ValueT;
71
72 size_t _beginp = 0; // position that is the beginning of the new string
73 size_t _delimp = 0; // position of the delimiter in the string
74 ContainerT _result = {};
75 if(mpl::reserve(_result, 0))
76 {
77 size_t _nmax = 0;
78 for(char itr : line)
79 {
80 if(delimiters.find(itr) != std::string::npos) ++_nmax;
81 }
82 mpl::reserve(_result, _nmax);
83 }
84 while(_beginp < line.length() && _delimp < line.length())
85 {
86 // find the first character (starting at _delimp) that is not a delimiter
87 _beginp = line.find_first_not_of(delimiters, _delimp);
88 // if no a character after or at _end that is not a delimiter is not found
89 // then we are done
90 if(_beginp == std::string::npos) break;
91 // starting at the position of the new string, find the next delimiter
92 _delimp = line.find_first_of(delimiters, _beginp);
93
94 auto _tmp = value_type{};
95 // starting at the position of the new string, get the characters
96 // between this position and the next delimiter
97 if(_beginp < line.length()) _tmp = line.substr(_beginp, _delimp - _beginp);
98
99 // don't add empty strings
100 if(!_tmp.empty())
101 {
102 mpl::emplace(_result, std::forward<PredicateT>(predicate)(std::move(_tmp)));
103 }
104 }
105 return _result;
106}