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

better subview #709

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .drone.star
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def main(ctx):
linux_cxx("gcc 6", "g++-6", packages="g++-6", buildtype="boost", buildscript="drone", image=linuxglobalimage, environment={'B2_TOOLSET': 'gcc-6', 'B2_CXXSTD': '11,14', 'DRONE_JOB_UUID': '7b52009b64'}, globalenv=globalenv),
linux_cxx("gcc 7", "g++-7", packages="g++-7", buildtype="boost", buildscript="drone", image=linuxglobalimage, environment={'B2_TOOLSET': 'gcc-7', 'B2_CXXSTD': '14,17', 'DRONE_JOB_UUID': 'bd307a3ec3'}, globalenv=globalenv),
linux_cxx("gcc 8", "g++-8", packages="g++-8", buildtype="boost", buildscript="drone", image=linuxglobalimage, environment={'B2_TOOLSET': 'gcc-8', 'B2_CXXSTD': '17,2a', 'DRONE_JOB_UUID': 'fa35e19212'}, globalenv=globalenv),
linux_cxx("gcc 8.3.0", "g++-8", packages="", buildtype="boost", buildscript="drone", image="cppalliance/droneubuntu1804:gcc-8.3.0", environment={'B2_TOOLSET': 'gcc-8', 'B2_CXXSTD': '17,2a'}, globalenv=globalenv),
linux_cxx("gcc 9", "g++-9", packages="g++-9", buildtype="boost", buildscript="drone", image=linuxglobalimage, environment={'B2_TOOLSET': 'gcc-9', 'B2_CXXSTD': '17,2a', 'DRONE_JOB_UUID': 'f1abd67035'}, globalenv=globalenv),
linux_cxx("gcc 10", "g++-10", packages="g++-10", buildtype="boost", buildscript="drone", image="cppalliance/droneubuntu2004:1", environment={'B2_TOOLSET': 'gcc-10', 'B2_CXXSTD': '17,2a', 'DRONE_JOB_UUID': '0716d9708d'}, globalenv=globalenv),
linux_cxx("gcc 10 cmake-superproject", "g++-10", packages="g++-10", image=linuxglobalimage, buildtype="cmake-superproject", buildscript="drone", environment={"COMMENT": "cmake-superproject", "CXX": "g++-10"}, globalenv=globalenv),
Expand Down
3 changes: 3 additions & 0 deletions include/boost/json/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1483,12 +1483,15 @@ class object
}

private:
#ifndef BOOST_JSON_DOCS
// VFALCO friending a detail function makes it public
template<class CharRange>
friend
std::pair<key_value_pair*, std::size_t>
detail::find_in_object(
object const& obj,
CharRange key) noexcept;
#endif

template<class InputIt>
void
Expand Down
74 changes: 53 additions & 21 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 @@ -2248,12 +2248,25 @@ class string
*/
string_view
subview(
std::size_t pos = 0,
std::size_t count = npos) const
std::size_t pos
#ifdef BOOST_JSON_DOCS
= 0
#endif
,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
#ifndef BOOST_JSON_DOCS
string_view
subview() const noexcept
{
return string_view( data(), size() );
}
#endif

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

/** Copy a substring to another string.
Expand Down Expand Up @@ -2281,7 +2294,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 +2473,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 +2500,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 +2531,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 +2559,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 +2590,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 +2620,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 +2647,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 +2679,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 +2709,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 +2738,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 +2775,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 +2814,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 +2837,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 +2860,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 +2883,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 +2902,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 +2925,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