| 1 | #include "./buffersearch.h" |
| 2 | |
| 3 | using namespace std; |
| 4 | |
| 5 | namespace CppUtilities { |
| 6 | |
| 7 | /*! |
| 8 | * \class BufferSearch |
| 9 | * \brief The BufferSearch struct invokes a callback if an initially given search term occurs in consecutively provided buffers. |
| 10 | * \remarks |
| 11 | * - The class works without making internal copies of the specified buffers, except for the search result. |
| 12 | * - The callback is invoked after the search term has been found and one of the specified termination characters occurred. The |
| 13 | * search result is passed to the callback. |
| 14 | * - The "search result" is the data after the last character of the search term and before any of the specified termination |
| 15 | * characters. |
| 16 | * - If no termination characters are specified, the callback is invoked directly after the search term occurred (with an empty |
| 17 | * search result). |
| 18 | * - If the specified give-up term has occurred, operator() will exit early and the specified callback will not be invoked |
| 19 | * anymore. |
| 20 | * - If the callback has been invoked, operator() will exit early and the callback will not be invoked anymore (even if the |
| 21 | * search term occurs again). Call reset() after consuming the result within the callback to continue the search. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /*! |
| 26 | * \brief Processes the specified \a buffer. Invokes the callback according to the remarks mentioned in the class documentation. |
| 27 | * \returns |
| 28 | * - Returns the offset in \a buffer after the search term and search result. This is the first character after the search term if |
| 29 | * no termination characters have been specified; otherwiese it is the offset of the termination character. |
| 30 | * - Returns nullptr if the search term could not be found. |
| 31 | */ |
| 32 | const std::string_view::value_type *BufferSearch::process(const std::string_view::value_type *buffer, std::size_t bufferSize) |
| 33 | { |
| 34 | if (m_hasResult || (!m_giveUpTerm.empty() && m_giveUpTermIterator == m_giveUpTerm.end())) { |
| 35 | return nullptr; |
| 36 | } |
| 37 | for (auto i = buffer, end = buffer + bufferSize; i != end; ++i) { |
| 38 | const auto currentChar = *i; |
| 39 | if (m_searchTermIterator == m_searchTerm.end()) { |
| 40 | if (m_terminationChars.empty()) { |
| 41 | m_hasResult = true; |
| 42 | } else { |
| 43 | for (const auto &terminationChar : m_terminationChars) { |
| 44 | if (currentChar == terminationChar) { |
| 45 | m_hasResult = true; |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | if (m_hasResult) { |
| 51 | if (m_callback) { |
| 52 | m_callback(*this, std::move(m_result)); |
| 53 | } |
| 54 | return i; |
| 55 | } |
| 56 | m_result += currentChar; |
| 57 | continue; |
| 58 | } |
| 59 | if (currentChar == *m_searchTermIterator) { |
| 60 | ++m_searchTermIterator; |
| 61 | } else { |
| 62 | m_searchTermIterator = m_searchTerm.begin(); |
| 63 | } |
| 64 | if (m_giveUpTerm.empty()) { |
| 65 | continue; |
| 66 | } |
| 67 | if (currentChar == *m_giveUpTermIterator) { |
| 68 | ++m_giveUpTermIterator; |
| 69 | } else { |
| 70 | m_giveUpTermIterator = m_giveUpTerm.begin(); |
| 71 | } |
| 72 | } |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
| 76 | /*! |
| 77 | * \brief Processes the specified \a buffer. Invokes the callback according to the remarks mentioned in the class documentation. |
| 78 | * \todo Make inline in v6. |
| 79 | */ |
| 80 | void BufferSearch::operator()(const std::string_view::value_type *buffer, std::size_t bufferSize) |
| 81 | { |
| 82 | process(buffer, bufferSize); |
| 83 | } |
| 84 | |
| 85 | /*! |
| 86 | * \brief Resets the search to its initial state (assuming no characters of the search term or give-up term have been found yet). |
| 87 | */ |
| 88 | void BufferSearch::reset() |
| 89 | { |
| 90 | m_searchTermIterator = m_searchTerm.begin(); |
| 91 | m_giveUpTermIterator = m_giveUpTerm.begin(); |
| 92 | m_terminationTermIterator = m_terminationTerm.begin(); |
| 93 | m_hasResult = false; |
| 94 | m_result.clear(); |
| 95 | } |
| 96 | |
| 97 | } // namespace CppUtilities |