From d59b7c234c4db101beb37a558ae852614204b54d Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 2 Jun 2018 20:21:52 +0200 Subject: [PATCH] Optimized string() functions --- client_http.hpp | 8 +++++--- server_http.hpp | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/client_http.hpp b/client_http.hpp index fcc47dd..be8d023 100644 --- a/client_http.hpp +++ b/client_http.hpp @@ -62,9 +62,11 @@ namespace SimpleWeb { /// Convenience function to return std::string. The stream buffer is consumed. std::string string() noexcept { try { - std::stringstream ss; - ss << rdbuf(); - return ss.str(); + std::string str; + auto size = streambuf.size(); + str.resize(size); + read(&str[0], static_cast(size)); + return str; } catch(...) { return std::string(); diff --git a/server_http.hpp b/server_http.hpp index e282bcb..235dcb8 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -163,9 +163,11 @@ namespace SimpleWeb { /// Convenience function to return std::string. The stream buffer is consumed. std::string string() noexcept { try { - std::stringstream ss; - ss << rdbuf(); - return ss.str(); + std::string str; + auto size = streambuf.size(); + str.resize(size); + read(&str[0], static_cast(size)); + return str; } catch(...) { return std::string();