Skip to content

Commit 29becf8

Browse files
committed
Deprecate and remove all uses of utility::conversions::print_string/scan_string.
We should eliminate all locale-dependent code internal to the library to enable more consistent behavior on all machines.
1 parent 7d2a9a2 commit 29becf8

File tree

10 files changed

+92
-23
lines changed

10 files changed

+92
-23
lines changed

Release/include/cpprest/asyncrt_utils.h

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ namespace conversions
201201
_ASYNCRTIMP std::vector<unsigned char> __cdecl from_base64(const utility::string_t& str);
202202

203203
template <typename Source>
204-
utility::string_t print_string(const Source &val, const std::locale &loc)
204+
CASABLANCA_DEPRECATED("All locale-sensitive APIs will be removed in a future update. Use stringstreams directly if locale support is required.")
205+
utility::string_t print_string(const Source &val, const std::locale& loc = std::locale())
205206
{
206207
utility::ostringstream_t oss;
207208
oss.imbue(loc);
@@ -213,19 +214,81 @@ namespace conversions
213214
return oss.str();
214215
}
215216

216-
template <typename Source>
217-
utility::string_t print_string(const Source &val)
217+
CASABLANCA_DEPRECATED("All locale-sensitive APIs will be removed in a future update. Use stringstreams directly if locale support is required.")
218+
inline utility::string_t print_string(const utility::string_t &val)
218219
{
219-
return print_string(val, std::locale());
220+
return val;
220221
}
221222

222-
inline utility::string_t print_string(const utility::string_t &val)
223+
namespace details
223224
{
224-
return val;
225+
226+
#if defined(__ANDROID__)
227+
template<class T>
228+
inline std::string to_string(const T& t)
229+
{
230+
std::ostringstream os;
231+
os.imbue(std::locale::classic());
232+
os << t;
233+
return os.str();
234+
}
235+
#endif
236+
237+
template<class T>
238+
inline utility::string_t to_string_t(T&& t)
239+
{
240+
#ifdef _UTF16_STRINGS
241+
using std::to_wstring;
242+
return to_wstring(std::forward<T>(t));
243+
#else
244+
#if !defined(__ANDROID__)
245+
using std::to_string;
246+
#endif
247+
return to_string(std::forward<T>(t));
248+
#endif
249+
}
250+
251+
template <typename Source>
252+
utility::string_t print_string(const Source &val)
253+
{
254+
utility::ostringstream_t oss;
255+
oss.imbue(std::locale::classic());
256+
oss << val;
257+
if (oss.bad())
258+
{
259+
throw std::bad_cast();
260+
}
261+
return oss.str();
262+
}
263+
264+
inline const utility::string_t& print_string(const utility::string_t &val)
265+
{
266+
return val;
267+
}
268+
269+
template <typename Target>
270+
Target scan_string(const utility::string_t &str)
271+
{
272+
Target t;
273+
utility::istringstream_t iss(str);
274+
iss.imbue(std::locale::classic());
275+
iss >> t;
276+
if (iss.bad())
277+
{
278+
throw std::bad_cast();
279+
}
280+
return t;
281+
}
282+
283+
inline const utility::string_t& scan_string(const utility::string_t &str)
284+
{
285+
return str;
286+
}
225287
}
226288

227289
template <typename Target>
228-
Target scan_string(const utility::string_t &str, const std::locale &loc)
290+
CASABLANCA_DEPRECATED("All locale-sensitive APIs will be removed in a future update. Use stringstreams directly if locale support is required.")
291+
Target scan_string(const utility::string_t &str, const std::locale &loc = std::locale())
229292
{
230293
Target t;
231294
utility::istringstream_t iss(str);
@@ -238,12 +301,7 @@ namespace conversions
238301
return t;
239302
}
240303

241-
template <typename Target>
242-
Target scan_string(const utility::string_t &str)
243-
{
244-
return scan_string<Target>(str, std::locale());
245-
}
246-
304+
CASABLANCA_DEPRECATED("All locale-sensitive APIs will be removed in a future update. Use stringstreams directly if locale support is required.")
247305
inline utility::string_t scan_string(const utility::string_t &str)
248306
{
249307
return str;

Release/include/cpprest/http_headers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ class http_headers
147147
{
148148
if (has(name))
149149
{
150-
m_headers[name].append(_XPLATSTR(", ")).append(utility::conversions::print_string(value));
150+
m_headers[name].append(_XPLATSTR(", ")).append(utility::conversions::details::print_string(value));
151151
}
152152
else
153153
{
154-
m_headers[name] = utility::conversions::print_string(value);
154+
m_headers[name] = utility::conversions::details::print_string(value);
155155
}
156156
}
157157

Release/include/cpprest/oauth1.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class oauth1_config
501501

502502
static utility::string_t _generate_timestamp()
503503
{
504-
return utility::conversions::print_string(utility::datetime::utc_timestamp(), std::locale::classic());
504+
return utility::conversions::details::to_string_t(utility::datetime::utc_timestamp());
505505
}
506506

507507
_ASYNCRTIMP static std::vector<unsigned char> __cdecl _hmac_sha1(const utility::string_t& key, const utility::string_t& data);

Release/include/cpprest/uri_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ namespace web
226226
uri_builder &append_query(const utility::string_t &name, const T &value, bool do_encoding = true)
227227
{
228228
auto encodedName = name;
229-
auto encodedValue = ::utility::conversions::print_string(value, std::locale::classic());
229+
auto encodedValue = utility::conversions::details::print_string(value);
230230

231231
if (do_encoding)
232232
{

Release/src/http/client/http_client_asio.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141

4242
using boost::asio::ip::tcp;
4343

44+
#ifdef __ANDROID__
45+
using utility::conversions::details::to_string;
46+
#else
47+
using std::to_string;
48+
#endif
49+
4450
#define CRLF std::string("\r\n")
4551

4652
namespace web { namespace http
@@ -439,7 +445,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
439445

440446
m_context->m_timer.start();
441447

442-
tcp::resolver::query query(proxy_host, utility::conversions::print_string(proxy_port, std::locale::classic()));
448+
tcp::resolver::query query(utility::conversions::to_utf8string(proxy_host), to_string(proxy_port));
443449

444450
auto client = std::static_pointer_cast<asio_client>(m_context->m_http_client);
445451
client->m_resolver.async_resolve(query, boost::bind(&ssl_proxy_tunnel::handle_resolve, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::iterator));
@@ -724,7 +730,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
724730
auto tcp_host = proxy_type == http_proxy_type::http ? proxy_host : host;
725731
auto tcp_port = proxy_type == http_proxy_type::http ? proxy_port : port;
726732

727-
tcp::resolver::query query(tcp_host, utility::conversions::print_string(tcp_port, std::locale::classic()));
733+
tcp::resolver::query query(tcp_host, to_string(tcp_port));
728734
auto client = std::static_pointer_cast<asio_client>(ctx->m_http_client);
729735
client->m_resolver.async_resolve(query, boost::bind(&asio_context::handle_resolve, ctx, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
730736
}

Release/src/http/common/http_msg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ utility::size64_t http_headers::content_length() const
208208

209209
void http_headers::set_content_length(utility::size64_t length)
210210
{
211-
m_headers[http::header_names::content_length] = utility::conversions::print_string(length, std::locale::classic());
211+
m_headers[http::header_names::content_length] = utility::conversions::details::to_string_t(length);
212212
}
213213

214214
namespace details {

Release/src/http/listener/http_server_asio.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#pragma clang diagnostic pop
2323
#endif
2424

25+
#ifdef __ANDROID__
26+
using utility::conversions::details::to_string;
27+
#else
28+
using std::to_string;
29+
#endif
2530
using namespace boost::asio;
2631
using namespace boost::asio::ip;
2732

Release/src/uri/uri.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ utility::string_t uri_components::join()
6161

6262
if (m_port > 0)
6363
{
64-
ret.append({ _XPLATSTR(':') }).append(utility::conversions::print_string(m_port, std::locale::classic()));
64+
ret.append({ _XPLATSTR(':') }).append(utility::conversions::details::to_string_t(m_port));
6565
}
6666
}
6767

Release/src/uri/uri_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ bool inner_parse(
266266
//skip the colon
267267
port_begin++;
268268

269-
*port = utility::conversions::scan_string<int>(utility::string_t(port_begin, authority_end), std::locale::classic());
269+
*port = utility::conversions::details::scan_string<int>(utility::string_t(port_begin, authority_end));
270270
}
271271
else
272272
{

Release/tests/functional/http/client/header_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ TEST_FIXTURE(uri_address, overwrite_http_header)
379379
// Test default case of cpprestsdk setting host header as host:port
380380
auto& host = m_uri.host();
381381
int port = m_uri.port();
382-
utility::string_t expected_default_header = host + U(":") + utility::conversions::print_string(port);
382+
utility::string_t expected_default_header = host + U(":") + utility::conversions::details::to_string_t(port);
383383
http_request default_host_headers_request(methods::GET);
384384
scoped.server()->next_request().then([&](test_request *p_request)
385385
{

0 commit comments

Comments
 (0)