sources/network/bytestream.cpp

branch
protocol5
changeset 174
78ee24fd3321
parent 167
0150f86e68f0
child 175
18f2d2de1929
--- a/sources/network/bytestream.cpp	Wed Jan 27 12:34:26 2021 +0200
+++ b/sources/network/bytestream.cpp	Wed Jan 27 12:39:00 2021 +0200
@@ -129,15 +129,28 @@
  */
 String Bytestream::readString()
 {
-	String result;
+	ByteArray::Iterator stringEndIterator;
 
-	for (char byte; (byte = readByte()) != '\0';)
+	// Where's the end of the string?
+	for (stringEndIterator = getCurrentIterator(); *stringEndIterator != '\0'; ++stringEndIterator)
 	{
-		if (result.length() < MAX_NETWORK_STRING)
-			result += byte;
+		if (stringEndIterator == m_data.end())
+		{
+			// Past the end of the buffer
+			throw IOError("unterminated or too long string in packet");
+		}
 	}
 
-	return result;
+	// Skip past the null terminator.
+	if (*stringEndIterator == '\0')
+		stringEndIterator += 1;
+
+	// Build and return the string, and advance the position.
+	int stringStart = m_position;
+	unsigned int length = stringEndIterator - getCurrentIterator();
+	length = min<int>(length, MAX_NETWORK_STRING);
+	m_position += length;
+	return String::fromBytes(m_data.splice(stringStart, stringStart + length));
 }
 
 /*!

mercurial