diff --git a/bench/bench.cpp b/bench/bench.cpp index f72127e5..20171c2d 100644 --- a/bench/bench.cpp +++ b/bench/bench.cpp @@ -15,7 +15,6 @@ #include #include -#include #include #include #include @@ -122,7 +121,7 @@ public: boost::string_view s) override { boost::system::error_code ec; - p_->write({s.data(), s.size()}, ec); + p_->write(s.data(), s.size(), ec); } }; @@ -177,7 +176,7 @@ class factory { std::string s_; std::mt19937 g_; - beast::static_string<445> lorem_; + std::string lorem_; int depth_; int indent_ = 4; int max_depth_ = 6; @@ -210,10 +209,11 @@ public: { } - beast::static_string<20> - key() noexcept + std::string const& + key(std::string& s) noexcept { - beast::static_string<20> s; + s.clear(); + s.reserve(20); auto const append = [this, &s]() { @@ -337,7 +337,7 @@ private: void append_integer() { - auto const ns = beast::to_static_string( + auto const ns = std::to_string( rand(std::numeric_limits::max())); s_.append(ns.c_str(), ns.size()); } diff --git a/include/boost/json/basic_parser.hpp b/include/boost/json/basic_parser.hpp index a36f264d..518c6529 100644 --- a/include/boost/json/basic_parser.hpp +++ b/include/boost/json/basic_parser.hpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include @@ -71,44 +70,18 @@ public: void reset(); - template< - class ConstBufferSequence -#ifndef GENERATING_DOCUMENTATION - ,class = typename std::enable_if< - ! std::is_convertible< - ConstBufferSequence, - boost::asio::const_buffer>::value>::type -#endif - > - std::size_t - write_some( - ConstBufferSequence const& buffers, - error_code& ec); - BOOST_JSON_DECL std::size_t write_some( - boost::asio::const_buffer buffer, - error_code& ec); - - template< - class ConstBufferSequence -#ifndef GENERATING_DOCUMENTATION - ,class = typename std::enable_if< - ! std::is_convertible< - ConstBufferSequence, - boost::asio::const_buffer>::value>::type -#endif - > - std::size_t - write( - ConstBufferSequence const& buffers, + void const* data, + std::size_t size, error_code& ec); BOOST_JSON_DECL std::size_t write( - boost::asio::const_buffer buffer, + void const* data, + std::size_t size, error_code& ec); BOOST_JSON_DECL @@ -192,7 +165,6 @@ protected: } // json } // boost -#include #ifdef BOOST_JSON_HEADER_ONLY #include #endif diff --git a/include/boost/json/impl/basic_parser.hpp b/include/boost/json/impl/basic_parser.hpp deleted file mode 100644 index e97fe4af..00000000 --- a/include/boost/json/impl/basic_parser.hpp +++ /dev/null @@ -1,77 +0,0 @@ - // -// Copyright (c) 2018-2019 Vinnie Falco (vinnie dot falco at gmail dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -// Official repository: https://github.com/vinniefalco/json -// - -#ifndef BOOST_JSON_IMPL_BASIC_PARSER_HPP -#define BOOST_JSON_IMPL_BASIC_PARSER_HPP - -namespace boost { -namespace json { - -template -std::size_t -basic_parser:: -write_some( - ConstBufferSequence const& buffers, - error_code& ec) -{ - static_assert( - boost::asio::is_const_buffer_sequence::value, - "ConstBufferSequence type requirements not met"); - - ec = {}; - std::size_t bytes_used = 0; - auto it = - boost::asio::buffer_sequence_begin(buffers); - auto end = - boost::asio::buffer_sequence_end(buffers); - for(;it != end; ++it) - { - bytes_used += - write_some(*it, ec); - if(ec) - return bytes_used; - } - return bytes_used; -} - -template -std::size_t -basic_parser:: -write( - ConstBufferSequence const& buffers, - error_code& ec) -{ - static_assert( - boost::asio::is_const_buffer_sequence::value, - "ConstBufferSequence type requirements not met"); - - ec = {}; - std::size_t bytes_used = 0; - auto it = - boost::asio::buffer_sequence_begin(buffers); - auto end = - boost::asio::buffer_sequence_end(buffers); - if(it != end) - { - for(--end; it != end; ++it) - { - bytes_used += - write_some(*it, ec); - if(ec) - return bytes_used; - } - bytes_used += write(*it, ec); - } - return bytes_used; -} - -} // json -} // boost - -#endif diff --git a/include/boost/json/impl/basic_parser.ipp b/include/boost/json/impl/basic_parser.ipp index c853e148..253129fa 100644 --- a/include/boost/json/impl/basic_parser.ipp +++ b/include/boost/json/impl/basic_parser.ipp @@ -12,7 +12,6 @@ #include #include -#include #include namespace boost { @@ -164,9 +163,7 @@ write_eof(error_code& ec) [this, &ec] { char c = 0; - write_some( - boost::asio::const_buffer( - &c, 1), ec); + write_some(&c, 1, ec); BOOST_ASSERT(ec); }; @@ -182,8 +179,7 @@ write_eof(error_code& ec) case state::number_exp: case state::number_exp_digits2: stack_.front() = state::number_end; - write_some( - boost::asio::const_buffer{}, ec); + write_some(nullptr, 0, ec); if(ec) return; break; @@ -204,15 +200,16 @@ write_eof(error_code& ec) std::size_t basic_parser:: write_some( - boost::asio::const_buffer buffer, + void const* data, + std::size_t size, error_code& ec) { - auto p = static_cast< - char const*>(buffer.data()); - auto n = buffer.size(); + auto p = static_cast(data); + auto n = size; auto const p0 = p; auto const p1 = p0 + n; - beast::static_string<4096> temp; + std::string temp; // VFALCO string bad! + temp.reserve(4096); ec = {}; BOOST_ASSERT(stack_.front() != state::end); auto const maybe_flush = @@ -1015,11 +1012,12 @@ finish: std::size_t basic_parser:: write( - boost::asio::const_buffer buffer, + void const* data, + std::size_t size, error_code& ec) { auto bytes_used = - write_some(buffer, ec); + write_some(data, size, ec); if(! ec) write_eof(ec); return bytes_used; diff --git a/include/boost/json/impl/error.ipp b/include/boost/json/impl/error.ipp index e7fc5ebe..09dca51e 100644 --- a/include/boost/json/impl/error.ipp +++ b/include/boost/json/impl/error.ipp @@ -23,7 +23,7 @@ public: const char* name() const noexcept override { - return "boost.beast.json"; + return "boost.json"; } BOOST_JSON_DECL @@ -113,7 +113,7 @@ public: const char* name() const noexcept override { - return "boost.beast"; + return "boost.json"; } BOOST_JSON_DECL diff --git a/include/boost/json/impl/parse_file.ipp b/include/boost/json/impl/parse_file.ipp index db3e5cfb..fd27344d 100644 --- a/include/boost/json/impl/parse_file.ipp +++ b/include/boost/json/impl/parse_file.ipp @@ -10,9 +10,8 @@ #ifndef BOOST_JSON_IMPL_PARSE_FILE_IPP #define BOOST_JSON_IMPL_PARSE_FILE_IPP -#include -#include -#include +#include +#include namespace boost { namespace json { @@ -23,34 +22,47 @@ parse_file( basic_parser& parser, error_code& ec) { - beast::file f; - f.open(path, beast::file_mode::scan, ec); - if(ec) - return; - beast::flat_buffer b; - auto remain = f.size(ec); - if(ec) - return; - while(remain > 0) + auto const& gc = + boost::system::generic_category(); + + struct cleanup { - auto amount = beast::detail::clamp(remain); - auto mb = b.prepare(amount); - b.commit(f.read(mb.data(), mb.size(), ec)); - if(ec) - return; - if(remain == b.size()) - break; - auto bytes_used = - parser.write_some(b.data(), ec); - if(ec) - return; - remain -= b.size(); - b.consume(bytes_used); - } - parser.write(b.data(), ec); - if(ec) + FILE* f; + ~cleanup() + { + ::fclose(f); + } + }; + + auto f = ::fopen(path, "rb"); + if(! f) + { + ec = error_code(errno, gc); return; - // finished + } + cleanup c{f}; + std::size_t result; + result = ::fseek(f, 0, SEEK_END); + if(result != 0) + { + ec = error_code(errno, gc); + return; + } + auto const size = ::ftell(f); + if(size == -1L) + { + ec = error_code(errno, gc); + return; + } + char* buf = new char[size]; + result = ::fread(buf, 1, size, f); + if(result != 0) + { + ec = error_code(errno, gc); + return; + } + parser.write(buf, size, ec); + delete[] buf; } } // json diff --git a/test/basic_parser.cpp b/test/basic_parser.cpp index 7256378f..119d22e9 100644 --- a/test/basic_parser.cpp +++ b/test/basic_parser.cpp @@ -13,7 +13,6 @@ #include #include -#include namespace boost { namespace json { @@ -114,50 +113,24 @@ public: // write_some with 1 buffer { test_parser p; - auto used = p.write_some( - boost::asio::const_buffer(s.data(), i), ec); + auto used = p.write_some(s.data(), i, ec); BEAST_EXPECT(used == i); BEAST_EXPECT(! p.is_done()); if(! BEAST_EXPECTS(! ec, ec.message())) continue; - used = p.write_some(boost::asio::const_buffer( - s.data() + i, s.size() - i), ec); + used = p.write_some( + s.data() + i, s.size() - i, ec); BEAST_EXPECT(used == s.size() - i); if(! BEAST_EXPECTS(! ec, ec.message())) continue; - p.write({}, ec); + p.write(nullptr, 0, ec); BEAST_EXPECTS(! ec, ec.message()); BEAST_EXPECT(p.is_done()); } - // write_some with 1 buffer sequence - { - test_parser p; - std::array< - boost::asio::const_buffer, 2> b; - b[0] = {s.data(), i}; - b[1] = {s.data()+i, s.size()-i}; - auto used = p.write_some(b, ec); - BEAST_EXPECT(used = s.size()); - BEAST_EXPECTS(! ec, ec.message()); - p.write({}, ec); - BEAST_EXPECTS(! ec, ec.message()); - } - // write with 1 buffer sequence - { - test_parser p; - std::array< - boost::asio::const_buffer, 2> b; - b[0] = {s.data(), i}; - b[1] = {s.data()+i, s.size()-i}; - auto used = p.write(b, ec); - BEAST_EXPECT(used = s.size()); - BEAST_EXPECTS(! ec, ec.message()); - } // write with 1 buffer { test_parser p; - auto used = p.write( - {s.data(), s.size()}, ec); + auto used = p.write(s.data(), s.size(), ec); BEAST_EXPECT(used = s.size()); BEAST_EXPECTS(! ec, ec.message()); } @@ -170,8 +143,7 @@ public: error_code ec; test_parser p; auto const used = p.write_some( - boost::asio::const_buffer( - s.data(), s.size()), ec); + s.data(), s.size(), ec); if(! ec) { if(p.is_done()) diff --git a/test/parser.cpp b/test/parser.cpp index d8b5ae1c..4ba37ab2 100644 --- a/test/parser.cpp +++ b/test/parser.cpp @@ -49,7 +49,7 @@ R"xx({ ; parser p; error_code ec; - p.write({in.data(), in.size()}, ec); + p.write(in.data(), in.size(), ec); if(BEAST_EXPECTS(! ec, ec.message())) { std::stringstream ss;