C++ Utilities  5.11.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
buffersearch.h
Go to the documentation of this file.
1 #ifndef IOUTILITIES_BUFFER_SEARCH_H
2 #define IOUTILITIES_BUFFER_SEARCH_H
3 
4 #include "../global.h"
5 #include "../misc/traits.h"
6 
7 #include <array>
8 #include <functional>
9 #include <memory>
10 #include <string>
11 #include <string_view>
12 
13 namespace CppUtilities {
14 
16 public:
17  using CallbackType = std::function<void(BufferSearch &, std::string &&)>;
18  BufferSearch(std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, CallbackType &&callback);
19  void operator()(std::string_view buffer);
20  void operator()(const std::string_view::value_type *buffer, std::size_t bufferSize);
21  template <std::size_t bufferCapacity>
22  void operator()(std::shared_ptr<std::array<std::string_view::value_type, bufferCapacity>> buffer, std::size_t bufferSize);
23  void reset();
24 
25 private:
26  const std::string_view m_searchTerm;
27  const std::string_view m_terminationChars;
28  const std::string_view m_terminationTerm;
29  const std::string_view m_giveUpTerm;
30  const CallbackType m_callback;
31  std::string_view::const_iterator m_searchTermIterator;
32  std::string_view::const_iterator m_giveUpTermIterator;
33  std::string_view::const_iterator m_terminationTermIterator;
34  std::string m_result;
35  bool m_hasResult;
36 };
37 
42  std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, CallbackType &&callback)
43  : m_searchTerm(searchTerm)
44  , m_terminationChars(terminationChars)
45  , m_giveUpTerm(giveUpTerm)
46  , m_callback(std::move(callback))
47  , m_searchTermIterator(m_searchTerm.begin())
48  , m_giveUpTermIterator(m_giveUpTerm.begin())
49  , m_terminationTermIterator(m_terminationTerm.begin())
50  , m_hasResult(false)
51 {
52 }
53 
57 inline void BufferSearch::operator()(std::string_view buffer)
58 {
59  (*this)(buffer.data(), buffer.size());
60 }
61 
65 template <std::size_t bufferCapacity>
66 inline void BufferSearch::operator()(std::shared_ptr<std::array<std::string_view::value_type, bufferCapacity>> buffer, std::size_t bufferSize)
67 {
68  (*this)(buffer->data(), bufferSize);
69 }
70 
71 } // namespace CppUtilities
72 
73 #endif // IOUTILITIES_BUFFER_SEARCH_H
The BufferSearch struct invokes a callback if an initially given search term occurs in consecutively ...
Definition: buffersearch.h:15
void operator()(std::string_view buffer)
Processes the specified buffer.
Definition: buffersearch.h:57
std::function< void(BufferSearch &, std::string &&)> CallbackType
Definition: buffersearch.h:17
BufferSearch(std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, CallbackType &&callback)
Constructs a new BufferSearch.
Definition: buffersearch.h:41
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Contains all utilities provides by the c++utilities library.