Skip to content

Commit

Permalink
used size() to avoid reallocations
Browse files Browse the repository at this point in the history
  • Loading branch information
neandreithal committed Jun 4, 2024
1 parent 84abb20 commit f2e403b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions include/jsoncons/json_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ namespace jsoncons {
members_.reserve(count);
for (auto s = first; s != last; ++s)
{
members_.emplace_back(key_type(s->first.c_str(),get_allocator()), s->second);
members_.emplace_back(key_type(s->first.c_str(),s->first.size(),get_allocator()), s->second);
}
std::stable_sort(members_.begin(), members_.end(),
[](const key_value_type& a, const key_value_type& b) -> bool {return a.key().compare(b.key()) < 0;});
Expand Down Expand Up @@ -534,13 +534,13 @@ namespace jsoncons {
auto last = first + count;

std::sort(first, last, compare);
members_.emplace_back(key_type(first->name.c_str(),get_allocator()), std::move(first->value));
members_.emplace_back(key_type(first->name.c_str(),first->name.size(),get_allocator()), std::move(first->value));
auto prev_it = first;
for (auto it = first+1; it != last; ++it)
{
if (it->name != prev_it->name)
{
members_.emplace_back(key_type(it->name.c_str(),get_allocator()), std::move(it->value));
members_.emplace_back(key_type(it->name.c_str(),it->name.size(),get_allocator()), std::move(it->value));
}
++prev_it;
}
Expand All @@ -552,7 +552,7 @@ namespace jsoncons {
{
for (auto s = first; s != last; ++s)
{
members_.emplace_back(key_type(s->first.c_str(),get_allocator()), s->second);
members_.emplace_back(key_type(s->first.c_str(),s->first.size(),get_allocator()), s->second);
}
std::stable_sort(members_.begin(),members_.end(),
[](const key_value_type& a, const key_value_type& b) -> bool {return a.key().compare(b.key()) < 0;});
Expand Down Expand Up @@ -1340,10 +1340,10 @@ namespace jsoncons {
std::unordered_set<key_type,MyHash> keys;
for (auto it = first; it != last; ++it)
{
key_type key{it->first.c_str(), get_allocator()};
key_type key{it->first.c_str(), it->first.size(), get_allocator()};
if (keys.find(key) == keys.end())
{
keys.emplace(key.c_str(), get_allocator());
keys.emplace(key.c_str(), key.size(), get_allocator());
members_.emplace_back(std::move(key), it->second);
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/jsoncons_ext/cbor/cbor_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class basic_cbor_parser : public ser_context
byte_string_type bytes;

mapped_string(const string_type& str, const allocator_type& alloc = allocator_type())
: type(jsoncons::cbor::detail::cbor_major_type::text_string), str(str.c_str(),alloc), bytes(alloc)
: type(jsoncons::cbor::detail::cbor_major_type::text_string), str(str.c_str(),str.size(),alloc), bytes(alloc)
{
}

Expand Down

0 comments on commit f2e403b

Please sign in to comment.