libzypp 17.37.17
string.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------*/
9#ifndef ZYPP_NG_CORE_STRING_H_INCLUDED
10#define ZYPP_NG_CORE_STRING_H_INCLUDED
11
12#include <optional>
14#include <boost/utility/string_view.hpp>
15
16namespace zyppng {
17
18namespace str {
19
20 using zypp::str::Trim;
21
22 template <typename T>
23 std::optional<T> safe_strtonum ( const std::string_view &val)
24 {
25 int oerrno = errno;
26 errno = 0; // strtonum/::strtol has no dedicated error-return-code one could check
27 const T entryVal = zypp::str::strtonum<T>( val.data() );
28 if ( errno == ERANGE )
29 return {};
30 errno = oerrno;
31 return entryVal;
32 }
33
34 template< typename StrType, typename T = std::remove_reference_t<StrType> >
35 T trim( StrType&& s, const Trim trim_r )
36 {
37 T ret( std::forward<StrType>(s) );
38
39 if ( ret.empty() || trim_r == Trim::NO_TRIM )
40 return ret;
41
42 if ( trim_r & Trim::L_TRIM )
43 {
44 typename T::size_type p = ret.find_first_not_of( " \t\r\n" );
45 if ( p == T::npos )
46 {
47 if constexpr ( std::is_same_v<std::string_view, StrType> )
48 return T();
49 else {
50 ret.clear();
51 return ret;
52 }
53 }
54 ret.remove_prefix( p );
55 }
56
57 if ( trim_r & Trim::R_TRIM )
58 {
59 typename T::size_type p = ret.find_last_not_of( " \t\r\n" );
60 if ( p == T::npos )
61 {
62 if constexpr ( std::is_same_v<std::string_view, StrType> )
63 return T();
64 else {
65 ret.clear();
66 return ret;
67 }
68 }
69 ret.remove_suffix( ret.size() - ( p+1 ) );
70 }
71
72 return ret;
73 }
74
75 template<class TOutputIterator>
76 void split( const boost::string_view & line_r, TOutputIterator result_r, const boost::string_view & sepchars_r = " \t", const Trim trim_r = Trim::NO_TRIM )
77 {
78 //skip initial sepchars
79 std::string_view::size_type tokenEnd = 0, tokenBegin = line_r.find_first_not_of( sepchars_r );
80
81 //if we do not find a character that is not in sepchars there is nothing to split
82 if ( tokenBegin == std::string_view::npos )
83 return;
84
85 while ( ( tokenEnd = line_r.find_first_of( sepchars_r, tokenBegin ) ) != std::string_view::npos ) {
86 auto line = line_r.substr( tokenBegin, tokenEnd-tokenBegin );
87 *result_r = trim( line, trim_r );
88
89 //find start of next token
90 tokenBegin = line_r.find_first_not_of( sepchars_r, tokenEnd );
91 if( tokenBegin == std::string_view::npos )
92 break;
93 }
94
95 //insert the final element
96 if ( tokenBegin != std::string_view::npos && tokenBegin < line_r.size() )
97 *result_r = trim( line_r.substr( tokenBegin ), trim_r );
98 }
99}
100
101 // use strerror from zypp::str in zyppng
103
104}
105
106
107#endif
String related utilities and Regular expression matching.
Trim
To define how to trim.
Definition String.h:567
@ NO_TRIM
Definition String.h:568
std::string strerror(int errno_r)
Return string describing the error_r code.
Definition String.cc:56
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition String.h:602
TInt strtonum(const C_Str &str)
Parsing numbers from string.
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:226
std::optional< T > safe_strtonum(const std::string_view &val)
Definition string.h:23