--- a/sources/network/bytestream.cpp Wed Jan 27 12:41:50 2021 +0200 +++ b/sources/network/bytestream.cpp Wed Jan 27 19:48:41 2021 +0200 @@ -1,5 +1,5 @@ /* - Copyright 2014 - 2016 Teemu Piippo + Copyright 2014 - 2021 Teemu Piippo All rights reserved. Redistribution and use in source and binary forms, with or without @@ -36,7 +36,7 @@ * \brief Constructs a byte cursor. The cursor is placed to the beginning of the stream. * \param data */ -Bytestream::Bytestream(ByteArray& data) : +Bytestream::Bytestream(std::vector<unsigned char>& data) : m_data(data), m_position(0) {} @@ -49,8 +49,8 @@ if (bytesLeft() < bytes) { int bytesPast = bytes - bytesLeft(); - String message; - message.sprintf("attempted to read %d byte%s past the end of bytestream", bytesPast, plural(bytesPast)); + std::string message; + message = sprintf("attempted to read %d byte%s past the end of bytestream", bytesPast, plural(bytesPast)); throw IOError (message); } } @@ -66,7 +66,7 @@ /*! * \returns an iterator to the current data position. */ -ByteArray::Iterator Bytestream::getCurrentIterator() +std::vector<unsigned char>::iterator Bytestream::getCurrentIterator() { return m_data.begin() + m_position; } @@ -90,7 +90,7 @@ ensureReadSpace (2); int16_t result = 0; - for (int i : range(2)) + for (int i : {0, 1}) result |= read() << (i * 8); return result; @@ -104,8 +104,8 @@ { ensureReadSpace (4); int32_t result = 0; - - for (int i : range(4)) + + for (int i = 0; i < 4; i += 1) result |= read() << (i * 8); return result; @@ -127,9 +127,9 @@ * \brief Reads in characters until a null terminator is encountered. * \returns the read string. */ -String Bytestream::readString() +std::string Bytestream::readString() { - String result; + std::string result; for (char byte; (byte = readByte()) != '\0';) { @@ -145,10 +145,10 @@ * \param length Amount of bytes to read. * \returns the read buffer. */ -ByteArray Bytestream::readBuffer(int length) +std::vector<unsigned char> Bytestream::readBuffer(int length) { ensureReadSpace(length); - ByteArray result(length); + std::vector<unsigned char> result(length); memcpy(result.data(), m_data.data() + m_position, length); m_position += length; return result; @@ -160,7 +160,7 @@ */ void Bytestream::writeByte(int8_t value) { - m_data.append(value); + m_data.push_back(value); } /*! @@ -169,8 +169,9 @@ */ void Bytestream::writeShort(int16_t value) { - for (int i : range(2)) - m_data.append((value >> (i * 8)) & 0xFF); + m_data.reserve(m_data.size() + 2); + for (int i : {0, 1}) + m_data.push_back((value >> (i * 8)) & 0xFF); } /*! @@ -179,8 +180,9 @@ */ void Bytestream::writeLong(int32_t value) { - for (int i : range(4)) - m_data.append((value >> (i * 8)) & 0xFF); + m_data.reserve(m_data.size() + 4); + for (int i = 0; i < 4; i += 1) + m_data.push_back((value >> (i * 8)) & 0xFF); } /*! @@ -199,10 +201,13 @@ * \brief Writes the given string to the end of the data. * \param text String to write. */ -void Bytestream::writeString(const String& string) +void Bytestream::writeString(const std::string& string) { - m_data.append(string.toBytes(), string.length()); - m_data.append(0); + const int oldSize = m_data.size(); + m_data.reserve(m_data.size() + string.length() + 1); + m_data.resize(m_data.size() + string.length()); + std::copy(string.begin(), string.end(), m_data.begin() + oldSize); + m_data.push_back(0); } /*!