Optimize reading string from packet.
Avoids possibility or re-allocation of the internal buffer of the string when its long.
This commit is contained in:
+14
-6
@@ -259,14 +259,22 @@ class ByteBuffer
|
||||
|
||||
ByteBuffer& operator>>(std::string& value)
|
||||
{
|
||||
value.clear();
|
||||
while (rpos() < size()) // prevent crash at wrong string format in packet
|
||||
// prevent crash at wrong string format in packet
|
||||
if (_rpos < size())
|
||||
{
|
||||
char c = read<char>();
|
||||
if (c == 0)
|
||||
break;
|
||||
value += c;
|
||||
size_t startPos = _rpos;
|
||||
|
||||
while (_storage[_rpos] != '\0')
|
||||
{
|
||||
_rpos++;
|
||||
|
||||
if (_rpos + sizeof(char) > size())
|
||||
throw ByteBufferException(false, _rpos, sizeof(char), size());
|
||||
}
|
||||
value.assign((char*)(&_storage[startPos]), _rpos - startPos);
|
||||
_rpos++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user