Skip to content

Commit

Permalink
Replaces the usage of boost::string_view with std::string_view (XRPLF…
Browse files Browse the repository at this point in the history
  • Loading branch information
ckeshava authored and vlntb committed Aug 23, 2024
1 parent 63aefbd commit dd4b5d5
Show file tree
Hide file tree
Showing 24 changed files with 131 additions and 90 deletions.
14 changes: 14 additions & 0 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ then you will need to choose the `libstdc++11` ABI:
conan profile update settings.compiler.libcxx=libstdc++11 default
```


Ensure inter-operability between `boost::string_view` and `std::string_view` types:

```
conan profile update 'conf.tools.build:cxxflags+=["-DBOOST_BEAST_USE_STD_STRING_VIEW"]' default
conan profile update 'env.CXXFLAGS="-DBOOST_BEAST_USE_STD_STRING_VIEW"' default
```

If you have other flags in the `conf.tools.build` or `env.CXXFLAGS` sections, make sure to retain the existing flags and append the new ones. You can check them with:
```
conan profile show default
```


**Windows** developers may need to use the x64 native build tools.
An easy way to do that is to run the shortcut "x64 Native Tools Command
Prompt" for the version of Visual Studio that you have installed.
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/app/misc/ValidatorList.h
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ class ValidatorList
*/
std::optional<Json::Value>
getAvailable(
boost::beast::string_view const& pubKey,
std::string_view pubKey,
std::optional<std::uint32_t> forceVersion = {});

/** Return the number of configured validator list sites. */
Expand Down
6 changes: 3 additions & 3 deletions src/ripple/app/misc/impl/ValidatorList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <boost/regex.hpp>

#include <cmath>
#include <mutex>
#include <numeric>
#include <shared_mutex>

Expand Down Expand Up @@ -215,7 +214,8 @@ ValidatorList::load(
return false;
}

auto const id = parseBase58<PublicKey>(TokenType::NodePublic, match[1]);
auto const id =
parseBase58<PublicKey>(TokenType::NodePublic, match[1].str());

if (!id)
{
Expand Down Expand Up @@ -1707,7 +1707,7 @@ ValidatorList::for_each_available(

std::optional<Json::Value>
ValidatorList::getAvailable(
boost::beast::string_view const& pubKey,
std::string_view pubKey,
std::optional<std::uint32_t> forceVersion /* = {} */)
{
std::shared_lock read_lock{mutex_};
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/basics/StringUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ strUnHex(std::string const& strSrc)
}

inline std::optional<Blob>
strViewUnHex(boost::string_view const& strSrc)
strViewUnHex(std::string_view strSrc)
{
return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend());
}
Expand Down Expand Up @@ -150,7 +150,7 @@ to_uint64(std::string const& s);
doesn't check whether the TLD is valid.
*/
bool
isProperlyFormedTomlDomain(std::string const& domain);
isProperlyFormedTomlDomain(std::string_view domain);

} // namespace ripple

Expand Down
2 changes: 1 addition & 1 deletion src/ripple/basics/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ base64_encode(std::string const& s)
}

std::string
base64_decode(std::string const& data);
base64_decode(std::string_view data);

} // namespace ripple

Expand Down
4 changes: 2 additions & 2 deletions src/ripple/basics/impl/StringUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ to_uint64(std::string const& s)
}

bool
isProperlyFormedTomlDomain(std::string const& domain)
isProperlyFormedTomlDomain(std::string_view domain)
{
// The domain must be between 4 and 128 characters long
if (domain.size() < 4 || domain.size() > 128)
Expand All @@ -143,7 +143,7 @@ isProperlyFormedTomlDomain(std::string const& domain)
,
boost::regex_constants::optimize);

return boost::regex_match(domain, re);
return boost::regex_match(domain.begin(), domain.end(), re);
}

} // namespace ripple
2 changes: 1 addition & 1 deletion src/ripple/basics/impl/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ base64_encode(std::uint8_t const* data, std::size_t len)
}

std::string
base64_decode(std::string const& data)
base64_decode(std::string_view data)
{
std::string dest;
dest.resize(base64::decoded_size(data.size()));
Expand Down
61 changes: 48 additions & 13 deletions src/ripple/beast/core/LexicalCast.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef BEAST_MODULE_CORE_TEXT_LEXICALCAST_H_INCLUDED
#define BEAST_MODULE_CORE_TEXT_LEXICALCAST_H_INCLUDED

#include <boost/core/detail/string_view.hpp>
#include <algorithm>
#include <cassert>
#include <cerrno>
Expand Down Expand Up @@ -64,9 +65,9 @@ struct LexicalCast<std::string, In>
}
};

// Parse std::string to number
template <class Out>
struct LexicalCast<Out, std::string>
// Parse a std::string_view into a number
template <typename Out>
struct LexicalCast<Out, std::string_view>
{
explicit LexicalCast() = default;

Expand All @@ -78,7 +79,7 @@ struct LexicalCast<Out, std::string>
std::enable_if_t<
std::is_integral_v<Integral> && !std::is_same_v<Integral, bool>,
bool>
operator()(Integral& out, std::string const& in) const
operator()(Integral& out, std::string_view in) const
{
auto first = in.data();
auto last = in.data() + in.size();
Expand All @@ -92,20 +93,23 @@ struct LexicalCast<Out, std::string>
}

bool
operator()(bool& out, std::string in) const
operator()(bool& out, std::string_view in) const
{
std::string result;

// Convert the input to lowercase
std::transform(in.begin(), in.end(), in.begin(), [](auto c) {
return std::tolower(static_cast<unsigned char>(c));
});
std::transform(
in.begin(), in.end(), std::back_inserter(result), [](auto c) {
return std::tolower(static_cast<unsigned char>(c));
});

if (in == "1" || in == "true")
if (result == "1" || result == "true")
{
out = true;
return true;
}

if (in == "0" || in == "false")
if (result == "0" || result == "false")
{
out = false;
return true;
Expand All @@ -114,9 +118,38 @@ struct LexicalCast<Out, std::string>
return false;
}
};

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

// Parse boost library's string_view to number or boolean value
// Note: As of Jan 2024, Boost contains three different types of string_view
// (boost::core::basic_string_view<char>, boost::string_ref and
// boost::string_view). The below template specialization is included because
// it is used in the handshake.cpp file
template <class Out>
struct LexicalCast<Out, boost::core::basic_string_view<char>>
{
explicit LexicalCast() = default;

bool
operator()(Out& out, boost::core::basic_string_view<char> in) const
{
return LexicalCast<Out, std::string_view>()(out, in);
}
};

// Parse std::string to number or boolean value
template <class Out>
struct LexicalCast<Out, std::string>
{
explicit LexicalCast() = default;

bool
operator()(Out& out, std::string in) const
{
return LexicalCast<Out, std::string_view>()(out, in);
}
};

// Conversion from null terminated char const*
template <class Out>
struct LexicalCast<Out, char const*>
Expand All @@ -126,7 +159,8 @@ struct LexicalCast<Out, char const*>
bool
operator()(Out& out, char const* in) const
{
return LexicalCast<Out, std::string>()(out, in);
assert(in);
return LexicalCast<Out, std::string_view>()(out, in);
}
};

Expand All @@ -140,7 +174,8 @@ struct LexicalCast<Out, char*>
bool
operator()(Out& out, char* in) const
{
return LexicalCast<Out, std::string>()(out, in);
assert(in);
return LexicalCast<Out, std::string_view>()(out, in);
}
};

Expand Down
1 change: 1 addition & 0 deletions src/ripple/json/Output.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <boost/beast/core/string.hpp>
#include <functional>
#include <string>

namespace Json {

Expand Down
5 changes: 2 additions & 3 deletions src/ripple/overlay/impl/Cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
//==============================================================================

#include <ripple/app/main/Application.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/core/Config.h>
Expand All @@ -27,7 +26,6 @@
#include <ripple/protocol/jss.h>
#include <ripple/protocol/tokens.h>
#include <boost/regex.hpp>
#include <memory.h>

namespace ripple {

Expand Down Expand Up @@ -113,7 +111,8 @@ Cluster::load(Section const& nodes)
return false;
}

auto const id = parseBase58<PublicKey>(TokenType::NodePublic, match[1]);
auto const id =
parseBase58<PublicKey>(TokenType::NodePublic, match[1].str());

if (!id)
{
Expand Down
13 changes: 4 additions & 9 deletions src/ripple/overlay/impl/Handshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@
#include <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/main/Application.h>
#include <ripple/basics/base64.h>
#include <ripple/basics/safe_cast.h>
#include <ripple/beast/core/LexicalCast.h>
#include <ripple/beast/rfc2616.h>
#include <ripple/overlay/impl/Handshake.h>
#include <ripple/protocol/digest.h>

#include <boost/regex.hpp>

#include <algorithm>
#include <chrono>

// VFALCO Shouldn't we have to include the OpenSSL
// headers or something for SSL_get_finished?
Expand All @@ -46,8 +42,8 @@ getFeatureValue(
return {};
boost::smatch match;
boost::regex rx(feature + "=([^;\\s]+)");
std::string const value = header->value();
if (boost::regex_search(value, match, rx))
std::string const allFeatures(header->value());
if (boost::regex_search(allFeatures, match, rx))
return {match[1]};
return {};
}
Expand Down Expand Up @@ -243,7 +239,7 @@ verifyHandshake(
{
std::uint32_t nid;

if (!beast::lexicalCastChecked(nid, std::string(iter->value())))
if (!beast::lexicalCastChecked(nid, iter->value()))
throw std::runtime_error("Invalid peer network identifier");

if (networkID && nid != *networkID)
Expand All @@ -252,8 +248,7 @@ verifyHandshake(

if (auto const iter = headers.find("Network-Time"); iter != headers.end())
{
auto const netTime =
[str = std::string(iter->value())]() -> TimeKeeper::time_point {
auto const netTime = [str = iter->value()]() -> TimeKeeper::time_point {
TimeKeeper::duration::rep val;

if (beast::lexicalCastChecked(val, str))
Expand Down
7 changes: 3 additions & 4 deletions src/ripple/overlay/impl/OverlayImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include <ripple/server/SimpleWriter.h>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/utility/in_place_factory.hpp>

namespace ripple {

Expand Down Expand Up @@ -826,7 +825,7 @@ OverlayImpl::getOverlayInfo()
auto version{sp->getVersion()};
if (!version.empty())
// Could move here if Json::value supported moving from strings
pv[jss::version] = version;
pv[jss::version] = std::string{version};
}

std::uint32_t minSeq, maxSeq;
Expand Down Expand Up @@ -994,9 +993,9 @@ OverlayImpl::processValidatorList(
return true;
};

auto key = req.target().substr(prefix.size());
std::string_view key = req.target().substr(prefix.size());

if (auto slash = key.find('/'); slash != boost::string_view::npos)
if (auto slash = key.find('/'); slash != std::string_view::npos)
{
auto verString = key.substr(0, slash);
if (!boost::conversion::try_lexical_convert(verString, version))
Expand Down
10 changes: 4 additions & 6 deletions src/ripple/overlay/impl/PeerImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@
#include <ripple/basics/random.h>
#include <ripple/basics/safe_cast.h>
#include <ripple/beast/core/LexicalCast.h>
#include <ripple/beast/core/SemanticVersion.h>
#include <ripple/nodestore/DatabaseShard.h>
#include <ripple/overlay/Cluster.h>
#include <ripple/overlay/impl/PeerImp.h>
#include <ripple/overlay/impl/Tuning.h>
#include <ripple/overlay/predicates.h>
#include <ripple/protocol/digest.h>

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/beast/core/ostream.hpp>

Expand Down Expand Up @@ -160,7 +158,7 @@ PeerImp::run()
return post(strand_, std::bind(&PeerImp::run, shared_from_this()));

auto parseLedgerHash =
[](std::string const& value) -> std::optional<uint256> {
[](std::string_view value) -> std::optional<uint256> {
if (uint256 ret; ret.parseHex(value))
return ret;

Expand Down Expand Up @@ -397,15 +395,15 @@ PeerImp::json()
}

if (auto const d = domain(); !d.empty())
ret[jss::server_domain] = domain();
ret[jss::server_domain] = std::string{d};

if (auto const nid = headers_["Network-ID"]; !nid.empty())
ret[jss::network_id] = std::string(nid);
ret[jss::network_id] = std::string{nid};

ret[jss::load] = usage_.balance();

if (auto const version = getVersion(); !version.empty())
ret[jss::version] = version;
ret[jss::version] = std::string{version};

ret[jss::protocol] = to_string(protocol_);

Expand Down
2 changes: 1 addition & 1 deletion src/ripple/resource/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Manager : public beast::PropertyStream::Source
newInboundEndpoint(
beast::IP::Endpoint const& address,
bool const proxy,
boost::string_view const& forwardedFor) = 0;
std::string_view forwardedFor) = 0;

/** Create a new endpoint keyed by outbound IP address and port. */
virtual Consumer
Expand Down
Loading

0 comments on commit dd4b5d5

Please sign in to comment.