timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
delimit.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
27#include <functional>
28#include <sstream>
29#include <stdexcept>
30#include <string>
31#include <vector>
32
33namespace tim
34{
35//
36//--------------------------------------------------------------------------------------//
37//
38template <typename T>
39inline T
41{
42 std::stringstream ss{};
43 ss << str;
44 T val{};
45 ss >> val;
46 return val;
47}
48//
49//--------------------------------------------------------------------------------------//
50//
51template <typename T>
52inline T
53from_string(const char* cstr)
54{
55 std::stringstream ss{};
56 ss << cstr;
57 T val{};
58 ss >> val;
59 return val;
60}
61//
62//--------------------------------------------------------------------------------------//
63// delimit a string into a set
64//
65template <typename ContainerT = std::vector<std::string>,
66 typename PredicateT = std::function<std::string(const std::string&)>>
67inline ContainerT
69 const std::string& line, const std::string& delimiters = "\"',;: ",
70 PredicateT&& predicate = [](const std::string& s) -> std::string { return s; })
71{
72 ContainerT _result{};
73 size_t _beginp = 0; // position that is the beginning of the new string
74 size_t _delimp = 0; // position of the delimiter in the string
75 while(_beginp < line.length() && _delimp < line.length())
76 {
77 // find the first character (starting at _delimp) that is not a delimiter
78 _beginp = line.find_first_not_of(delimiters, _delimp);
79 // if no a character after or at _end that is not a delimiter is not found
80 // then we are done
81 if(_beginp == std::string::npos)
82 break;
83 // starting at the position of the new string, find the next delimiter
84 _delimp = line.find_first_of(delimiters, _beginp);
85 std::string _tmp{};
86 try
87 {
88 // starting at the position of the new string, get the characters
89 // between this position and the next delimiter
90 _tmp = line.substr(_beginp, _delimp - _beginp);
91 } catch(std::exception& e)
92 {
93 // print the exception but don't fail, unless maybe it should?
94 fprintf(stderr, "%s\n", e.what());
95 }
96 // don't add empty strings
97 if(!_tmp.empty())
98 {
99 _result.insert(_result.end(), predicate(_tmp));
100 }
101 }
102 return _result;
103}
104//
105//--------------------------------------------------------------------------------------//
106/// \brief apply a string transformation to substring inbetween a common delimiter.
107/// e.g.
108//
109template <typename PredicateT = std::function<std::string(const std::string&)>>
110inline std::string
111str_transform(const std::string& input, const std::string& _begin,
112 const std::string& _end, PredicateT&& predicate)
113{
114 size_t _beg_pos = 0; // position that is the beginning of the new string
115 size_t _end_pos = 0; // position of the delimiter in the string
116 std::string _result = input;
117 while(_beg_pos < _result.length() && _end_pos < _result.length())
118 {
119 // find the first sequence of characters after the end-position
120 _beg_pos = _result.find(_begin, _end_pos);
121
122 // if sequence wasn't found, we are done
123 if(_beg_pos == std::string::npos)
124 break;
125
126 // starting after the position of the first delimiter, find the end sequence
127 if(!_end.empty())
128 _end_pos = _result.find(_end, _beg_pos + 1);
129 else
130 _end_pos = _beg_pos + _begin.length();
131
132 // break if not found
133 if(_end_pos == std::string::npos)
134 break;
135
136 // length of the substr being operated on
137 auto _len = _end_pos - _beg_pos;
138
139 // get the substring between the two delimiters (including first delimiter)
140 auto _sub = _result.substr(_beg_pos, _len);
141
142 // apply the transform
143 auto _transformed = predicate(_sub);
144
145 // only replace if necessary
146 if(_sub != _transformed)
147 {
148 _result = _result.replace(_beg_pos, _len, _transformed);
149 // move end to the end of transformed string
150 _end_pos = _beg_pos + _transformed.length();
151 }
152 }
153 return _result;
154}
155} // namespace tim
Definition: kokkosp.cpp:39
std::string str_transform(const std::string &input, const std::string &_begin, const std::string &_end, PredicateT &&predicate)
apply a string transformation to substring inbetween a common delimiter. e.g.
Definition: delimit.hpp:111
T from_string(const std::string &str)
Definition: delimit.hpp:40
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
ContainerT delimit(const std::string &line, const std::string &delimiters="\"',;: ", PredicateT &&predicate=[](const std::string &s) -> std::string { return s;})
Definition: delimit.hpp:68