libzypp  17.32.5
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>
13 #include <zypp-core/base/String.h>
14 #include <boost/utility/string_view.hpp>
15 
16 namespace zyppng {
17 
18 namespace 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  errno = 0;
26  const int entryVal = zypp::str::strtonum<T>( val.data() );
27  if ( errno == ERANGE )
28  return {};
29  return entryVal;
30  }
31 
32  template< typename StrType, typename T = std::remove_reference_t<StrType> >
33  T trim( StrType&& s, const Trim trim_r )
34  {
35  T ret( std::forward<StrType>(s) );
36 
37  if ( ret.empty() || trim_r == Trim::NO_TRIM )
38  return ret;
39 
40  if ( trim_r & Trim::L_TRIM )
41  {
42  typename T::size_type p = ret.find_first_not_of( " \t\r\n" );
43  if ( p == T::npos )
44  {
45  if constexpr ( std::is_same_v<std::string_view, StrType> )
46  return T();
47  else {
48  ret.clear();
49  return ret;
50  }
51  }
52  ret.remove_prefix( p );
53  }
54 
55  if ( trim_r & Trim::R_TRIM )
56  {
57  typename T::size_type p = ret.find_last_not_of( " \t\r\n" );
58  if ( p == T::npos )
59  {
60  if constexpr ( std::is_same_v<std::string_view, StrType> )
61  return T();
62  else {
63  ret.clear();
64  return ret;
65  }
66  }
67  ret.remove_suffix( ret.size() - ( p+1 ) );
68  }
69 
70  return ret;
71  }
72 
73  template<class TOutputIterator>
74  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 )
75  {
76  //skip initial sepchars
77  std::string_view::size_type tokenEnd = 0, tokenBegin = line_r.find_first_not_of( sepchars_r );
78 
79  //if we do not find a character that is not in sepchars there is nothing to split
80  if ( tokenBegin == std::string_view::npos )
81  return;
82 
83  while ( ( tokenEnd = line_r.find_first_of( sepchars_r, tokenBegin ) ) != std::string_view::npos ) {
84  auto line = line_r.substr( tokenBegin, tokenEnd-tokenBegin );
85  *result_r = trim( line, trim_r );
86 
87  //find start of next token
88  tokenBegin = line_r.find_first_not_of( sepchars_r, tokenEnd );
89  if( tokenBegin == std::string_view::npos )
90  break;
91  }
92 
93  //insert the final element
94  if ( tokenBegin != std::string_view::npos && tokenBegin < line_r.size() )
95  *result_r = trim( line_r.substr( tokenBegin ), trim_r );
96  }
97 }
98 
99  // use strerror from zypp::str in zyppng
100  using zypp::str::strerror;
101 
102 }
103 
104 
105 #endif
T trim(StrType &&s, const Trim trim_r)
Definition: string.h:33
void split(const boost::string_view &line_r, TOutputIterator result_r, const boost::string_view &sepchars_r=" \, const Trim trim_r=Trim::NO_TRIM)
Definition: string.h:74
String related utilities and Regular expression matching.
Trim
To define how to trim.
Definition: String.h:496
std::optional< T > safe_strtonum(const std::string_view &val)
Definition: string.h:23
std::string strerror(int errno_r)
Return string describing the error_r code.
Definition: String.cc:54
SolvableIdType size_type
Definition: PoolMember.h:126