diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2015-02-24 06:27:57 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2015-02-24 06:27:57 +0200 |
commit | 52925684387496e83425c79f74b29a91943466a7 (patch) | |
tree | 02ba612b339e73625be28ab141fe11e358866404 | |
parent | 5fc53bd74c7635a292cef041180da4cd32b7fb12 (diff) |
Make empty key to always be prefix in prefix_map
-rw-r--r-- | build/algorithm.cxx | 7 | ||||
-rw-r--r-- | build/prefix-map | 10 | ||||
-rw-r--r-- | tests/build/prefix-map/driver.cxx | 12 |
3 files changed, 15 insertions, 14 deletions
diff --git a/build/algorithm.cxx b/build/algorithm.cxx index ddf0071..d418cda 100644 --- a/build/algorithm.cxx +++ b/build/algorithm.cxx @@ -44,13 +44,10 @@ namespace build if (i == rules.end ()) // No rules registered for this target type. continue; - const auto& rules (i->second); // Name map. + const auto& rules (i->second); // Hint map. string hint; // @@ TODO - - auto rs (hint.empty () - ? make_pair (rules.begin (), rules.end ()) - : rules.find (hint)); + auto rs (rules.find (hint)); for (auto i (rs.first); i != rs.second; ++i) { diff --git a/build/prefix-map b/build/prefix-map index d2ca9be..a385ca8 100644 --- a/build/prefix-map +++ b/build/prefix-map @@ -39,8 +39,9 @@ namespace build prefix (const K& p, const K& k) const { size_type pn (p.size ()); - return compare ( - p.c_str (), pn, k.c_str (), pn == k.size () ? pn : pn + 1) == 0; + return pn == 0 || // Empty key is always a prefix. + compare ( + p.c_str (), pn, k.c_str (), pn == k.size () ? pn : pn + 1) == 0; } int @@ -76,6 +77,11 @@ namespace build // the ability to retrieve a range of entries that have a specific // prefix. The '.' and '/' above are the delimiter characters. // + // Note that as a special rule, the default implementation of + // compare_prefix treats empty key as everyone's prefix even if + // the paths don't start with the delimiter (useful to represent + // a "root path"). + // // Implementation-wise, the idea is to pretend that each key ends // with the delimiter. This way we automatically avoid matching // 'foobar' as having a prefix 'foo'. diff --git a/tests/build/prefix-map/driver.cxx b/tests/build/prefix-map/driver.cxx index fb5269c..77edb6c 100644 --- a/tests/build/prefix-map/driver.cxx +++ b/tests/build/prefix-map/driver.cxx @@ -35,7 +35,8 @@ main () { auto r (m.find ("")); - assert (r.first == r.second); + assert (r.first != r.second && r.first->second == 1 && + ++r.first == r.second); } { @@ -70,7 +71,9 @@ main () { auto r (m.find ("")); - assert (r.first == r.second); + assert (r.first != r.second && r.first->second == 2 && + ++r.first != r.second && r.first->second == 1 && + ++r.first == r.second); } { @@ -114,11 +117,6 @@ main () '.'); { - auto r (m.find ("")); - assert (r.first == r.second); - } - - { auto r (m.find ("fo")); assert (r.first == r.second); } |