From f8270369b688cb8bb69f10d4c772eeebaf23bc80 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 8 Jun 2021 17:30:29 +0200 Subject: [PATCH] Make whitespace-reduction a bit more efficient. We now use 2 passes instead of 3. --- src/search.cpp | 41 +++++++++++++++++++---------------- tests/test_search_helpers.cpp | 2 +- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index 2605e56..173d81f 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -133,26 +133,29 @@ file_in_epub cleanup_text(const std::string_view text) "(title|aria-label)" "=\"([[:alnum:]]+)\""}; - // TODO: Make this more efficient, 3 → 1; + { + size_t pos{0}; + while ((pos = output.find_first_of("\n\t\r", pos)) != string::npos) + { + if (output[pos] == '\r') + { + output.erase(pos, 1); + } + else + { + output.replace(pos, 1, " "); + } + } + } + { + size_t pos{0}; + while ((pos = output.find(" ", pos)) != string::npos) + { + output.replace(pos, 2, " "); + } + } + size_t pos{0}; - while ((pos = output.find('\r', pos)) != string::npos) - { - output.erase(pos, 1); - } - - pos = 0; - while ((pos = output.find_first_of("\n\t", pos)) != string::npos) - { - output.replace(pos, 1, " "); - } - - pos = 0; - while ((pos = output.find(" ", pos)) != string::npos) - { - output.replace(pos, 2, " "); - } - - pos = 0; file_in_epub file; size_t headline_start{string::npos}; while ((pos = output.find('<', pos)) != string::npos) diff --git a/tests/test_search_helpers.cpp b/tests/test_search_helpers.cpp index d9026a6..cdb23bf 100644 --- a/tests/test_search_helpers.cpp +++ b/tests/test_search_helpers.cpp @@ -54,7 +54,7 @@ SCENARIO("Searching helpers work as intended") } THEN("No exception is thrown") - AND_THEN("The \\r are removed unchanged") + AND_THEN("The \\r are removed") { REQUIRE_FALSE(exception); REQUIRE(text == "💖🦝");