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

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

Rocprofiler SDK Developer API: rocprofiler-sdk/cxx/details/delimit.hpp Source File
Rocprofiler SDK Developer API 0.5.0
ROCm Profiling API and tools
delimit.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 apply a string transformation to substring in between a common delimiter.
109///
110template <typename PredicateT = std::function<std::string(const std::string&)>>
111inline std::string
112str_transform(std::string_view input,
113 std::string_view _begin,
114 std::string_view _end,
115 PredicateT&& predicate)
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}
156} // namespace parse
157} // namespace sdk
158} // 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 delimit.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 delimit.hpp:112
Tp from_string(const std::string &str)
Definition delimit.hpp:41