C++ Utilities 5.25.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
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
13namespace CppUtilities {
14
16public:
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 const std::string_view::value_type *process(std::string_view buffer);
24 const std::string_view::value_type *process(const std::string_view::value_type *buffer, std::size_t bufferSize);
25 void reset();
26 std::string &result();
27 const std::string &result() const;
28
29private:
30 const std::string_view m_searchTerm;
31 const std::string_view m_terminationChars;
32 const std::string_view m_terminationTerm;
33 const std::string_view m_giveUpTerm;
34 const CallbackType m_callback;
35 std::string_view::const_iterator m_searchTermIterator;
36 std::string_view::const_iterator m_giveUpTermIterator;
37 std::string_view::const_iterator m_terminationTermIterator;
38 std::string m_result;
39 bool m_hasResult;
40};
41
46 std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, CallbackType &&callback)
47 : m_searchTerm(searchTerm)
48 , m_terminationChars(terminationChars)
49 , m_giveUpTerm(giveUpTerm)
50 , m_callback(std::move(callback))
51 , m_searchTermIterator(m_searchTerm.begin())
52 , m_giveUpTermIterator(m_giveUpTerm.begin())
53 , m_terminationTermIterator(m_terminationTerm.begin())
54 , m_hasResult(false)
55{
56}
57
61inline void BufferSearch::operator()(std::string_view buffer)
62{
63 (*this)(buffer.data(), buffer.size());
64}
65
69template <std::size_t bufferCapacity>
70inline void BufferSearch::operator()(std::shared_ptr<std::array<std::string_view::value_type, bufferCapacity>> buffer, std::size_t bufferSize)
71{
72 (*this)(buffer->data(), bufferSize);
73}
74
79inline const std::string_view::value_type *BufferSearch::process(std::string_view buffer)
80{
81 return process(buffer.data(), buffer.size());
82}
83
87inline std::string &BufferSearch::result()
88{
89 return m_result;
90}
91
95inline const std::string &BufferSearch::result() const
96{
97 return m_result;
98}
99
100} // namespace CppUtilities
101
102#endif // IOUTILITIES_BUFFER_SEARCH_H
The BufferSearch struct invokes a callback if an initially given search term occurs in consecutively ...
void operator()(std::string_view buffer)
Processes the specified buffer.
const std::string_view::value_type * process(std::string_view buffer)
Processes the specified buffer.
std::function< void(BufferSearch &, std::string &&)> CallbackType
BufferSearch(std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, CallbackType &&callback)
Constructs a new BufferSearch.
std::string & result()
Returns the search result at this point.
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
Contains all utilities provides by the c++utilities library.
STL namespace.