Fix basic_fields insert ordering

This commit is contained in:
Vinnie Falco
2017-06-15 08:31:47 -07:00
parent 06290dc39f
commit f64109d79a
3 changed files with 33 additions and 16 deletions

View File

@@ -5,6 +5,7 @@ Version 58:
* Reorganize examples * Reorganize examples
* Specification for http read * Specification for http read
* Avoid `std::string` in websocket * Avoid `std::string` in websocket
* Fix basic_fields insert ordering
API Changes: API Changes:

View File

@@ -480,29 +480,25 @@ insert(field name,
name, sname, value.str()); name, sname, value.str());
auto const before = auto const before =
set_.upper_bound(sname, key_compare{}); set_.upper_bound(sname, key_compare{});
if(before == set_.end())
{
set_.push_back(e);
list_.push_back(e);
return;
}
if(before == set_.begin()) if(before == set_.begin())
{ {
set_.push_front(e); BOOST_ASSERT(count(sname) == 0);
list_.push_back(e);
return;
}
auto const last = std::prev(before);
if(! beast::detail::ci_equal(
sname, last->name_string()))
{
set_.insert_before(before, e); set_.insert_before(before, e);
list_.push_back(e); list_.push_back(e);
return; return;
} }
// count(name_string) > 0 auto const last = std::prev(before);
// VFALCO is it worth comparing `field name` first?
if(! beast::detail::ci_equal(sname, last->name_string()))
{
BOOST_ASSERT(count(sname) == 0);
set_.insert_before(before, e);
list_.push_back(e);
return;
}
// keep duplicate fields together in the list
set_.insert_before(before, e); set_.insert_before(before, e);
list_.insert(list_.iterator_to(*before), e); list_.insert(++list_.iterator_to(*last), e);
} }
template<class Allocator> template<class Allocator>

View File

@@ -375,6 +375,26 @@ public:
BEAST_EXPECT(std::next(f.begin(), 0)->name_string() == "a"); BEAST_EXPECT(std::next(f.begin(), 0)->name_string() == "a");
BEAST_EXPECT(std::next(f.begin(), 1)->name_string() == "b"); BEAST_EXPECT(std::next(f.begin(), 1)->name_string() == "b");
} }
{
// verify insertion orde
fields f;
f.insert( "a", 1);
f.insert("dd", 2);
f.insert("b", 3);
f.insert("dD", 4);
f.insert("c", 5);
f.insert("Dd", 6);
f.insert("DD", 7);
f.insert( "e", 8);
BEAST_EXPECT(f.count("dd") == 4);
BEAST_EXPECT(std::next(f.begin(), 1)->name_string() == "dd");
BEAST_EXPECT(std::next(f.begin(), 2)->name_string() == "dD");
BEAST_EXPECT(std::next(f.begin(), 3)->name_string() == "Dd");
BEAST_EXPECT(std::next(f.begin(), 4)->name_string() == "DD");
f.set("dd", "-");
BEAST_EXPECT(f.count("dd") == 1);
BEAST_EXPECT(f["dd"] == "-");
}
} }
void run() override void run() override