Minor improvements to sf::Packet operators (now using strlen and wcslen instead of explicit loops) (#118)

This commit is contained in:
Laurent Gomila 2013-06-17 21:04:41 +02:00
parent 5d377fdb38
commit 0db73b6a4c

View File

@ -29,6 +29,7 @@
#include <SFML/Network/SocketImpl.hpp> #include <SFML/Network/SocketImpl.hpp>
#include <SFML/System/String.hpp> #include <SFML/System/String.hpp>
#include <cstring> #include <cstring>
#include <cwchar>
namespace sf namespace sf
@ -404,9 +405,7 @@ Packet& Packet::operator <<(double data)
Packet& Packet::operator <<(const char* data) Packet& Packet::operator <<(const char* data)
{ {
// First insert string length // First insert string length
Uint32 length = 0; Uint32 length = std::strlen(data);
for (const char* c = data; *c != '\0'; ++c)
++length;
*this << length; *this << length;
// Then insert characters // Then insert characters
@ -425,9 +424,7 @@ Packet& Packet::operator <<(const std::string& data)
// Then insert characters // Then insert characters
if (length > 0) if (length > 0)
{
append(data.c_str(), length * sizeof(std::string::value_type)); append(data.c_str(), length * sizeof(std::string::value_type));
}
return *this; return *this;
} }
@ -437,9 +434,7 @@ Packet& Packet::operator <<(const std::string& data)
Packet& Packet::operator <<(const wchar_t* data) Packet& Packet::operator <<(const wchar_t* data)
{ {
// First insert string length // First insert string length
Uint32 length = 0; Uint32 length = std::wcslen(data);
for (const wchar_t* c = data; *c != L'\0'; ++c)
++length;
*this << length; *this << length;
// Then insert characters // Then insert characters