12#ifndef ZYPP_BASE_STRINGV_H
13#define ZYPP_BASE_STRINGV_H
15#ifdef __cpp_lib_string_view
18#include <zypp-core/base/String.h>
19#include <zypp-core/base/Regex.h>
20#include <zypp-core/base/Flags.h>
27 using regex = str::regex;
28 using smatch = str::smatch;
41 inline constexpr std::string_view blank =
" \t";
44 inline std::string_view
ltrim( std::string_view str_r, std::string_view chars_r = blank )
48 auto pos = str_r.find_first_not_of( chars_r );
49 if ( pos == str_r.npos )
50 str_r.remove_prefix( str_r.size() );
52 str_r.remove_prefix( pos );
57 inline std::string_view
rtrim( std::string_view str_r, std::string_view chars_r = blank )
61 auto pos = str_r.find_last_not_of( chars_r );
62 if ( pos == str_r.npos )
63 str_r.remove_suffix( str_r.size() );
64 else if ( (pos = str_r.size()-1-pos) )
65 str_r.remove_suffix( pos );
70 inline std::string_view
trim( std::string_view str_r, std::string_view chars_r = blank )
74 str_r =
ltrim( std::move(str_r), chars_r );
75 str_r =
rtrim( std::move(str_r), chars_r );
80 inline std::string_view
trim( std::string_view str_r, std::string_view chars_r, TrimFlag trim_r )
82 if ( str_r.empty() || trim_r == Trim::notrim )
84 if ( trim_r.testFlag( Trim::left ) )
85 str_r =
ltrim( std::move(str_r), chars_r );
86 if ( trim_r.testFlag( Trim::right ) )
87 str_r =
rtrim( std::move(str_r), chars_r );
91 inline std::string_view
trim( std::string_view str_r, TrimFlag trim_r )
92 {
return trim( std::move(str_r), blank, std::move(trim_r) ); }
95 inline bool hasPrefix( std::string_view str_r, std::string_view prefix_r )
96 {
return( ::strncmp( str_r.data(), prefix_r.data(), prefix_r.size() ) == 0 ); }
99 inline bool hasPrefixCI( std::string_view str_r, std::string_view prefix_r )
100 {
return( ::strncasecmp( str_r.data(), prefix_r.data(), prefix_r.size() ) == 0 ); }
106 using WordConsumer = std::function<
bool(std::string_view,
unsigned,
bool)>;
123 template <
typename Callable, std::enable_if_t<
124 std::is_invocable_r_v<bool,Callable,std::string_view,unsigned,bool>
126 WordConsumer wordConsumer( Callable && fnc_r )
127 {
return std::forward<Callable>(fnc_r); }
129 template <
typename Callable, std::enable_if_t<
130 ! std::is_invocable_r_v<bool,Callable,std::string_view,unsigned,bool>
131 && std::is_invocable_r_v<void,Callable,std::string_view,unsigned,bool>
133 WordConsumer wordConsumer( Callable && fnc_r )
134 {
return [&fnc_r](std::string_view a1,
unsigned a2,
bool a3)->
bool { fnc_r(a1,a2,a3);
return true; }; }
137 template <
typename Callable, std::enable_if_t<
138 std::is_invocable_r_v<bool,Callable,std::string_view,unsigned>
140 WordConsumer wordConsumer( Callable && fnc_r )
141 {
return [&fnc_r](std::string_view a1,
unsigned a2,
bool)->
bool {
return fnc_r(a1,a2); }; }
143 template <
typename Callable, std::enable_if_t<
144 ! std::is_invocable_r_v<bool,Callable,std::string_view,unsigned>
145 && std::is_invocable_r_v<void,Callable,std::string_view,unsigned>
147 WordConsumer wordConsumer( Callable && fnc_r )
148 {
return [&fnc_r](std::string_view a1,
unsigned a2,
bool)->
bool { fnc_r(a1,a2);
return true; }; }
151 template <
typename Callable, std::enable_if_t<
152 std::is_invocable_r_v<bool,Callable,std::string_view>
154 WordConsumer wordConsumer( Callable && fnc_r )
155 {
return [&fnc_r](std::string_view a1,unsigned,
bool)->
bool {
return fnc_r(a1); }; }
157 template <
typename Callable, std::enable_if_t<
158 ! std::is_invocable_r_v<bool,Callable,std::string_view>
159 && std::is_invocable_r_v<void,Callable,std::string_view>
161 WordConsumer wordConsumer( Callable && fnc_r )
162 {
return [&fnc_r](std::string_view a1,unsigned,
bool)->
bool { fnc_r(a1);
return true; }; }
165 template <
typename Callable, std::enable_if_t<
166 std::is_invocable_r_v<bool,Callable>
168 WordConsumer wordConsumer( Callable && fnc_r )
169 {
return [&fnc_r](std::string_view,unsigned,
bool)->
bool {
return fnc_r(); }; }
171 template <
typename Callable, std::enable_if_t<
172 ! std::is_invocable_r_v<bool,Callable>
173 && std::is_invocable_r_v<void,Callable>
175 WordConsumer wordConsumer( Callable && fnc_r )
176 {
return [&fnc_r](std::string_view,unsigned,
bool)->
bool { fnc_r();
return true; }; }
180 unsigned _split( std::string_view line_r, std::string_view sep_r, Trim trim_r, WordConsumer && fnc_r );
183 unsigned _splitRx( std::string_view line_r,
const regex & rx_r, WordConsumer && fnc_r );
185 unsigned _splitRx(
const std::string & line_r,
const regex & rx_r, WordConsumer && fnc_r );
210 template <
typename Callable = detail::WordConsumer>
211 unsigned splitRx( std::string_view line_r,
const regex & rx_r, Callable && fnc_r = Callable() )
212 {
return detail::_splitRx( line_r, rx_r, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
275 template <
typename Callable = detail::WordConsumer>
276 unsigned split( std::string_view line_r, std::string_view sep_r, Trim trim_r, Callable && fnc_r = Callable() )
277 {
return detail::_split( line_r, sep_r, trim_r, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
280 template <
typename Callable = detail::WordConsumer>
281 inline unsigned split( std::string_view line_r, std::string_view sep_r, Callable && fnc_r = Callable() )
282 {
return detail::_split( line_r, sep_r, Trim::notrim, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
285 template <
typename Callable = detail::WordConsumer>
286 inline unsigned split( std::string_view line_r, Callable && fnc_r = Callable() )
287 {
return detail::_split( line_r, std::string_view(), Trim::notrim, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
289 inline std::string_view asStringView(
const char * t )
290 {
return t ==
nullptr ? std::string_view() : t; }
Provides API related macros.
Trim
To define how to trim.
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
std::string rtrim(const std::string &s)
std::string ltrim(const std::string &s)
bool hasPrefixCI(const C_Str &str_r, const C_Str &prefix_r)
std::string trim(const std::string &s, const Trim trim_r)
void split(ParamVec &pvec, const std::string &pstr, const std::string &psep)
Split into a parameter vector.
Easy-to use interface to the ZYPP dependency resolver.
#define ZYPP_DECLARE_FLAGS_AND_OPERATORS(Name, Enum)