rocprofiler-sdk/cxx/details/tokenize.hpp Source File

rocprofiler-sdk/cxx/details/tokenize.hpp Source File#

Rocprofiler SDK Developer API: rocprofiler-sdk/cxx/details/tokenize.hpp Source File
Rocprofiler SDK Developer API 0.5.0
ROCm Profiling API and tools
tokenize.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#pragma once
24
26
27#include <functional>
28#include <sstream>
29#include <stdexcept>
30#include <string>
31#include <vector>
32
33namespace rocprofiler
34{
35namespace sdk
36{
37namespace parse
38{
39template <typename Tp>
40inline Tp
41from_string(const std::string& str)
42{
43 auto ss = std::stringstream{str};
44 auto val = Tp{};
45 ss >> val;
46 return val;
47}
48
49template <typename Tp>
50inline Tp
51from_string(const char* cstr)
52{
53 auto ss = std::stringstream{cstr};
54 auto val = Tp{};
55 ss >> val;
56 return val;
57}
58
59/// \brief tokenize a string into a set
60///
61template <typename ContainerT = std::vector<std::string>,
62 typename ValueT = typename ContainerT::value_type,
63 typename PredicateT = std::function<ValueT(ValueT&&)>>
64inline ContainerT
66 std::string_view line,
67 std::string_view delimiters = "\"',;: ",
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}
107
108/// \brief tokenize a string into a set
109///
110template <typename ContainerT = std::vector<std::string>,
111 typename DelimT = std::string_view,
112 typename ValueT = typename ContainerT::value_type,
113 typename PredicateT = ValueT (*)(DelimT&&)>
114inline ContainerT
116 std::string_view line,
117 const std::vector<DelimT>& delimiters,
118 PredicateT&& predicate = [](DelimT&& s) -> ValueT { 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}
165
166/// \brief apply a string transformation to substring in between a common delimiter.
167///
168template <typename PredicateT = std::function<std::string(const std::string&)>>
169inline std::string
170str_transform(std::string_view input,
171 std::string_view _begin,
172 std::string_view _end,
173 PredicateT&& predicate)
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}
214} // namespace parse
215} // namespace sdk
216} // namespace rocprofiler
auto emplace(ContainerT &_c, Args &&... _args)
Definition mpl.hpp:241
auto reserve(ContainerT &_c, ArgT _arg)
Definition mpl.hpp:248
ContainerT tokenize(std::string_view line, std::string_view delimiters="\"',;: ", PredicateT &&predicate=[](ValueT &&s) -> ValueT { return s;})
tokenize a string into a set
Definition tokenize.hpp:65
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.
Definition tokenize.hpp:170
Tp from_string(const std::string &str)
Definition tokenize.hpp:41