12 #ifndef ZYPP_BASE_STRINGV_H 13 #define ZYPP_BASE_STRINGV_H 14 #include <string_view> 15 #ifdef __cpp_lib_string_view 17 #include <zypp-core/base/String.h> 18 #include <zypp-core/base/Regex.h> 19 #include <zypp-core/base/Flags.h> 26 using regex = str::regex;
27 using smatch = str::smatch;
40 inline constexpr std::string_view blank =
" \t";
43 inline std::string_view
ltrim( std::string_view str_r, std::string_view chars_r = blank )
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() );
51 str_r.remove_prefix( pos );
56 inline std::string_view
rtrim( std::string_view str_r, std::string_view chars_r = blank )
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 );
69 inline std::string_view
trim( std::string_view str_r, std::string_view chars_r = blank )
73 str_r =
ltrim( std::move(str_r), chars_r );
74 str_r =
rtrim( std::move(str_r), chars_r );
79 inline std::string_view
trim( std::string_view str_r, std::string_view chars_r, TrimFlag trim_r )
81 if ( str_r.empty() || trim_r == Trim::notrim )
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 );
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) ); }
94 inline bool hasPrefix( std::string_view str_r, std::string_view prefix_r )
95 {
return( ::strncmp( str_r.data(), prefix_r.data(), prefix_r.size() ) == 0 ); }
98 inline bool hasPrefixCI( std::string_view str_r, std::string_view prefix_r )
99 {
return( ::strncasecmp( str_r.data(), prefix_r.data(), prefix_r.size() ) == 0 ); }
105 using WordConsumer = std::function<bool(std::string_view,unsigned,bool)>;
122 template <
typename Callable, std::enable_if_t<
123 std::is_invocable_r_v<bool,Callable,std::string_view,unsigned,bool>
125 WordConsumer wordConsumer( Callable && fnc_r )
126 {
return std::forward<Callable>(fnc_r); }
128 template <
typename Callable, std::enable_if_t<
129 ! std::is_invocable_r_v<bool,Callable,std::string_view,unsigned,bool>
130 && std::is_invocable_r_v<void,Callable,std::string_view,unsigned,bool>
132 WordConsumer wordConsumer( Callable && fnc_r )
133 {
return [&fnc_r](std::string_view a1,
unsigned a2,
bool a3)->
bool { fnc_r(a1,a2,a3);
return true; }; }
136 template <
typename Callable, std::enable_if_t<
137 std::is_invocable_r_v<bool,Callable,std::string_view,unsigned>
139 WordConsumer wordConsumer( Callable && fnc_r )
140 {
return [&fnc_r](std::string_view a1,
unsigned a2,
bool)->
bool {
return fnc_r(a1,a2); }; }
142 template <
typename Callable, std::enable_if_t<
143 ! std::is_invocable_r_v<bool,Callable,std::string_view,unsigned>
144 && std::is_invocable_r_v<void,Callable,std::string_view,unsigned>
146 WordConsumer wordConsumer( Callable && fnc_r )
147 {
return [&fnc_r](std::string_view a1,
unsigned a2,
bool)->
bool { fnc_r(a1,a2);
return true; }; }
150 template <
typename Callable, std::enable_if_t<
151 std::is_invocable_r_v<bool,Callable,std::string_view>
153 WordConsumer wordConsumer( Callable && fnc_r )
154 {
return [&fnc_r](std::string_view a1,unsigned,
bool)->
bool {
return fnc_r(a1); }; }
156 template <
typename Callable, std::enable_if_t<
157 ! std::is_invocable_r_v<bool,Callable,std::string_view>
158 && std::is_invocable_r_v<void,Callable,std::string_view>
160 WordConsumer wordConsumer( Callable && fnc_r )
161 {
return [&fnc_r](std::string_view a1,unsigned,
bool)->
bool { fnc_r(a1);
return true; }; }
164 template <
typename Callable, std::enable_if_t<
165 std::is_invocable_r_v<bool,Callable>
167 WordConsumer wordConsumer( Callable && fnc_r )
168 {
return [&fnc_r](std::string_view,unsigned,
bool)->
bool {
return fnc_r(); }; }
170 template <
typename Callable, std::enable_if_t<
171 ! std::is_invocable_r_v<bool,Callable>
172 && std::is_invocable_r_v<void,Callable>
174 WordConsumer wordConsumer( Callable && fnc_r )
175 {
return [&fnc_r](std::string_view,unsigned,
bool)->
bool { fnc_r();
return true; }; }
179 unsigned _split( std::string_view line_r, std::string_view sep_r,
Trim trim_r, WordConsumer && fnc_r );
182 unsigned _splitRx(
const std::string & line_r,
const regex & rx_r, WordConsumer && fnc_r );
207 template <
typename Callable = detail::WordConsumer>
208 unsigned splitRx(
const std::string & line_r,
const regex & rx_r, Callable && fnc_r = Callable() )
209 {
return detail::_splitRx( line_r, rx_r, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
272 template <
typename Callable = detail::WordConsumer>
273 unsigned split( std::string_view line_r, std::string_view sep_r,
Trim trim_r, Callable && fnc_r = Callable() )
274 {
return detail::_split( line_r, sep_r, trim_r, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
277 template <
typename Callable = detail::WordConsumer>
278 inline unsigned split( std::string_view line_r, std::string_view sep_r, Callable && fnc_r = Callable() )
279 {
return detail::_split( line_r, sep_r, Trim::notrim, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
282 template <
typename Callable = detail::WordConsumer>
283 inline unsigned split( std::string_view line_r, Callable && fnc_r = Callable() )
284 {
return detail::_split( line_r, std::string_view(), Trim::notrim, detail::wordConsumer( std::forward<Callable>(fnc_r) ) ); }
286 inline std::string_view asStringView(
const char * t )
287 {
return t ==
nullptr ? std::string_view() : t; }
292 #endif // __cpp_lib_string_view 293 #endif // ZYPP_BASE_STRINGV_H bool hasPrefixCI(const C_Str &str_r, const C_Str &prefix_r)
Trim
To define how to trim.
std::string ltrim(const std::string &s)
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, const Trim trim_r=NO_TRIM)
Split line_r into words.
std::string trim(const std::string &s, const Trim trim_r)
#define ZYPP_DECLARE_FLAGS_AND_OPERATORS(Name, Enum)
std::string rtrim(const std::string &s)
Easy-to use interface to the ZYPP dependency resolver.
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.