| 1 | #cxx . . $ `<file>/io/buffersearch.cpp`/include "./buffersearch.h"cxx . . $ `<file>/io/buffersearch.h`/ |
| 2 | |
| 3 | using namespace std; |
| 4 | |
| 5 | namespace CppUtilitiescxx . . $ 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 stdcxx . . $ std/::string_viewcxx . . $ std/string_view#::value_typecxx . . $ std/basic_string_view#value_type# *BufferSearchcxx . . $ CppUtilities/BufferSearch#::processcxx . . $ CppUtilities/BufferSearch#process(e3d2f176fff90d6e).(const stdcxx . . $ std/::string_viewcxx . . $ std/string_view#::value_typecxx . . $ std/basic_string_view#value_type# *buffer, stdcxx . . $ std/::size_tcxx . . $ std/size_t# bufferSize) |
| 33 | { |
| 34 | if (m_hasResultcxx . . $ CppUtilities/BufferSearch#m_hasResult. || (!m_giveUpTermcxx . . $ CppUtilities/BufferSearch#m_giveUpTerm..emptycxx . . $ std/basic_string_view#empty(3482b152b9333168).() && m_giveUpTermIteratorcxx . . $ CppUtilities/BufferSearch#m_giveUpTermIterator. == m_giveUpTermcxx . . $ CppUtilities/BufferSearch#m_giveUpTerm..endcxx . . $ std/basic_string_view#end(8e614c7de0b1573e).())) { |
| 35 | return nullptr; |
| 36 | } |
| 37 | for (auto i = buffer, end = buffer + bufferSize; i != end; ++i) { |
| 38 | const auto currentChar = *i; |
| 39 | if (m_searchTermIteratorcxx . . $ CppUtilities/BufferSearch#m_searchTermIterator. == m_searchTermcxx . . $ CppUtilities/BufferSearch#m_searchTerm..endcxx . . $ std/basic_string_view#end(8e614c7de0b1573e).()) { |
| 40 | if (m_terminationCharscxx . . $ CppUtilities/BufferSearch#m_terminationChars..emptycxx . . $ std/basic_string_view#empty(3482b152b9333168).()) { |
| 41 | m_hasResultcxx . . $ CppUtilities/BufferSearch#m_hasResult. = true; |
| 42 | } else { |
| 43 | for (const auto &terminationChar : m_terminationCharscxx . . $ CppUtilities/BufferSearch#m_terminationChars.) { |
| 44 | if (currentChar == terminationChar) { |
| 45 | m_hasResultcxx . . $ CppUtilities/BufferSearch#m_hasResult. = true; |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | if (m_hasResultcxx . . $ CppUtilities/BufferSearch#m_hasResult.) { |
| 51 | if (m_callbackcxx . . $ CppUtilities/BufferSearch#m_callback.) { |
| 52 | m_callbackcxx . . $ CppUtilities/BufferSearch#m_callback.(cxx . . $ std/function#`operator()`(39e0826ffb540481).*this, stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(m_resultcxx . . $ CppUtilities/BufferSearch#m_result.)); |
| 53 | } |
| 54 | return i; |
| 55 | } |
| 56 | m_resultcxx . . $ CppUtilities/BufferSearch#m_result. +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). currentChar; |
| 57 | continue; |
| 58 | } |
| 59 | if (currentChar == *m_searchTermIteratorcxx . . $ CppUtilities/BufferSearch#m_searchTermIterator.) { |
| 60 | ++m_searchTermIteratorcxx . . $ CppUtilities/BufferSearch#m_searchTermIterator.; |
| 61 | } else { |
| 62 | m_searchTermIteratorcxx . . $ CppUtilities/BufferSearch#m_searchTermIterator. = m_searchTermcxx . . $ CppUtilities/BufferSearch#m_searchTerm..begincxx . . $ std/basic_string_view#begin(8e614c7de0b1573e).(); |
| 63 | } |
| 64 | if (m_giveUpTermcxx . . $ CppUtilities/BufferSearch#m_giveUpTerm..emptycxx . . $ std/basic_string_view#empty(3482b152b9333168).()) { |
| 65 | continue; |
| 66 | } |
| 67 | if (currentChar == *m_giveUpTermIteratorcxx . . $ CppUtilities/BufferSearch#m_giveUpTermIterator.) { |
| 68 | ++m_giveUpTermIteratorcxx . . $ CppUtilities/BufferSearch#m_giveUpTermIterator.; |
| 69 | } else { |
| 70 | m_giveUpTermIteratorcxx . . $ CppUtilities/BufferSearch#m_giveUpTermIterator. = m_giveUpTermcxx . . $ CppUtilities/BufferSearch#m_giveUpTerm..begincxx . . $ std/basic_string_view#begin(8e614c7de0b1573e).(); |
| 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 BufferSearchcxx . . $ CppUtilities/BufferSearch#::operatorcxx . . $ CppUtilities/BufferSearch#`operator()`(7cafb6e0818eff3b).()(const stdcxx . . $ std/::string_viewcxx . . $ std/string_view#::value_typecxx . . $ std/basic_string_view#value_type# *buffer, stdcxx . . $ std/::size_tcxx . . $ std/size_t# bufferSize) |
| 81 | { |
| 82 | processcxx . . $ CppUtilities/BufferSearch#process(e3d2f176fff90d6e).(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 BufferSearchcxx . . $ CppUtilities/BufferSearch#::resetcxx . . $ CppUtilities/BufferSearch#reset(49f6e7a06ebc5aa8).() |
| 89 | { |
| 90 | m_searchTermIteratorcxx . . $ CppUtilities/BufferSearch#m_searchTermIterator. = m_searchTermcxx . . $ CppUtilities/BufferSearch#m_searchTerm..begincxx . . $ std/basic_string_view#begin(8e614c7de0b1573e).(); |
| 91 | m_giveUpTermIteratorcxx . . $ CppUtilities/BufferSearch#m_giveUpTermIterator. = m_giveUpTermcxx . . $ CppUtilities/BufferSearch#m_giveUpTerm..begincxx . . $ std/basic_string_view#begin(8e614c7de0b1573e).(); |
| 92 | m_terminationTermIteratorcxx . . $ CppUtilities/BufferSearch#m_terminationTermIterator. = m_terminationTermcxx . . $ CppUtilities/BufferSearch#m_terminationTerm..begincxx . . $ std/basic_string_view#begin(8e614c7de0b1573e).(); |
| 93 | m_hasResultcxx . . $ CppUtilities/BufferSearch#m_hasResult. = false; |
| 94 | m_resultcxx . . $ CppUtilities/BufferSearch#m_result..clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).(); |
| 95 | } |
| 96 | |
| 97 | } // namespace CppUtilities |