Remove use of lexical_cast

This commit is contained in:
Vinnie Falco
2017-07-21 08:55:25 -07:00
parent 7139dd639a
commit b97586b318
16 changed files with 134 additions and 41 deletions

View File

@@ -1,6 +1,7 @@
Version 86: Version 86:
* Boost prep * Boost prep
* Remove use of lexical_cast
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@@ -12,7 +12,6 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp> #include <boost/beast/websocket.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <chrono> #include <chrono>
#include <cstdlib> #include <cstdlib>

View File

@@ -12,7 +12,6 @@
#include <boost/beast/unit_test/amount.hpp> #include <boost/beast/unit_test/amount.hpp>
#include <boost/beast/unit_test/recorder.hpp> #include <boost/beast/unit_test/recorder.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
@@ -218,8 +217,7 @@ reporter<_>::fmtdur(typename clock_type::duration const& d)
using namespace std::chrono; using namespace std::chrono;
auto const ms = duration_cast<milliseconds>(d); auto const ms = duration_cast<milliseconds>(d);
if(ms < seconds{1}) if(ms < seconds{1})
return boost::lexical_cast<std::string>( return std::to_string(ms.count()) + "ms";
ms.count()) + "ms";
std::stringstream ss; std::stringstream ss;
ss << std::fixed << std::setprecision(1) << ss << std::fixed << std::setprecision(1) <<
(ms.count()/1000.) << "s"; (ms.count()/1000.) << "s";

View File

@@ -12,7 +12,6 @@
#include <boost/beast/unit_test/runner.hpp> #include <boost/beast/unit_test/runner.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include <ostream> #include <ostream>
#include <sstream> #include <sstream>
@@ -36,7 +35,7 @@ make_reason(String const& reason,
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
s.append(fs::path{file}.filename().string()); s.append(fs::path{file}.filename().string());
s.append("("); s.append("(");
s.append(boost::lexical_cast<std::string>(line)); s.append(std::to_string(line));
s.append(")"); s.append(")");
return s; return s;
} }

View File

@@ -16,7 +16,6 @@
#include <boost/beast/core/flat_buffer.hpp> #include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/multi_buffer.hpp> #include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/unit_test/suite.hpp> #include <boost/beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@@ -42,8 +41,14 @@ public:
std::string std::string
to_string(ConstBufferSequence const& bs) to_string(ConstBufferSequence const& bs)
{ {
return boost::lexical_cast< using boost::asio::buffer_cast;
std::string>(buffers(bs)); using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(boost::asio::const_buffer b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
} }
corpus corpus

View File

@@ -16,7 +16,6 @@
#include <boost/beast/unit_test/suite.hpp> #include <boost/beast/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp> #include <boost/asio/buffer.hpp>
#include <boost/asio/streambuf.hpp> #include <boost/asio/streambuf.hpp>
#include <boost/lexical_cast.hpp>
#include <iterator> #include <iterator>
namespace boost { namespace boost {
@@ -30,8 +29,14 @@ public:
std::string std::string
to_string(ConstBufferSequence const& bs) to_string(ConstBufferSequence const& bs)
{ {
return boost::lexical_cast< using boost::asio::buffer_cast;
std::string>(buffers(bs)); using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(boost::asio::const_buffer b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
} }
void testBuffersAdapter() void testBuffersAdapter()

View File

@@ -17,7 +17,6 @@
#include <boost/beast/core/string.hpp> #include <boost/beast/core/string.hpp>
#include <boost/beast/test/test_allocator.hpp> #include <boost/beast/test/test_allocator.hpp>
#include <boost/beast/unit_test/suite.hpp> #include <boost/beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
#include <algorithm> #include <algorithm>
namespace boost { namespace boost {

View File

@@ -12,7 +12,6 @@
#include <boost/beast/core/multi_buffer.hpp> #include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/unit_test/suite.hpp> #include <boost/beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
#include <ostream> #include <ostream>
namespace boost { namespace boost {
@@ -21,6 +20,21 @@ namespace beast {
class ostream_test : public beast::unit_test::suite class ostream_test : public beast::unit_test::suite
{ {
public: public:
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(boost::asio::const_buffer b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
}
void void
run() override run() override
{ {
@@ -29,8 +43,7 @@ public:
auto os = ostream(b); auto os = ostream(b);
os << "Hello, world!\n"; os << "Hello, world!\n";
os.flush(); os.flush();
BOOST_BEAST_EXPECT(boost::lexical_cast<std::string>( BOOST_BEAST_EXPECT(to_string(b.data()) == "Hello, world!\n");
buffers(b.data())) == "Hello, world!\n");
auto os2 = std::move(os); auto os2 = std::move(os);
} }
{ {
@@ -45,8 +58,7 @@ public:
"0123456789abcdef" "0123456789abcdef" "0123456789abcdef" "0123456789abcdef"; "0123456789abcdef" "0123456789abcdef" "0123456789abcdef" "0123456789abcdef";
multi_buffer b; multi_buffer b;
ostream(b) << s; ostream(b) << s;
BOOST_BEAST_EXPECT(boost::lexical_cast<std::string>( BOOST_BEAST_EXPECT(to_string(b.data()) == s);
buffers(b.data())) == s);
} }
} }
}; };

View File

@@ -26,6 +26,7 @@
#include <array> #include <array>
#include <limits> #include <limits>
#include <list> #include <list>
#include <sstream>
#include <vector> #include <vector>
namespace boost { namespace boost {
@@ -43,6 +44,31 @@ public:
{ {
} }
template<bool isRequest, class Body, class Fields>
static
std::string
to_string(message<isRequest, Body, Fields> const& m)
{
std::stringstream ss;
ss << m;
return ss.str();
}
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(boost::asio::const_buffer b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
}
template<bool isRequest> template<bool isRequest>
bool bool
equal_body(string_view sv, string_view body) equal_body(string_view sv, string_view body)
@@ -157,8 +183,7 @@ public:
response<string_body> res; response<string_body> res;
read_istream(is, buffer, res, ec); read_istream(is, buffer, res, ec);
BOOST_BEAST_EXPECTS(! ec, ec.message()); BOOST_BEAST_EXPECTS(! ec, ec.message());
BOOST_BEAST_EXPECT(boost::lexical_cast< BOOST_BEAST_EXPECT(to_string(res) == s);
std::string>(res) == s);
} }
void void
@@ -173,8 +198,7 @@ public:
error_code ec; error_code ec;
write_ostream(os, req, ec); write_ostream(os, req, ec);
BOOST_BEAST_EXPECTS(! ec, ec.message()); BOOST_BEAST_EXPECTS(! ec, ec.message());
BOOST_BEAST_EXPECT(boost::lexical_cast< BOOST_BEAST_EXPECT(to_string(req) == os.str());
std::string>(req) == os.str());
} }
void void
@@ -343,8 +367,7 @@ public:
std::allocator<double>{} std::allocator<double>{}
), ec); ), ec);
BOOST_BEAST_EXPECT( BOOST_BEAST_EXPECT(
boost::lexical_cast<std::string>( to_string(p.server.buffer.data()) ==
buffers(p.server.buffer.data())) ==
"HTTP/1.1 200 OK\r\n" "HTTP/1.1 200 OK\r\n"
"Server: test\r\n" "Server: test\r\n"
"Accept: Expires, Content-MD5\r\n" "Accept: Expires, Content-MD5\r\n"

View File

@@ -17,7 +17,6 @@
#include <boost/beast/http/write.hpp> #include <boost/beast/http/write.hpp>
#include <boost/beast/test/string_istream.hpp> #include <boost/beast/test/string_istream.hpp>
#include <boost/beast/unit_test/suite.hpp> #include <boost/beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
namespace boost { namespace boost {
namespace beast { namespace beast {
@@ -28,6 +27,31 @@ class dynamic_body_test : public beast::unit_test::suite
boost::asio::io_service ios_; boost::asio::io_service ios_;
public: public:
template<bool isRequest, class Body, class Fields>
static
std::string
to_string(message<isRequest, Body, Fields> const& m)
{
std::stringstream ss;
ss << m;
return ss.str();
}
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(boost::asio::const_buffer b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
}
void void
run() override run() override
{ {
@@ -42,9 +66,8 @@ public:
multi_buffer b; multi_buffer b;
read(ss, b, p); read(ss, b, p);
auto const& m = p.get(); auto const& m = p.get();
BOOST_BEAST_EXPECT(boost::lexical_cast<std::string>( BOOST_BEAST_EXPECT(to_string(m.body.data()) == "xyz");
buffers(m.body.data())) == "xyz"); BOOST_BEAST_EXPECT(to_string(m) == s);
BOOST_BEAST_EXPECT(boost::lexical_cast<std::string>(m) == s);
} }
}; };

View File

@@ -15,7 +15,7 @@
#include <boost/beast/http/type_traits.hpp> #include <boost/beast/http/type_traits.hpp>
#include <boost/beast/test/test_allocator.hpp> #include <boost/beast/test/test_allocator.hpp>
#include <boost/beast/unit_test/suite.hpp> #include <boost/beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp> #include <string>
namespace boost { namespace boost {
namespace beast { namespace beast {
@@ -37,7 +37,7 @@ public:
fill(std::size_t n, basic_fields<Allocator>& f) fill(std::size_t n, basic_fields<Allocator>& f)
{ {
for(std::size_t i = 1; i<= n; ++i) for(std::size_t i = 1; i<= n; ++i)
f.insert(boost::lexical_cast<std::string>(i), i); f.insert(std::to_string(i), i);
} }
template<class U, class V> template<class U, class V>

View File

@@ -258,6 +258,31 @@ public:
}; };
}; };
template<bool isRequest, class Body, class Fields>
static
std::string
to_string(message<isRequest, Body, Fields> const& m)
{
std::stringstream ss;
ss << m;
return ss.str();
}
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(boost::asio::const_buffer b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
}
template<bool isRequest> template<bool isRequest>
bool bool
equal_body(string_view sv, string_view body) equal_body(string_view sv, string_view body)
@@ -574,7 +599,7 @@ public:
m.version = 11; m.version = 11;
m.set(field::user_agent, "test"); m.set(field::user_agent, "test");
m.body = "*"; m.body = "*";
BOOST_BEAST_EXPECT(boost::lexical_cast<std::string>(m) == BOOST_BEAST_EXPECT(to_string(m) ==
"GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n*"); "GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n*");
} }

View File

@@ -46,10 +46,17 @@ public:
std::string std::string
to_string(ConstBufferSequence const& bs) to_string(ConstBufferSequence const& bs)
{ {
return boost::lexical_cast< using boost::asio::buffer_cast;
std::string>(buffers(bs)); using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(boost::asio::const_buffer b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
} }
struct con struct con
{ {
stream<socket_type> ws; stream<socket_type> ws;

View File

@@ -13,7 +13,6 @@
#include <boost/beast/core/multi_buffer.hpp> #include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/websocket/stream.hpp> #include <boost/beast/websocket/stream.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <atomic> #include <atomic>
#include <functional> #include <functional>
@@ -374,8 +373,8 @@ private:
if(d.server.log_) if(d.server.log_)
if(ec != boost::beast::websocket::error::closed) if(ec != boost::beast::websocket::error::closed)
d.server.fail("[#" + std::to_string(d.id) + d.server.fail("[#" + std::to_string(d.id) +
" " + boost::lexical_cast<std::string>(d.ep) + " " + d.ep.address().to_string() + ":" +
"] " + what, ec); std::to_string(d.ep.port()) + "] " + what, ec);
} }
}; };

View File

@@ -12,7 +12,6 @@
#include <boost/beast/core/multi_buffer.hpp> #include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/websocket.hpp> #include <boost/beast/websocket.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <atomic> #include <atomic>
#include <functional> #include <functional>
@@ -216,8 +215,8 @@ private:
if(log_) if(log_)
if(ec != boost::beast::websocket::error::closed) if(ec != boost::beast::websocket::error::closed)
fail("[#" + std::to_string(id) + " " + fail("[#" + std::to_string(id) + " " +
boost::lexical_cast<std::string>(ep) + ep.address().to_string() + ":" +
"] " + what, ec); std::to_string(ep.port()) + "] " + what, ec);
} }
void void

View File

@@ -14,7 +14,6 @@
#include <boost/beast/websocket.hpp> #include <boost/beast/websocket.hpp>
#include <boost/beast/unit_test/dstream.hpp> #include <boost/beast/unit_test/dstream.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <cstdlib> #include <cstdlib>
@@ -148,7 +147,7 @@ private:
if(ec) if(ec)
return fail("on_connect", ec); return fail("on_connect", ec);
ws_.async_handshake( ws_.async_handshake(
boost::lexical_cast<std::string>(ep_), ep_.address().to_string() + ":" + std::to_string(ep_.port()),
"/", "/",
alloc_.wrap(std::bind( alloc_.wrap(std::bind(
&connection::on_handshake, &connection::on_handshake,