Prevent use of extra buffer in BinaryReader::readTerminatedString()

This commit is contained in:
Martchus 2019-06-04 19:40:12 +02:00
parent 0f2f98393f
commit 96dda1b862
1 changed files with 9 additions and 13 deletions

View File

@ -88,7 +88,7 @@ string BinaryReader::readString(size_t length)
{ {
string res; string res;
res.resize(length); res.resize(length);
m_stream->read(&res[0], length); m_stream->read(&res[0], static_cast<streamsize>(length));
return res; return res;
} }
@ -98,9 +98,6 @@ string BinaryReader::readString(size_t length)
* Advances the current position of the stream by the string length plus one byte. * Advances the current position of the stream by the string length plus one byte.
* *
* \param termination The byte to be recognized as termination value. * \param termination The byte to be recognized as termination value.
*
* \deprecated This method is likely refactored/removed in v5.
* \todo Refactor/remove in v5.
*/ */
std::string BinaryReader::readTerminatedString(std::uint8_t termination) std::string BinaryReader::readTerminatedString(std::uint8_t termination)
{ {
@ -119,20 +116,19 @@ std::string BinaryReader::readTerminatedString(std::uint8_t termination)
* *
* \param maxBytesToRead The maximal number of bytes to read. * \param maxBytesToRead The maximal number of bytes to read.
* \param termination The value to be recognized as termination. * \param termination The value to be recognized as termination.
*
* \deprecated This method is likely refactored/removed in v5.
* \todo Refactor/remove in v5.
*/ */
string BinaryReader::readTerminatedString(std::size_t maxBytesToRead, std::uint8_t termination) string BinaryReader::readTerminatedString(std::size_t maxBytesToRead, std::uint8_t termination)
{ {
unique_ptr<char[]> buff = make_unique<char[]>(maxBytesToRead); string res;
for (char *i = buff.get(), *end = i + maxBytesToRead; i < end; ++i) { res.reserve(maxBytesToRead);
m_stream->get(*i); while (res.size() < maxBytesToRead) {
if (*(reinterpret_cast<std::uint8_t *>(i)) == termination) { const auto c = static_cast<char>(m_stream->get());
return string(buff.get(), static_cast<size_t>(i - buff.get())); if (static_cast<std::uint8_t>(c) == termination) {
return res;
} }
res += c;
} }
return string(buff.get(), maxBytesToRead); return res;
} }
/*! /*!