# HG changeset patch # User Teemu Piippo # Date 1611745731 -7200 # Node ID 2e7225dbd9b27d887cfe718ea6a37aa246afa7de # Parent 7fc34735178e73e96b256f1b516ed737d9725f25 continue cleanup diff -r 7fc34735178e -r 2e7225dbd9b2 sources/list.cpp --- a/sources/list.cpp Wed Jan 27 13:02:51 2021 +0200 +++ b/sources/list.cpp Wed Jan 27 13:08:51 2021 +0200 @@ -76,21 +76,11 @@ } } -/*! - * \brief Constructs a byte array from an initializer list containing bytes. - * \param initializerList List of bytes. - */ -ByteArray::ByteArray(std::initializer_list initializerList) : - Vector(initializerList) {} - -/*! - * \returns a quoted representation of the contents of the byte array. - */ -String ByteArray::quote() const +String quote(const ByteArray &bytes) { String result; - for (unsigned char byte : *this) + for (unsigned char byte : bytes) result += representByte(byte); return "\"" + result + "\""; diff -r 7fc34735178e -r 2e7225dbd9b2 sources/list.h --- a/sources/list.h Wed Jan 27 13:02:51 2021 +0200 +++ b/sources/list.h Wed Jan 27 13:08:51 2021 +0200 @@ -346,24 +346,6 @@ // template -class List : public Container > -{ -public: - typedef Container > Super; - - List(){} - - List (int numvalues) : - Super (numvalues) {} - - List (const Super& other) : - Super (other) {} -}; - -// ------------------------------------------------------------------------------------------------- -// - -template class Vector : public Container > { public: @@ -399,24 +381,8 @@ } }; -class ByteArray : public Vector -{ -public: - ByteArray(std::initializer_list initializerList); - - template - ByteArray(Args&& ...args); - - class String quote() const; -}; - -/*! - * \brief Constructs a byte array by passing all arguments to Vector's constructor. - * \param args Arguments to pass. - */ -template -ByteArray::ByteArray(Args&& ...args) : - Vector(args...) {} +using ByteArray = std::vector; +class String quote(const ByteArray& bytes); template T splice(const T& container, int start, int end, int step = 1) diff -r 7fc34735178e -r 2e7225dbd9b2 sources/network/bytestream.cpp --- a/sources/network/bytestream.cpp Wed Jan 27 13:02:51 2021 +0200 +++ b/sources/network/bytestream.cpp Wed Jan 27 13:08:51 2021 +0200 @@ -66,7 +66,7 @@ /*! * \returns an iterator to the current data position. */ -ByteArray::Iterator Bytestream::getCurrentIterator() +ByteArray::iterator Bytestream::getCurrentIterator() { return m_data.begin() + m_position; } @@ -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) { + m_data.reserve(m_data.size() + 2); for (int i : range(2)) - m_data.append((value >> (i * 8)) & 0xFF); + m_data.push_back((value >> (i * 8)) & 0xFF); } /*! @@ -179,8 +180,9 @@ */ void Bytestream::writeLong(int32_t value) { + m_data.reserve(m_data.size() + 4); for (int i : range(4)) - m_data.append((value >> (i * 8)) & 0xFF); + m_data.push_back((value >> (i * 8)) & 0xFF); } /*! @@ -201,8 +203,11 @@ */ void Bytestream::writeString(const 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); } /*! diff -r 7fc34735178e -r 2e7225dbd9b2 sources/network/bytestream.h --- a/sources/network/bytestream.h Wed Jan 27 13:02:51 2021 +0200 +++ b/sources/network/bytestream.h Wed Jan 27 13:08:51 2021 +0200 @@ -60,7 +60,7 @@ Bytestream(ByteArray& data); int bytesLeft() const; - ByteArray::Iterator getCurrentIterator(); + ByteArray::iterator getCurrentIterator(); int position() const; ByteArray readBuffer(int length); int8_t readByte(); diff -r 7fc34735178e -r 2e7225dbd9b2 sources/network/rconsession.cpp --- a/sources/network/rconsession.cpp Wed Jan 27 13:02:51 2021 +0200 +++ b/sources/network/rconsession.cpp Wed Jan 27 13:08:51 2021 +0200 @@ -227,7 +227,7 @@ catch (std::exception& e) { m_interface->printWarning("Couldn't process packet: %s\n", e.what()); - m_interface->printWarning("Packet contents was: %s\n", message.quote().chars()); + m_interface->printWarning("Packet contents was: %s\n", quote(message).chars()); m_interface->printWarning("Stream position in payload was: %d\n", stream.position()); } } diff -r 7fc34735178e -r 2e7225dbd9b2 sources/network/udpsocket.cpp --- a/sources/network/udpsocket.cpp Wed Jan 27 13:02:51 2021 +0200 +++ b/sources/network/udpsocket.cpp Wed Jan 27 13:08:51 2021 +0200 @@ -135,7 +135,7 @@ decodedPacket, length, &decodedLength); datagram.address.host = ntohl (claddr.sin_addr.s_addr); datagram.address.port = ntohs (claddr.sin_port); - datagram.message = ByteArray(decodedPacket, decodedLength); + datagram.message = ByteArray{&decodedPacket[0], &decodedPacket[decodedLength]}; return true; }