| 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 | |
| 12 | namespace CppUtilities { |
| 13 | |
| 14 | class CPP_UTILITIES_EXPORT BufferSearch { |
| 15 | public: |
| 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 | |
| 28 | private: |
| 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 | |
| 41 | /*! |
| 42 | * \brief Constructs a new BufferSearch. Might be overloaded in the future. |
| 43 | */ |
| 44 | inline BufferSearch::BufferSearch( |
| 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 | |
| 57 | /*! |
| 58 | * \brief Processes the specified \a buffer. Invokes the callback according to the remarks mentioned in the class documentation. |
| 59 | */ |
| 60 | inline void BufferSearch::operator()(std::string_view buffer) |
| 61 | { |
| 62 | (*this)(buffer.data(), buffer.size()); |
| 63 | } |
| 64 | |
| 65 | /*! |
| 66 | * \brief Processes the specified \a buffer which is a shared array with fixed \tparam bufferCapacity. Invokes the callback according to the remarks mentioned in the class documentation. |
| 67 | */ |
| 68 | template <std::size_t bufferCapacity> |
| 69 | inline 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 | |
| 74 | /*! |
| 75 | * \brief Processes the specified \a buffer. Invokes the callback according to the remarks mentioned in the class documentation. |
| 76 | * \returns See other overload for details. |
| 77 | */ |
| 78 | inline const std::string_view::value_type *BufferSearch::process(std::string_view buffer) |
| 79 | { |
| 80 | return process(buffer.data(), buffer.size()); |
| 81 | } |
| 82 | |
| 83 | /*! |
| 84 | * \brief Returns the search result at this point. |
| 85 | */ |
| 86 | inline std::string &BufferSearch::result() |
| 87 | { |
| 88 | return m_result; |
| 89 | } |
| 90 | |
| 91 | /*! |
| 92 | * \brief Returns the search result at this point. |
| 93 | */ |
| 94 | inline const std::string &BufferSearch::result() const |
| 95 | { |
| 96 | return m_result; |
| 97 | } |
| 98 | |
| 99 | } // namespace CppUtilities |
| 100 | |
| 101 | #endif // IOUTILITIES_BUFFER_SEARCH_H |