Skip to content

Commit

Permalink
better subview
Browse files Browse the repository at this point in the history
fix #708
  • Loading branch information
vinniefalco committed May 26, 2022
1 parent 5a6b2ce commit 5ccf9c8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
69 changes: 49 additions & 20 deletions include/boost/json/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@ class string
int
compare(string_view sv) const noexcept
{
return string_view(*this).compare(sv);
return subview().compare(sv);
}

//------------------------------------------------------
Expand Down Expand Up @@ -2246,14 +2246,24 @@ class string
@throw std::out_of_range `pos > size()`
*/
/**@{*/
string_view
subview(
std::size_t pos = 0,
std::size_t pos,
std::size_t count = npos) const
{
return string_view(*this).substr(pos, count);
return subview().substr(pos, count);
}

// this is a faster, leaner, noexcept
// version of subview() with no args
string_view
subview() const noexcept
{
return string_view( data(), size() );
}
/**@}*/

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

/** Copy a substring to another string.
Expand Down Expand Up @@ -2281,7 +2291,7 @@ class string
std::size_t count,
std::size_t pos = 0) const
{
return string_view(*this).copy(dest, count, pos);
return subview().copy(dest, count, pos);
}

//------------------------------------------------------
Expand Down Expand Up @@ -2460,7 +2470,7 @@ class string
string_view sv,
std::size_t pos = 0) const noexcept
{
return string_view(*this).find(sv, pos);
return subview().find(sv, pos);
}

/** Find the first occurrence of a character within the string.
Expand All @@ -2487,7 +2497,7 @@ class string
char ch,
std::size_t pos = 0) const noexcept
{
return string_view(*this).find(ch, pos);
return subview().find(ch, pos);
}

//------------------------------------------------------
Expand Down Expand Up @@ -2518,7 +2528,7 @@ class string
string_view sv,
std::size_t pos = npos) const noexcept
{
return string_view(*this).rfind(sv, pos);
return subview().rfind(sv, pos);
}

/** Find the last occurrence of a character within the string.
Expand Down Expand Up @@ -2546,7 +2556,7 @@ class string
char ch,
std::size_t pos = npos) const noexcept
{
return string_view(*this).rfind(ch, pos);
return subview().rfind(ch, pos);
}

//------------------------------------------------------
Expand Down Expand Up @@ -2577,7 +2587,7 @@ class string
string_view sv,
std::size_t pos = 0) const noexcept
{
return string_view(*this).find_first_of(sv, pos);
return subview().find_first_of(sv, pos);
}

//------------------------------------------------------
Expand Down Expand Up @@ -2607,7 +2617,7 @@ class string
string_view sv,
std::size_t pos = 0) const noexcept
{
return string_view(*this).find_first_not_of(sv, pos);
return subview().find_first_not_of(sv, pos);
}

/** Find the first occurrence of a character not equal to `ch`.
Expand All @@ -2634,7 +2644,7 @@ class string
char ch,
std::size_t pos = 0) const noexcept
{
return string_view(*this).find_first_not_of(ch, pos);
return subview().find_first_not_of(ch, pos);
}

//------------------------------------------------------
Expand Down Expand Up @@ -2666,7 +2676,7 @@ class string
string_view sv,
std::size_t pos = npos) const noexcept
{
return string_view(*this).find_last_of(sv, pos);
return subview().find_last_of(sv, pos);
}

//------------------------------------------------------
Expand Down Expand Up @@ -2696,7 +2706,7 @@ class string
string_view sv,
std::size_t pos = npos) const noexcept
{
return string_view(*this).find_last_not_of(sv, pos);
return subview().find_last_not_of(sv, pos);
}

/** Find the last occurrence of a character not equal to `ch`.
Expand Down Expand Up @@ -2725,7 +2735,7 @@ class string
char ch,
std::size_t pos = npos) const noexcept
{
return string_view(*this).find_last_not_of(ch, pos);
return subview().find_last_not_of(ch, pos);
}

private:
Expand Down Expand Up @@ -2762,6 +2772,25 @@ class string

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

namespace detail {

template<class T>
inline
string_view
to_sv(T const& t) noexcept
{
return string_view(t);
}

inline
string_view
to_sv(json::string const& jv) noexcept
{
return jv.subview();
}

} // detail

/** Return true if lhs equals rhs.
A lexicographical comparison is used.
Expand All @@ -2782,7 +2811,7 @@ typename std::enable_if<
operator==(T const& lhs, U const& rhs) noexcept
#endif
{
return string_view(lhs) == string_view(rhs);
return detail::to_sv(lhs) == detail::to_sv(rhs);
}

/** Return true if lhs does not equal rhs.
Expand All @@ -2805,7 +2834,7 @@ typename std::enable_if<
operator!=(T const& lhs, U const& rhs) noexcept
#endif
{
return string_view(lhs) != string_view(rhs);
return detail::to_sv(lhs) != detail::to_sv(rhs);
}

/** Return true if lhs is less than rhs.
Expand All @@ -2828,7 +2857,7 @@ typename std::enable_if<
operator<(T const& lhs, U const& rhs) noexcept
#endif
{
return string_view(lhs) < string_view(rhs);
return detail::to_sv(lhs) < detail::to_sv(rhs);
}

/** Return true if lhs is less than or equal to rhs.
Expand All @@ -2851,7 +2880,7 @@ typename std::enable_if<
operator<=(T const& lhs, U const& rhs) noexcept
#endif
{
return string_view(lhs) <= string_view(rhs);
return detail::to_sv(lhs) <= detail::to_sv(rhs);
}

#ifdef BOOST_JSON_DOCS
Expand All @@ -2870,7 +2899,7 @@ typename std::enable_if<
operator>=(T const& lhs, U const& rhs) noexcept
#endif
{
return string_view(lhs) >= string_view(rhs);
return detail::to_sv(lhs) >= detail::to_sv(rhs);
}

/** Return true if lhs is greater than rhs.
Expand All @@ -2893,7 +2922,7 @@ typename std::enable_if<
operator>(T const& lhs, U const& rhs) noexcept
#endif
{
return string_view(lhs) > string_view(rhs);
return detail::to_sv(lhs) > detail::to_sv(rhs);
}

BOOST_JSON_NS_END
Expand Down
6 changes: 2 additions & 4 deletions test/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,10 +1134,8 @@ class string_test

// operator string_view()
{
BOOST_TEST(
string_view(cs1) == t.v1);
BOOST_TEST(
string_view(cs2) == t.v2);
BOOST_TEST(cs1.subview() == t.v1);
BOOST_TEST(cs2.subview() == t.v2);
}
}

Expand Down

0 comments on commit 5ccf9c8

Please sign in to comment.