Skip to content

Commit d79c9a0

Browse files
committed
Merge pull request microsoft#149 from hanzhumsft/uri_perf_improvements
Some perf improvements for uri related code
2 parents 944daa7 + 8e09ae6 commit d79c9a0

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

Release/include/cpprest/asyncrt_utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ namespace conversions
197197
return print_string(val, std::locale());
198198
}
199199

200+
inline utility::string_t print_string(const utility::string_t &val)
201+
{
202+
return val;
203+
}
204+
200205
template <typename Target>
201206
Target scan_string(const utility::string_t &str, const std::locale &loc)
202207
{
@@ -216,6 +221,11 @@ namespace conversions
216221
{
217222
return scan_string<Target>(str, std::locale());
218223
}
224+
225+
inline utility::string_t scan_string(const utility::string_t &str)
226+
{
227+
return str;
228+
}
219229
}
220230

221231
namespace details

Release/include/cpprest/base_uri.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ namespace web {
223223
/// </summary>
224224
uri() { m_uri = _XPLATSTR("/");};
225225

226+
/// <summary>
227+
/// Creates a URI from the given URI components.
228+
/// </summary>
229+
/// <param name="components">A URI components object to create the URI instance.</param>
230+
_ASYNCRTIMP uri(const details::uri_components &components);
231+
226232
/// <summary>
227233
/// Creates a URI from the given encoded string. This will throw an exception if the string
228234
/// does not contain a valid URI. Use uri::validate if processing user-input.

Release/include/cpprest/http_headers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class http_headers
159159
{
160160
if (has(name))
161161
{
162-
m_headers[name] = m_headers[name].append(_XPLATSTR(", ") + utility::conversions::print_string(value));
162+
m_headers[name].append(_XPLATSTR(", ")).append(utility::conversions::print_string(value));
163163
}
164164
else
165165
{

Release/src/uri/uri.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,27 @@ utility::string_t uri_components::join()
5353
m_path.insert(m_path.begin(), 1, _XPLATSTR('/'));
5454
}
5555

56-
utility::ostringstream_t os;
57-
os.imbue(std::locale::classic());
56+
utility::string_t ret;
5857

5958
if (!m_scheme.empty())
6059
{
61-
os << m_scheme << _XPLATSTR(':');
60+
ret.append(m_scheme).append({ _XPLATSTR(':') });
6261
}
6362

6463
if (!m_host.empty())
6564
{
66-
os << _XPLATSTR("//");
65+
ret.append(_XPLATSTR("//"));
6766

6867
if (!m_user_info.empty())
6968
{
70-
os << m_user_info << _XPLATSTR('@');
69+
ret.append(m_user_info).append({ _XPLATSTR('@') });
7170
}
7271

73-
os << m_host;
72+
ret.append(m_host);
7473

7574
if (m_port > 0)
7675
{
77-
os << _XPLATSTR(':') << m_port;
76+
ret.append({ _XPLATSTR(':') }).append(utility::conversions::print_string(m_port, std::locale::classic()));
7877
}
7978
}
8079

@@ -83,27 +82,37 @@ utility::string_t uri_components::join()
8382
// only add the leading slash when the host is present
8483
if (!m_host.empty() && m_path.front() != _XPLATSTR('/'))
8584
{
86-
os << _XPLATSTR('/');
85+
ret.append({ _XPLATSTR('/') });
8786
}
88-
os << m_path;
87+
88+
ret.append(m_path);
8989
}
9090

9191
if (!m_query.empty())
9292
{
93-
os << _XPLATSTR('?') << m_query;
93+
ret.append({ _XPLATSTR('?') }).append(m_query);
9494
}
9595

9696
if (!m_fragment.empty())
9797
{
98-
os << _XPLATSTR('#') << m_fragment;
98+
ret.append({ _XPLATSTR('#') }).append(m_fragment);
9999
}
100100

101-
return os.str();
101+
return ret;
102102
}
103103
}
104104

105105
using namespace details;
106106

107+
uri::uri(const details::uri_components &components) : m_components(components)
108+
{
109+
m_uri = m_components.join();
110+
if (!details::uri_parser::validate(m_uri))
111+
{
112+
throw uri_exception("provided uri is invalid: " + utility::conversions::to_utf8string(m_uri));
113+
}
114+
}
115+
107116
uri::uri(const utility::string_t &uri_string)
108117
{
109118
if (!details::uri_parser::parse(uri_string, m_components))

Release/src/uri/uri_builder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ utility::string_t uri_builder::to_string()
110110

111111
uri uri_builder::to_uri()
112112
{
113-
return uri(m_uri.join());
113+
return uri(m_uri);
114114
}
115115

116116
bool uri_builder::is_valid()

Release/src/uri/uri_parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ bool parse(const utility::string_t &encoded_string, uri_components &components)
100100

101101
// convert scheme to lowercase
102102
std::transform(components.m_scheme.begin(), components.m_scheme.end(), components.m_scheme.begin(), [](utility::char_t c) {
103-
return std::tolower(c, std::locale::classic());
103+
return (utility::char_t)tolower(c);
104104
});
105105
}
106106
else
@@ -119,7 +119,7 @@ bool parse(const utility::string_t &encoded_string, uri_components &components)
119119

120120
// convert host to lowercase
121121
std::transform(components.m_host.begin(), components.m_host.end(), components.m_host.begin(), [](utility::char_t c) {
122-
return std::tolower(c, std::locale::classic());
122+
return (utility::char_t)tolower(c);
123123
});
124124
}
125125
else

0 commit comments

Comments
 (0)