From 928a7ffce8fba6d1e368efedba2d52cf0a0757f4 Mon Sep 17 00:00:00 2001 From: aaron0x Date: Sun, 20 Sep 2015 19:52:30 +0800 Subject: [PATCH] avoid redundant strlen. --- Foundation/include/Poco/BinaryWriter.h | 2 ++ Foundation/src/BinaryWriter.cpp | 42 +++++++++++++++----------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Foundation/include/Poco/BinaryWriter.h b/Foundation/include/Poco/BinaryWriter.h index 24163cf5d..0ea99839c 100644 --- a/Foundation/include/Poco/BinaryWriter.h +++ b/Foundation/include/Poco/BinaryWriter.h @@ -189,6 +189,8 @@ private: while (value); } + BinaryWriter& write(const char* value, std::size_t length); + std::ostream& _ostr; bool _flipBytes; TextConverter* _pTextConverter; diff --git a/Foundation/src/BinaryWriter.cpp b/Foundation/src/BinaryWriter.cpp index 65d18ed55..7c7d59829 100644 --- a/Foundation/src/BinaryWriter.cpp +++ b/Foundation/src/BinaryWriter.cpp @@ -153,29 +153,13 @@ BinaryWriter& BinaryWriter::operator << (UInt64 value) BinaryWriter& BinaryWriter::operator << (const std::string& value) { - return *this << value.c_str(); + return write(value.c_str(), value.length()); } BinaryWriter& BinaryWriter::operator << (const char* value) { - poco_check_ptr (value); - - if (_pTextConverter) - { - std::string converted; - _pTextConverter->convert(value, static_cast(std::strlen(value)), converted); - UInt32 length = (UInt32) converted.size(); - write7BitEncoded(length); - _ostr.write(converted.data(), length); - } - else - { - UInt32 length = static_cast(std::strlen(value)); - write7BitEncoded(length); - _ostr.write(value, length); - } - return *this; + return write(value, std::strlen(value)); } @@ -223,4 +207,26 @@ void BinaryWriter::flush() } +BinaryWriter& BinaryWriter::write(const char* value, std::size_t length) +{ + poco_check_ptr (value); + + if (_pTextConverter) + { + std::string converted; + _pTextConverter->convert(value, static_cast(length), converted); + UInt32 convertedLength = (UInt32) converted.length(); + write7BitEncoded(convertedLength); + _ostr.write(converted.data(), convertedLength); + } + else + { + UInt32 lengthUInt32 = static_cast(length); + write7BitEncoded(lengthUInt32); + _ostr.write(value, lengthUInt32); + } + return *this; +} + + } // namespace Poco