C++ Utilities 5.31.1
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
6#include <array>
7#include <functional>
8#include <memory>
9#include <string>
10#include <string_view>
11
12namespace CppUtilities {
13
15public:
16 using CallbackType = std::function<void(BufferSearch &, std::string &&)>;
17 BufferSearch(std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, CallbackType &&callback);
18 void operator()(std::string_view buffer);
19 void operator()(const std::string_view::value_type *buffer, std::size_t bufferSize);
20 template <std::size_t bufferCapacity>
21 void operator()(std::shared_ptr<std::array<std::string_view::value_type, bufferCapacity>> buffer, std::size_t bufferSize);
22 const std::string_view::value_type *process(std::string_view buffer);
23 const std::string_view::value_type *process(const std::string_view::value_type *buffer, std::size_t bufferSize);
24 void reset();
25 std::string &result();
26 const std::string &result() const;
27
28private:
29 const std::string_view m_searchTerm;
30 const std::string_view m_terminationChars;
31 const std::string_view m_terminationTerm;
32 const std::string_view m_giveUpTerm;
33 const CallbackType m_callback;
34 std::string_view::const_iterator m_searchTermIterator;
35 std::string_view::const_iterator m_giveUpTermIterator;
36 std::string_view::const_iterator m_terminationTermIterator;
37 std::string m_result;
38 bool m_hasResult;
39};
40
45 std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, CallbackType &&callback)
46 : m_searchTerm(searchTerm)
47 , m_terminationChars(terminationChars)
48 , m_giveUpTerm(giveUpTerm)
49 , m_callback(std::move(callback))
50 , m_searchTermIterator(m_searchTerm.begin())
51 , m_giveUpTermIterator(m_giveUpTerm.begin())
52 , m_terminationTermIterator(m_terminationTerm.begin())
53 , m_hasResult(false)
54{
55}
56
60inline void BufferSearch::operator()(std::string_view buffer)
61{
62 (*this)(buffer.data(), buffer.size());
63}
64
68template <std::size_t bufferCapacity>
69inline void BufferSearch::operator()(std::shared_ptr<std::array<std::string_view::value_type, bufferCapacity>> buffer, std::size_t bufferSize)
70{
71 (*this)(buffer->data(), bufferSize);
72}
73
78inline const std::string_view::value_type *BufferSearch::process(std::string_view buffer)
79{
80 return process(buffer.data(), buffer.size());
81}
82
86inline std::string &BufferSearch::result()
87{
88 return m_result;
89}
90
94inline const std::string &BufferSearch::result() const
95{
96 return m_result;
97}
98
99} // namespace CppUtilities
100
101#endif // IOUTILITIES_BUFFER_SEARCH_H
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.
void reset()
Resets the search to its initial state (assuming no characters of the search term or give-up term hav...
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 provided by the c++utilities library.
STL namespace.