libzypp 17.31.6
StringV.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_BASE_STRINGV_H
13#define ZYPP_BASE_STRINGV_H
14#include <string_view>
15#ifdef __cpp_lib_string_view
16
17#include <zypp-core/base/String.h>
18#include <zypp-core/base/Regex.h>
19#include <zypp-core/base/Flags.h>
20
22namespace zypp
23{
24 namespace strv
25 {
26 using regex = str::regex;
27 using smatch = str::smatch;
28
30 enum class Trim {
31 notrim = 0,
32 left = 1<<0,
33 right = 1<<1,
34 trim = (left|right),
35 };
36
37 ZYPP_DECLARE_FLAGS_AND_OPERATORS( TrimFlag, Trim );
38
40 inline constexpr std::string_view blank = " \t";
41
43 inline std::string_view ltrim( std::string_view str_r, std::string_view chars_r = blank )
44 {
45 if ( str_r.empty() )
46 return str_r;
47 auto pos = str_r.find_first_not_of( chars_r );
48 if ( pos == str_r.npos )
49 str_r.remove_prefix( str_r.size() );
50 else if ( pos )
51 str_r.remove_prefix( pos );
52 return str_r;
53 }
54
56 inline std::string_view rtrim( std::string_view str_r, std::string_view chars_r = blank )
57 {
58 if ( str_r.empty() )
59 return str_r;
60 auto pos = str_r.find_last_not_of( chars_r );
61 if ( pos == str_r.npos )
62 str_r.remove_suffix( str_r.size() );
63 else if ( (pos = str_r.size()-1-pos) )
64 str_r.remove_suffix( pos );
65 return str_r;
66 }
67
69 inline std::string_view trim( std::string_view str_r, std::string_view chars_r = blank )
70 {
71 if ( str_r.empty() )
72 return str_r;
73 str_r = ltrim( std::move(str_r), chars_r );
74 str_r = rtrim( std::move(str_r), chars_r );
75 return str_r;
76 }
77
79 inline std::string_view trim( std::string_view str_r, std::string_view chars_r, TrimFlag trim_r )
80 {
81 if ( str_r.empty() || trim_r == Trim::notrim )
82 return str_r;
83 if ( trim_r.testFlag( Trim::left ) )
84 str_r = ltrim( std::move(str_r), chars_r );
85 if ( trim_r.testFlag( Trim::right ) )
86 str_r = rtrim( std::move(str_r), chars_r );
87 return str_r;
88 }
90 inline std::string_view trim( std::string_view str_r, TrimFlag trim_r )
91 { return trim( std::move(str_r), blank, std::move(trim_r) ); }
92
94 namespace detail
95 {
97 using WordConsumer = std::function<bool(std::string_view,unsigned,bool)>;
98
114 template <typename Callable, std::enable_if_t<
115 std::is_invocable_r_v<bool,Callable,std::string_view,unsigned,bool>
116 , bool> = true>
117 WordConsumer wordConsumer( Callable && fnc_r )
118 { return std::forward<Callable>(fnc_r); }
120 template <typename Callable, std::enable_if_t<
121 ! std::is_invocable_r_v<bool,Callable,std::string_view,unsigned,bool>
122 && std::is_invocable_r_v<void,Callable,std::string_view,unsigned,bool>
123 , bool> = true>
124 WordConsumer wordConsumer( Callable && fnc_r )
125 { return [&fnc_r](std::string_view a1,unsigned a2,bool a3)->bool { fnc_r(a1,a2,a3); return true; }; }
126
128 template <typename Callable, std::enable_if_t<
129 std::is_invocable_r_v<bool,Callable,std::string_view,unsigned>
130 , bool> = true>
131 WordConsumer wordConsumer( Callable && fnc_r )
132 { return [&fnc_r](std::string_view a1,unsigned a2,bool)->bool { return fnc_r(a1,a2); }; }
134 template <typename Callable, std::enable_if_t<
135 ! std::is_invocable_r_v<bool,Callable,std::string_view,unsigned>
136 && std::is_invocable_r_v<void,Callable,std::string_view,unsigned>
137 , bool> = true>
138 WordConsumer wordConsumer( Callable && fnc_r )
139 { return [&fnc_r](std::string_view a1,unsigned a2,bool)->bool { fnc_r(a1,a2); return true; }; }
140
142 template <typename Callable, std::enable_if_t<
143 std::is_invocable_r_v<bool,Callable,std::string_view>
144 , bool> = true>
145 WordConsumer wordConsumer( Callable && fnc_r )
146 { return [&fnc_r](std::string_view a1,unsigned,bool)->bool { return fnc_r(a1); }; }
148 template <typename Callable, std::enable_if_t<
149 ! std::is_invocable_r_v<bool,Callable,std::string_view>
150 && std::is_invocable_r_v<void,Callable,std::string_view>
151 , bool> = true>
152 WordConsumer wordConsumer( Callable && fnc_r )
153 { return [&fnc_r](std::string_view a1,unsigned,bool)->bool { fnc_r(a1); return true; }; }
154
156 template <typename Callable, std::enable_if_t<
157 std::is_invocable_r_v<bool,Callable>
158 , bool> = true>
159 WordConsumer wordConsumer( Callable && fnc_r )
160 { return [&fnc_r](std::string_view,unsigned,bool)->bool { return fnc_r(); }; }
162 template <typename Callable, std::enable_if_t<
163 ! std::is_invocable_r_v<bool,Callable>
164 && std::is_invocable_r_v<void,Callable>
165 , bool> = true>
166 WordConsumer wordConsumer( Callable && fnc_r )
167 { return [&fnc_r](std::string_view,unsigned,bool)->bool { fnc_r(); return true; }; }
169
171 unsigned _split( std::string_view line_r, std::string_view sep_r, Trim trim_r, WordConsumer && fnc_r );
172
174 unsigned _splitRx( const std::string & line_r, const regex & rx_r, WordConsumer && fnc_r );
175
176 } // namespace detail
178
199 template <typename Callable = detail::WordConsumer>
200 unsigned splitRx( const std::string & line_r, const regex & rx_r, Callable && fnc_r = Callable() )
201 { return detail::_splitRx( line_r, rx_r, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
202
203
264 template <typename Callable = detail::WordConsumer>
265 unsigned split( std::string_view line_r, std::string_view sep_r, Trim trim_r, Callable && fnc_r = Callable() )
266 { return detail::_split( line_r, sep_r, trim_r, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
267
269 template <typename Callable = detail::WordConsumer>
270 inline unsigned split( std::string_view line_r, std::string_view sep_r, Callable && fnc_r = Callable() )
271 { return detail::_split( line_r, sep_r, Trim::notrim, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
272
274 template <typename Callable = detail::WordConsumer>
275 inline unsigned split( std::string_view line_r, Callable && fnc_r = Callable() )
276 { return detail::_split( line_r, std::string_view(), Trim::notrim, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
277
278 inline std::string_view asStringView( const char * t )
279 { return t == nullptr ? std::string_view() : t; }
280
281 } // namespace strv
282} // namespace zypp
284#endif // __cpp_lib_string_view
285#endif // ZYPP_BASE_STRINGV_H
Trim
To define how to trim.
Definition: String.h:496
std::string rtrim(const std::string &s)
Definition: String.h:511
std::string ltrim(const std::string &s)
Definition: String.h:506
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:223
void split(ParamVec &pvec, const std::string &pstr, const std::string &psep)
Split into a parameter vector.
Definition: UrlUtils.cc:165
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
#define ZYPP_DECLARE_FLAGS_AND_OPERATORS(Name, Enum)
Definition: Flags.h:189