From 5df687f16908a76d211d6a04bc8051aefc8dfc74 Mon Sep 17 00:00:00 2001 From: Han Zhu Date: Wed, 4 May 2016 08:48:22 +0000 Subject: [PATCH] some perf improvements for uri related code --- Release/include/cpprest/asyncrt_utils.h | 10 ++++++++++ Release/include/cpprest/base_uri.h | 6 ++++++ Release/include/cpprest/http_headers.h | 2 +- Release/include/cpprest/uri_builder.h | 2 +- Release/src/uri/uri.cpp | 24 ++++++++++++------------ Release/src/uri/uri_builder.cpp | 2 +- Release/src/uri/uri_parser.cpp | 4 ++-- 7 files changed, 33 insertions(+), 17 deletions(-) diff --git a/Release/include/cpprest/asyncrt_utils.h b/Release/include/cpprest/asyncrt_utils.h index 3d7f3ee5e0..73b6f5b97e 100644 --- a/Release/include/cpprest/asyncrt_utils.h +++ b/Release/include/cpprest/asyncrt_utils.h @@ -197,6 +197,11 @@ namespace conversions return print_string(val, std::locale()); } + inline utility::string_t print_string(const utility::string_t &val) + { + return val; + } + template Target scan_string(const utility::string_t &str, const std::locale &loc) { @@ -216,6 +221,11 @@ namespace conversions { return scan_string(str, std::locale()); } + + inline utility::string_t scan_string(const utility::string_t &str) + { + return str; + } } namespace details diff --git a/Release/include/cpprest/base_uri.h b/Release/include/cpprest/base_uri.h index ee24fb453e..5f354fa5ad 100644 --- a/Release/include/cpprest/base_uri.h +++ b/Release/include/cpprest/base_uri.h @@ -223,6 +223,12 @@ namespace web { /// uri() { m_uri = _XPLATSTR("/");}; + /// + /// Creates a URI from the given URI components. + /// + /// A URI components object to create the URI instance. + _ASYNCRTIMP uri(const details::uri_components &components) : m_components(components) { m_uri = m_components.join(); } + /// /// Creates a URI from the given encoded string. This will throw an exception if the string /// does not contain a valid URI. Use uri::validate if processing user-input. diff --git a/Release/include/cpprest/http_headers.h b/Release/include/cpprest/http_headers.h index 8228d7cccb..e0c4526def 100644 --- a/Release/include/cpprest/http_headers.h +++ b/Release/include/cpprest/http_headers.h @@ -159,7 +159,7 @@ class http_headers { if (has(name)) { - m_headers[name] = m_headers[name].append(_XPLATSTR(", ") + utility::conversions::print_string(value)); + m_headers[name] = m_headers[name].append(_XPLATSTR(", ")).append(utility::conversions::print_string(value)); } else { diff --git a/Release/include/cpprest/uri_builder.h b/Release/include/cpprest/uri_builder.h index e4fb2024fd..1537063e5a 100644 --- a/Release/include/cpprest/uri_builder.h +++ b/Release/include/cpprest/uri_builder.h @@ -238,7 +238,7 @@ namespace web uri_builder &append_query(const utility::string_t &name, const T &value, bool do_encoding = true) { auto encodedName = name; - auto encodedValue = ::utility::conversions::print_string(value, std::locale::classic()); + auto encodedValue = ::utility::conversions::print_string(value); if (do_encoding) { diff --git a/Release/src/uri/uri.cpp b/Release/src/uri/uri.cpp index e9647bb276..b5d12af772 100644 --- a/Release/src/uri/uri.cpp +++ b/Release/src/uri/uri.cpp @@ -53,28 +53,27 @@ utility::string_t uri_components::join() m_path.insert(m_path.begin(), 1, _XPLATSTR('/')); } - utility::ostringstream_t os; - os.imbue(std::locale::classic()); + utility::string_t ret; if (!m_scheme.empty()) { - os << m_scheme << _XPLATSTR(':'); + ret.append(m_scheme).append({ _XPLATSTR(':') }); } if (!m_host.empty()) { - os << _XPLATSTR("//"); + ret.append(_XPLATSTR("//")); if (!m_user_info.empty()) { - os << m_user_info << _XPLATSTR('@'); + ret.append(m_user_info).append({ _XPLATSTR('@') }); } - os << m_host; + ret.append(m_host); if (m_port > 0) { - os << _XPLATSTR(':') << m_port; + ret.append({ _XPLATSTR(':') }).append(utility::conversions::print_string(m_port)); } } @@ -83,22 +82,23 @@ utility::string_t uri_components::join() // only add the leading slash when the host is present if (!m_host.empty() && m_path.front() != _XPLATSTR('/')) { - os << _XPLATSTR('/'); + ret.append({ _XPLATSTR('/') }); } - os << m_path; + + ret.append(m_path); } if (!m_query.empty()) { - os << _XPLATSTR('?') << m_query; + ret.append({ _XPLATSTR('?') }).append(m_query); } if (!m_fragment.empty()) { - os << _XPLATSTR('#') << m_fragment; + ret.append({ _XPLATSTR('#') }).append(m_fragment); } - return os.str(); + return ret; } } diff --git a/Release/src/uri/uri_builder.cpp b/Release/src/uri/uri_builder.cpp index a94a22eb9b..28085161c0 100644 --- a/Release/src/uri/uri_builder.cpp +++ b/Release/src/uri/uri_builder.cpp @@ -110,7 +110,7 @@ utility::string_t uri_builder::to_string() uri uri_builder::to_uri() { - return uri(m_uri.join()); + return uri(m_uri); } bool uri_builder::is_valid() diff --git a/Release/src/uri/uri_parser.cpp b/Release/src/uri/uri_parser.cpp index 1d4f7e33e7..75bf73f273 100644 --- a/Release/src/uri/uri_parser.cpp +++ b/Release/src/uri/uri_parser.cpp @@ -100,7 +100,7 @@ bool parse(const utility::string_t &encoded_string, uri_components &components) // convert scheme to lowercase std::transform(components.m_scheme.begin(), components.m_scheme.end(), components.m_scheme.begin(), [](utility::char_t c) { - return std::tolower(c, std::locale::classic()); + return (utility::char_t)tolower(c); }); } else @@ -119,7 +119,7 @@ bool parse(const utility::string_t &encoded_string, uri_components &components) // convert host to lowercase std::transform(components.m_host.begin(), components.m_host.end(), components.m_host.begin(), [](utility::char_t c) { - return std::tolower(c, std::locale::classic()); + return (utility::char_t)tolower(c); }); } else