Skip to content

Commit

Permalink
tidy segments
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Sep 14, 2022
1 parent 63db30d commit 89b006b
Show file tree
Hide file tree
Showing 14 changed files with 373 additions and 290 deletions.
48 changes: 13 additions & 35 deletions include/boost/url/detail/any_params_iter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,52 +31,33 @@ namespace detail {
possibly encoded sequence of
query params_ref.
*/
class BOOST_SYMBOL_VISIBLE
struct BOOST_SYMBOL_VISIBLE
any_params_iter
{
bool empty_ = false;

protected:
explicit
any_params_iter(
bool empty) noexcept
: empty_(empty)
{
}

any_params_iter(
bool empty,
string_view const& s0_) noexcept
: empty_(empty)
, s0(s0_)
{
}

any_params_iter(
bool empty,
string_view const& s0_,
string_view const& s1_) noexcept
: empty_(empty)
, s0(s0_)
bool empty_,
string_view s0_ = {},
string_view s1_ = {}) noexcept
: s0(s0_)
, s1(s1_)
, empty(empty_)
{
}

public:
// these are adjusted
// when self-intersecting
string_view s0;
string_view s1;

// True if the sequence is empty
bool empty = false;

BOOST_URL_DECL
virtual
~any_params_iter() noexcept = 0;

// True if the sequence is empty
bool
empty() const noexcept
{
return empty_;
}

// Rewind the iterator to the beginning
virtual
void
Expand Down Expand Up @@ -251,8 +232,6 @@ struct BOOST_SYMBOL_VISIBLE
param_encoded_iter
: any_params_iter
{
// constructor argument will
// throw on invalid input
BOOST_URL_DECL
explicit
param_encoded_iter(
Expand Down Expand Up @@ -342,9 +321,8 @@ struct params_encoded_iter
{
if(it_ == end_)
return false;
// param_pct_view conversion
// will throw on invalid input
measure_impl(n,
// throw on invalid input
measure_impl(n,
param_pct_view(*it_++));
return true;
}
Expand Down
134 changes: 86 additions & 48 deletions include/boost/url/detail/any_segments_iter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,70 +20,53 @@ namespace boost {
namespace urls {
namespace detail {

class BOOST_SYMBOL_VISIBLE
struct BOOST_SYMBOL_VISIBLE
any_segments_iter
{
string_view* s_ = nullptr;

protected:
explicit
any_segments_iter(
string_view* s = nullptr) noexcept
: s_(s)
string_view s_ = {}) noexcept
: s(s_)
{
}

string_view front_;

public:
string_view
front() const noexcept
{
return front_;
}
// this is adjusted
// when self-intersecting
string_view s;

// Return the input string or nullptr
string_view*
input() const noexcept
{
return s_;
}
// the first segment,
// to handle special cases
string_view front;

// Rewind the iterator to the beginning
virtual
void
rewind() noexcept = 0;
virtual void rewind() noexcept = 0;

// Measure and increment the current
// element. n is increased by the
// encoded size. Returns false on
// end of range.
virtual bool measure(
std::size_t& n) noexcept = 0;
virtual bool measure(std::size_t& n) = 0;

// Copy and increment the current
// element. encoding is performed
// if needed.
virtual
void
copy(
char*& dest,
// element, encoding as needed.
virtual void copy(char*& dest,
char const* end) noexcept = 0;
};

//------------------------------------------------

// iterates segments in a string
struct BOOST_SYMBOL_VISIBLE
path_iter :
public any_segments_iter
path_iter
: any_segments_iter
{
explicit
path_iter(
string_view s) noexcept;

protected:
string_view s_;
std::size_t pos_;
std::size_t next_;

Expand All @@ -109,18 +92,44 @@ struct BOOST_SYMBOL_VISIBLE
void copy(char*&, char const*) noexcept override;
};

//------------------------------------------------
//
// segment_iter
//
//------------------------------------------------

// A 1-segment range
// allowing self-intersection
// iterates segments in an encoded string
struct BOOST_SYMBOL_VISIBLE
segment_iter
: any_segments_iter
{
explicit
segment_iter(
string_view s) noexcept;

private:
bool at_end_ = false;
void rewind() noexcept override;
bool measure(std::size_t&) noexcept override;
void copy(char*&, char const*) noexcept override;
};

//------------------------------------------------
//
// segments_iter
//
//------------------------------------------------

class segments_iter_base
struct segments_iter_base
{
protected:
BOOST_URL_DECL static void measure_impl(
BOOST_URL_DECL static void
measure_impl(
string_view, std::size_t&) noexcept;
BOOST_URL_DECL static void copy_impl(
BOOST_URL_DECL static void
copy_impl(
string_view, char*&, char const*) noexcept;
};

Expand All @@ -145,7 +154,7 @@ struct segments_iter
, end_(last)
{
if (first != last)
front_ = *first;
front = *first;
}

private:
Expand Down Expand Up @@ -181,6 +190,30 @@ struct segments_iter
}
};

//------------------------------------------------
//
// segment_encoded_iter
//
//------------------------------------------------

// A 1-segment range
// allowing self-intersection
// iterates segments in an encoded string
struct BOOST_SYMBOL_VISIBLE
segment_encoded_iter
: any_segments_iter
{
explicit
segment_encoded_iter(
pct_string_view const& s) noexcept;

private:
bool at_end_ = false;
void rewind() noexcept override;
bool measure(std::size_t&) noexcept override;
void copy(char*&, char const*) noexcept override;
};

//------------------------------------------------
//
// segments_encoded_iter
Expand All @@ -189,13 +222,15 @@ struct segments_iter

// Validating and copying from
// a string of encoded segments
class segments_encoded_iter_base
struct segments_encoded_iter_base
{
protected:
BOOST_URL_DECL static bool measure_impl(
pct_string_view, std::size_t&) noexcept;
BOOST_URL_DECL static void copy_impl(
string_view, char*&, char const*) noexcept;
BOOST_URL_DECL static void
measure_impl(
std::size_t&, string_view) noexcept;
BOOST_URL_DECL static void
copy_impl(
char*&, char const*, string_view) noexcept;
};

// iterates segments in an
Expand Down Expand Up @@ -224,8 +259,11 @@ struct segments_encoded_iter
, it0_(first)
, end_(last)
{
if (it_ != end_)
front_ = *first;
if(it_ != end_)
{
// throw on invalid input
front = pct_string_view(*first);
}
}

private:
Expand All @@ -241,12 +279,13 @@ struct segments_encoded_iter

bool
measure(
std::size_t& n) noexcept override
std::size_t& n) override
{
if(it_ == end_)
return false;
if(! measure_impl(*it_, n))
return false;
// throw on invalid input
measure_impl(n,
pct_string_view(*it_));
++it_;
return true;
}
Expand All @@ -257,8 +296,7 @@ struct segments_encoded_iter
char const* end
) noexcept override
{
copy_impl(*it_, dest, end);
++it_;
copy_impl(dest, end, *it_++);
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/boost/url/detail/impl/any_params_iter.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void
query_iter::
rewind() noexcept
{
if(empty())
if(empty)
{
at_end_ = true;
return;
Expand Down
Loading

0 comments on commit 89b006b

Please sign in to comment.