1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#ifndef IOUTILITIES_BUFFER_SEARCH_H
#define IOUTILITIES_BUFFER_SEARCH_H
#include "../global.h"
#include <array>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
namespace CppUtilities {
class CPP_UTILITIES_EXPORT BufferSearch {
public:
using CallbackType = std::function<void(BufferSearch &, std::string &&)>;
BufferSearch(std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, CallbackType &&callback);
void operator()(std::string_view buffer);
void operator()(const std::string_view::value_type *buffer, std::size_t bufferSize);
template <std::size_t bufferCapacity>
void operator()(std::shared_ptr<std::array<std::string_view::value_type, bufferCapacity>> buffer, std::size_t bufferSize);
const std::string_view::value_type *process(std::string_view buffer);
const std::string_view::value_type *process(const std::string_view::value_type *buffer, std::size_t bufferSize);
void reset();
std::string &result();
const std::string &result() const;
private:
const std::string_view m_searchTerm;
const std::string_view m_terminationChars;
const std::string_view m_terminationTerm;
const std::string_view m_giveUpTerm;
const CallbackType m_callback;
std::string_view::const_iterator m_searchTermIterator;
std::string_view::const_iterator m_giveUpTermIterator;
std::string_view::const_iterator m_terminationTermIterator;
std::string m_result;
bool m_hasResult;
};
/*!
* \brief Constructs a new BufferSearch. Might be overloaded in the future.
*/
inline BufferSearch::BufferSearch(
std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, CallbackType &&callback)
: m_searchTerm(searchTerm)
, m_terminationChars(terminationChars)
, m_giveUpTerm(giveUpTerm)
, m_callback(std::move(callback))
, m_searchTermIterator(m_searchTerm.begin())
, m_giveUpTermIterator(m_giveUpTerm.begin())
, m_terminationTermIterator(m_terminationTerm.begin())
, m_hasResult(false)
{
}
/*!
* \brief Processes the specified \a buffer. Invokes the callback according to the remarks mentioned in the class documentation.
*/
inline void BufferSearch::operator()(std::string_view buffer)
{
(*this)(buffer.data(), buffer.size());
}
/*!
* \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.
*/
template <std::size_t bufferCapacity>
inline void BufferSearch::operator()(std::shared_ptr<std::array<std::string_view::value_type, bufferCapacity>> buffer, std::size_t bufferSize)
{
(*this)(buffer->data(), bufferSize);
}
/*!
* \brief Processes the specified \a buffer. Invokes the callback according to the remarks mentioned in the class documentation.
* \returns See other overload for details.
*/
inline const std::string_view::value_type *BufferSearch::process(std::string_view buffer)
{
return process(buffer.data(), buffer.size());
}
/*!
* \brief Returns the search result at this point.
*/
inline std::string &BufferSearch::result()
{
return m_result;
}
/*!
* \brief Returns the search result at this point.
*/
inline const std::string &BufferSearch::result() const
{
return m_result;
}
} // namespace CppUtilities
#endif // IOUTILITIES_BUFFER_SEARCH_H
|