libzypp  17.25.7
StringV.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
11 #include <iostream>
12 #include <zypp/base/StringV.h>
13 
15 namespace zypp
16 {
17  namespace strv
18  {
19 
20  unsigned split( std::string_view line_r, std::string_view sep_r, Trim trim_r,
21  std::function<void(std::string_view)> fnc_r )
22  {
23 #warning REIMPLEMENT
24  std::vector<std::string> words;
25  str::split( std::string(line_r), std::back_inserter(words), std::string(sep_r), str::TRIM );
26 
27  if ( fnc_r ) {
28  for ( const auto & w : words )
29  fnc_r( std::string_view(w) );
30  }
31  return words.size();
32  }
33 
34 
35  unsigned detail::_splitRx( const std::string & line_r, const regex & rx_r, WordConsumer && fnc_r )
36  {
37  // callback stats
38  bool fncStop = false;
39  unsigned fncCall = 0;
40 
41  // report lhs word of separator matches...
42  const char *const eol = line_r.data() + line_r.size(); // the '\0'
43  bool trailingNL = line_r.size() && *(eol-1) == '\n'; // line ends with NL
44  const char * wordstart = line_r.data(); // start of the next string to be reported
45  const char * searchfrom = line_r.data(); // start of the next search for a separator (moves with each cycle!)
46 
47  // Whether to match the ^ at beginning of the line or after an \n:
48  auto matchAtBOL = [&]() {
49  return searchfrom == line_r.data() || *(searchfrom-1) == '\n' ? regex::none : regex::not_bol;
50  };
51  do { // report lhs word of separator matches...
52  smatch match;
53  if ( fncStop || ! rx_r.matches( searchfrom, match, matchAtBOL() ) ) {
54  break;
55  }
56  if ( trailingNL && searchfrom+match.begin(0) == eol )
57  break; // don't report matches behind a trailing NL
58 
59  if ( match.end(0) == 0 && searchfrom == wordstart && searchfrom != line_r.data() ) {
60  // An empty(!) separator found at wordstart is ignored unless we're at the very beginning.
61  // If searchfrom == wordstart we just skipped over a not-empty separator. If wordstart is
62  // not part of a 2nd not-empty separator, we want the next separator to it's right.
63  // Example: Rx:"b*" Str:"0b2" - at pos 2 Rx matches empty; the previous cycle found 'b' and reported the '0'.
64  ++searchfrom;
65  } else {
66  // Report lhs word of the match and advance...
67  if ( fnc_r ) {
68  if ( ! fnc_r( std::string_view( wordstart, searchfrom+match.begin(0) - wordstart ), fncCall, false/*more to come*/ ) )
69  fncStop= true; // stop searching for further matches
70  }
71  ++fncCall;
72  // Next wordstart is behind the separator match.
73  // Next searchfrom also, but advances at least by 1.
74  wordstart = searchfrom+match.end(0);
75  searchfrom += match.end(0) ? match.end(0) : 1;
76  }
77  } while ( searchfrom <= eol ); // incl. '== eol' as there might be an (empty) match at $
78 
79  // finally report rhs word of the last separator match (or no match)
80  if ( fnc_r ) {
81  if ( wordstart < eol )
82  fnc_r( std::string_view( wordstart, eol-wordstart ), fncCall, true/*last*/ );
83  else // a final match at $ so a final empty word reported
84  fnc_r( std::string_view( eol, 0 ), fncCall, true/*last*/ );
85  }
86  ++fncCall;
87  return fncCall;
88  }
89 
90  } // namespace strv
91 } // namespace zypp
c++17: std::string_view tools
Trim
To define how to trim.
Definition: String.h:493
@ TRIM
Definition: String.h:497
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:527
unsigned split(std::string_view line_r, std::string_view sep_r, Trim trim_r, std::function< void(std::string_view)> fnc_r)
Definition: StringV.cc:20
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2