From feae2f1830befc7a672da601037d2fc445e0093b Mon Sep 17 00:00:00 2001 From: kampersanda Date: Sun, 10 Jul 2022 14:37:25 +0900 Subject: [PATCH] fix --- include/xcdat/tail_vector.hpp | 16 ++++++++++++---- tests/test_trie.cpp | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/xcdat/tail_vector.hpp b/include/xcdat/tail_vector.hpp index 7212533..90c4f7c 100644 --- a/include/xcdat/tail_vector.hpp +++ b/include/xcdat/tail_vector.hpp @@ -168,17 +168,25 @@ class tail_vector { // Returns true if TAIL[tpos..epos] is a prefix of key, where epos is the end position of the tail. inline bool prefix_match(std::string_view key, std::uint64_t tpos) const { - assert(key.size() != 0); + if (tpos == 0) { + // suffix is empty, returns true always. + return true; + } + if (key.size() == 0) { + // When key is empty, returns true iff suffix is empty. + return false; + } + std::uint64_t kpos = 0; if (bin_mode()) { do { - if (m_terms[tpos]) { - return true; - } if (key[kpos] != m_chars[tpos]) { return false; } kpos += 1; + if (m_terms[tpos]) { + return true; + } tpos += 1; } while (kpos < key.size()); return true; diff --git a/tests/test_trie.cpp b/tests/test_trie.cpp index f1f48ee..ac5a4a5 100644 --- a/tests/test_trie.cpp +++ b/tests/test_trie.cpp @@ -167,7 +167,7 @@ TEST_CASE("Test " TRIE_NAME " (tiny)") { test_basic_operations(trie, keys, others); { - auto itr = trie.make_prefix_iterator("MacBook_Pro"); + auto itr = trie.make_prefix_iterator("MacBook_Pro_13inch"); std::vector expected = {"Mac", "MacBook", "MacBook_Pro"}; for (const auto& exp : expected) { REQUIRE(itr.next());