parse Namespace Reference#
Rocprofiler SDK Developer API 0.5.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 ContainerT = std::vector<std::string>, typename DelimT = std::string_view, typename ValueT = typename ContainerT::value_type, typename PredicateT = ValueT (*)(DelimT&&)> | |
ContainerT | tokenize (std::string_view line, const std::vector< DelimT > &delimiters, PredicateT &&predicate=[](DelimT &&s) -> ValueT { return ValueT{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 >
|
inline |
Definition at line 51 of file tokenize.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 >
|
inline |
Definition at line 41 of file tokenize.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&)>>
|
inline |
apply a string transformation to substring in between a common delimiter.
Definition at line 170 of file tokenize.hpp.
174{
175 size_t _beg_pos = 0; // position that is the beginning of the new string
176 size_t _end_pos = 0; // position of the delimiter in the string
177 std::string _result = std::string{input};
178 while(_beg_pos < _result.length() && _end_pos < _result.length())
179 {
180 // find the first sequence of characters after the end-position
181 _beg_pos = _result.find(_begin, _end_pos);
182
183 // if sequence wasn't found, we are done
184 if(_beg_pos == std::string::npos) break;
185
186 // starting after the position of the first delimiter, find the end sequence
187 if(!_end.empty())
188 _end_pos = _result.find(_end, _beg_pos + 1);
189 else
190 _end_pos = _beg_pos + _begin.length();
191
192 // break if not found
193 if(_end_pos == std::string::npos) break;
194
195 // length of the substr being operated on
196 auto _len = _end_pos - _beg_pos;
197
198 // get the substring between the two delimiters (including first delimiter)
199 auto _sub = _result.substr(_beg_pos, _len);
200
201 // apply the transform
202 auto _transformed = predicate(_sub);
203
204 // only replace if necessary
205 if(_sub != _transformed)
206 {
207 _result = _result.replace(_beg_pos, _len, _transformed);
208 // move end to the end of transformed string
209 _end_pos = _beg_pos + _transformed.length();
210 }
211 }
212 return _result;
213}
◆ tokenize() [1/2]
template<typename ContainerT = std::vector<std::string>, typename DelimT = std::string_view, typename ValueT = typename ContainerT::value_type, typename PredicateT = ValueT (*)(DelimT&&)>
|
inline |
tokenize a string into a set
Definition at line 115 of file tokenize.hpp.
118 { return ValueT{s}; })
119{
120 ContainerT _result = {};
121 size_t _start = 0;
122 size_t _end = std::string::npos;
123
124 while(_start != std::string::npos)
125 {
126 _end = std::string::npos;
127
128 // Find the earliest occurrence of any delimiter
129 for(const auto& itr : delimiters)
130 {
131 size_t pos = line.find(itr, _start);
132 if(pos != std::string::npos && (_end == std::string::npos || pos < _end))
133 {
134 _end = pos;
135 }
136 }
137
138 // Extract token and update start position
139 if(_end != std::string::npos)
140 {
141 mpl::emplace(_result,
142 std::forward<PredicateT>(predicate)(line.substr(_start, _end - _start)));
143 _start = _end;
144
145 // Move start past the delimiter
146 for(const auto& delimiter : delimiters)
147 {
148 if(line.compare(_start, delimiter.size(), delimiter) == 0)
149 {
150 _start += delimiter.size();
151 break;
152 }
153 }
154 }
155 else
156 {
157 // Last token after the final delimiter
158 mpl::emplace(_result, std::forward<PredicateT>(predicate)(line.substr(_start)));
159 break;
160 }
161 }
162
163 return _result;
164}
◆ tokenize() [2/2]
template<typename ContainerT = std::vector<std::string>, typename ValueT = typename ContainerT::value_type, typename PredicateT = std::function<ValueT(ValueT&&)>>
|
inline |
tokenize a string into a set
Definition at line 65 of file tokenize.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}
Generated by 1.9.8