Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream cleanup #551

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions include/boost/json/basic_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ BOOST_JSON_NS_BEGIN
the error code to a suitable value. This error
code will be returned by the write function to
the caller.
\n
\n
Handlers are required to declare the maximum
limits on various elements. If these limits
are exceeded during parsing, then parsing
fails with an error.
\n
\n
The following declaration meets the parser's
handler requirements:

Expand Down Expand Up @@ -284,7 +284,7 @@ class basic_parser
obj1, obj2, obj3, obj4,
obj5, obj6, obj7, obj8,
obj9, obj10, obj11,
arr1, arr2, arr3,
arr1, arr2, arr3,
arr4, arr5, arr6,
num1, num2, num3, num4,
num5, num6, num7, num8,
Expand Down Expand Up @@ -349,21 +349,21 @@ class basic_parser
inline
const char*
fail(
const char* p,
const char* p,
error ev) noexcept;

BOOST_NOINLINE
inline
const char*
maybe_suspend(
const char* p,
const char* p,
state st);

BOOST_NOINLINE
inline
const char*
maybe_suspend(
const char* p,
const char* p,
state st,
std::size_t n);

Expand Down Expand Up @@ -453,12 +453,12 @@ class basic_parser
std::integral_constant<bool, StackEmpty_> stack_empty,
std::integral_constant<bool, IsKey_> is_key,
/*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8);

template<bool StackEmpty_, char First_>
const char* parse_number(const char* p,
std::integral_constant<bool, StackEmpty_> stack_empty,
std::integral_constant<char, First_> first);

template<bool StackEmpty_, bool IsKey_/*,
bool AllowBadUTF8_*/>
const char* parse_unescaped(const char* p,
Expand Down
56 changes: 1 addition & 55 deletions include/boost/json/detail/stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ namespace detail {

class const_stream
{
friend class local_const_stream;

char const* p_;
char const* end_;

Expand Down Expand Up @@ -89,36 +87,6 @@ class const_stream
}
};

class local_const_stream
: public const_stream
{
const_stream& src_;

public:
explicit
local_const_stream(
const_stream& src) noexcept
: const_stream(src)
, src_(src)
{
}

~local_const_stream()
{
src_.p_ = p_;
}

void
clip(std::size_t n) noexcept
{
if(static_cast<std::size_t>(
src_.end_ - p_) > n)
end_ = p_ + n;
else
end_ = src_.end_;
}
};

class const_stream_wrapper
{
const char*& p_;
Expand Down Expand Up @@ -158,7 +126,7 @@ class const_stream_wrapper
{
return p_ < end_;
}

const char* begin() const noexcept
{
return p_;
Expand Down Expand Up @@ -239,8 +207,6 @@ class clipped_const_stream

class stream
{
friend class local_stream;

char* p_;
char* end_;

Expand Down Expand Up @@ -324,26 +290,6 @@ class stream
}
};

class local_stream
: public stream
{
stream& src_;

public:
explicit
local_stream(
stream& src)
: stream(src)
, src_(src)
{
}

~local_stream()
{
src_.p_ = p_;
}
};

} // detail
BOOST_JSON_NS_END

Expand Down
38 changes: 15 additions & 23 deletions include/boost/json/impl/serializer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ suspend(
template<bool StackEmpty>
bool
serializer::
write_null(stream& ss0)
write_null(stream& ss)
{
local_stream ss(ss0);
if(! StackEmpty && ! st_.empty())
{
state st;
Expand Down Expand Up @@ -118,9 +117,8 @@ do_nul4:
template<bool StackEmpty>
bool
serializer::
write_true(stream& ss0)
write_true(stream& ss)
{
local_stream ss(ss0);
if(! StackEmpty && ! st_.empty())
{
state st;
Expand Down Expand Up @@ -160,9 +158,8 @@ do_tru4:
template<bool StackEmpty>
bool
serializer::
write_false(stream& ss0)
write_false(stream& ss)
{
local_stream ss(ss0);
if(! StackEmpty && ! st_.empty())
{
state st;
Expand Down Expand Up @@ -208,10 +205,8 @@ do_fal5:
template<bool StackEmpty>
bool
serializer::
write_string(stream& ss0)
write_string(stream& ss)
{
local_stream ss(ss0);
local_const_stream cs(cs0_);
if(! StackEmpty && ! st_.empty())
{
state st;
Expand Down Expand Up @@ -254,19 +249,19 @@ do_str1:
do_str2:
if(BOOST_JSON_LIKELY(ss))
{
std::size_t n = cs.remain();
std::size_t n = cs0_.remain();
if(BOOST_JSON_LIKELY(n > 0))
{
if(ss.remain() > n)
n = detail::count_unescaped(
cs.data(), n);
cs0_.data(), n);
else
n = detail::count_unescaped(
cs.data(), ss.remain());
cs0_.data(), ss.remain());
if(n > 0)
{
ss.append(cs.data(), n);
cs.skip(n);
ss.append(cs0_.data(), n);
cs0_.skip(n);
if(! ss)
return suspend(state::str2);
}
Expand All @@ -287,12 +282,12 @@ do_str2:
do_str3:
while(BOOST_JSON_LIKELY(ss))
{
if(BOOST_JSON_LIKELY(cs))
if(BOOST_JSON_LIKELY(cs0_))
{
auto const ch = *cs;
auto const ch = *cs0_;
auto const c = esc[static_cast<
unsigned char>(ch)];
++cs;
++cs0_;
if(! c)
{
ss.append(ch);
Expand Down Expand Up @@ -385,9 +380,8 @@ do_utf5:
template<bool StackEmpty>
bool
serializer::
write_number(stream& ss0)
write_number(stream& ss)
{
local_stream ss(ss0);
if(StackEmpty || st_.empty())
{
switch(jv_->kind())
Expand Down Expand Up @@ -455,10 +449,9 @@ write_number(stream& ss0)
template<bool StackEmpty>
bool
serializer::
write_array(stream& ss0)
write_array(stream& ss)
{
array const* pa;
local_stream ss(ss0);
array::const_iterator it;
array::const_iterator end;
if(StackEmpty || st_.empty())
Expand Down Expand Up @@ -521,10 +514,9 @@ do_arr4:
template<bool StackEmpty>
bool
serializer::
write_object(stream& ss0)
write_object(stream& ss)
{
object const* po;
local_stream ss(ss0);
object::const_iterator it;
object::const_iterator end;
if(StackEmpty || st_.empty())
Expand Down
3 changes: 0 additions & 3 deletions include/boost/json/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ class serializer
// VFALCO Too many streams
using stream = detail::stream;
using const_stream = detail::const_stream;
using local_stream = detail::local_stream;
using local_const_stream =
detail::local_const_stream;

using fn_t = bool (serializer::*)(stream&);

Expand Down