BSON: Reworked binary_reader::get_bson_cstr()

This commit is contained in:
Julian Becker 2018-09-29 11:50:01 +02:00
parent 0a09db9cc2
commit e8730e5e82
2 changed files with 22 additions and 40 deletions

View file

@ -125,36 +125,27 @@ class binary_reader
private: private:
template<class OutputIt, class UnaryPredicate, class Gen>
OutputIt generate_until(OutputIt&& d_first, UnaryPredicate&& pred, Gen&& gen)
{
for (auto x = gen(); !pred(x); x = gen())
{
*d_first++ = x;
}
return d_first;
}
/*! /*!
@return whether array creation completed @return whether array creation completed
*/ */
bool get_bson_cstr(string_t& result) bool get_bson_cstr(string_t& result)
{ {
bool success = true; auto out = std::back_inserter(result);
generate_until(std::back_inserter(result), [&success](char c) while (true)
{
return c == 0x00 || !success;
}, [this, &success]
{ {
get(); get();
if (JSON_UNLIKELY(not unexpect_eof())) if (JSON_UNLIKELY(not unexpect_eof()))
{ {
success = false; return false;
} }
return static_cast<char>(current); if (current == 0x00)
}); {
return success; return true;
}
*out++ = static_cast<char>(current);
}
return true;
} }
bool parse_bson_entries(bool is_array) bool parse_bson_entries(bool is_array)

View file

@ -6109,36 +6109,27 @@ class binary_reader
private: private:
template<class OutputIt, class UnaryPredicate, class Gen>
OutputIt generate_until(OutputIt&& d_first, UnaryPredicate&& pred, Gen&& gen)
{
for (auto x = gen(); !pred(x); x = gen())
{
*d_first++ = x;
}
return d_first;
}
/*! /*!
@return whether array creation completed @return whether array creation completed
*/ */
bool get_bson_cstr(string_t& result) bool get_bson_cstr(string_t& result)
{ {
bool success = true; auto out = std::back_inserter(result);
generate_until(std::back_inserter(result), [&success](char c) while (true)
{
return c == 0x00 || !success;
}, [this, &success]
{ {
get(); get();
if (JSON_UNLIKELY(not unexpect_eof())) if (JSON_UNLIKELY(not unexpect_eof()))
{ {
success = false; return false;
} }
return static_cast<char>(current); if (current == 0x00)
}); {
return success; return true;
}
*out++ = static_cast<char>(current);
}
return true;
} }
bool parse_bson_entries(bool is_array) bool parse_bson_entries(bool is_array)