LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
tokenize.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
11 #include <QStringView>
12 
13 namespace LC::Util
14 {
15  struct Tokenize
16  {
17  QStringView Str_;
18  QChar Ch_;
19 
20  struct Iterator
21  {
22  using difference_type = qsizetype;
23  using value_type = QStringView;
24  using reference = QStringView;
25  using pointer = void;
26  using iterator_category = std::bidirectional_iterator_tag;
27 
28  const Tokenize& Tok_;
29  qsizetype CurStart_;
30  qsizetype NextStart_;
31 
32  QStringView operator* () const
33  {
34  return NextStart_ >= 0 ?
35  Tok_.Str_.mid (CurStart_, NextStart_ - CurStart_ - 1) :
36  Tok_.Str_.mid (CurStart_);
37  }
38 
40  {
42  if (CurStart_ == -1)
43  return *this;
44 
45  NextStart_ = Tok_.Str_.indexOf (Tok_.Ch_, NextStart_);
46  if (NextStart_ >= 0)
47  ++NextStart_;
48 
49  return *this;
50  }
51 
53  {
54  if (CurStart_ == 0)
55  return *this;
56 
58 
59  if (CurStart_ == 1)
60  CurStart_ = 0;
61  else if (CurStart_ >= 0) // actually implies CurStart_ > 1
62  CurStart_ = Tok_.Str_.lastIndexOf (Tok_.Ch_, CurStart_ - 2) + 1;
63  else
64  CurStart_ = Tok_.Str_.lastIndexOf (Tok_.Ch_) + 1;
65 
66  return *this;
67  }
68 
69  bool operator== (const Iterator& other) const
70  {
71  return CurStart_ == other.CurStart_ && NextStart_ == other.NextStart_;
72  }
73  };
74 
75  Iterator begin () const
76  {
77  Iterator it { *this, -1, 0 };
78  return ++it;
79  }
80 
81  Iterator end () const
82  {
83  return { *this, -1, -1 };
84  }
85 
86  auto rbegin () const
87  {
88  return std::reverse_iterator { end () };
89  }
90 
91  auto rend () const
92  {
93  return std::reverse_iterator { begin () };
94  }
95  };
96 }
auto rend() const
Definition: tokenize.h:91
bool operator==(const Iterator &other) const
Definition: tokenize.h:69
Iterator end() const
Definition: tokenize.h:81
std::bidirectional_iterator_tag iterator_category
Definition: tokenize.h:26
const Tokenize & Tok_
Definition: tokenize.h:28
auto rbegin() const
Definition: tokenize.h:86
QStringView Str_
Definition: tokenize.h:17
QStringView operator*() const
Definition: tokenize.h:32
Iterator begin() const
Definition: tokenize.h:75