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 == "💖🦝");