Remove the use of beast::String from rippled (RIPD-443)

This commit is contained in:
Nik Bougalis
2014-09-28 18:38:11 -07:00
committed by Vinnie Falco
parent 4241dbb600
commit c0b69e8ef7
45 changed files with 285 additions and 342 deletions

View File

@@ -216,7 +216,7 @@ public:
// Returns `true` if a Source with the same unique ID already exists
//
bool findSourceByID (beast::String id)
bool findSourceByID (std::string id)
{
for (SourceTable::const_iterator iter (m_sources.begin());
iter != m_sources.end(); ++iter)
@@ -364,7 +364,7 @@ public:
m_journal.debug <<
"Rebuilt chosen list with " <<
beast::String::fromNumber (m_chosenList->size()) << " entries";
std::to_string (m_chosenList->size()) << " entries";
}
/** Mark the Chosen List for a rebuild. */

View File

@@ -198,25 +198,15 @@ public:
//
//--------------------------------------------------------------------------
void addStrings (beast::String name, std::vector <std::string> const& strings)
void addStrings (std::string const& name, std::vector <std::string> const& strings)
{
beast::StringArray stringArray;
stringArray.ensureStorageAllocated (strings.size());
for (std::size_t i = 0; i < strings.size(); ++i)
stringArray.add (strings [i]);
addStrings (name, stringArray);
}
void addStrings (beast::String name, beast::StringArray const& stringArray)
{
if (stringArray.size() > 0)
{
addStaticSource (SourceStrings::New (name, stringArray));
}
else
if (strings.empty ())
{
m_journal.debug << "Static source '" << name << "' is empty.";
return;
}
addStaticSource (SourceStrings::New (name, strings));
}
void addFile (beast::File const& file)

View File

@@ -45,14 +45,14 @@ public:
return ss.str();
}
beast::String uniqueID () const
std::string uniqueID () const
{
return "File," + m_file.getFullPathName ();
return "File," + m_file.getFullPathName ().toStdString();
}
beast::String createParam ()
std::string createParam ()
{
return m_file.getFullPathName ();
return m_file.getFullPathName ().toStdString();
}
void fetch (Results& results, beast::Journal journal)

View File

@@ -25,8 +25,7 @@ class SourceStringsImp
, public beast::LeakChecked <SourceStringsImp>
{
public:
SourceStringsImp (
beast::String name, beast::StringArray const& strings)
SourceStringsImp (std::string const& name, std::vector <std::string> const& strings)
: m_name (name)
, m_strings (strings)
{
@@ -38,18 +37,18 @@ public:
std::string to_string () const
{
return m_name.toStdString();
return m_name;
}
beast::String uniqueID () const
std::string uniqueID () const
{
// VFALCO TODO This can't be right...?
return beast::String::empty;
return std::string{};
}
beast::String createParam ()
std::string createParam ()
{
return beast::String::empty;
return std::string{};
}
void fetch (Results& results, beast::Journal journal)
@@ -57,10 +56,7 @@ public:
results.list.reserve (m_strings.size ());
for (int i = 0; i < m_strings.size (); ++i)
{
std::string const s (m_strings [i].toStdString ());
Utilities::parseResultLine (results, s);
}
Utilities::parseResultLine (results, m_strings [i]);
results.success = results.list.size () > 0;
results.expirationTime = beast::Time::getCurrentTime () +
@@ -68,14 +64,14 @@ public:
}
private:
beast::String m_name;
beast::StringArray m_strings;
std::string m_name;
std::vector <std::string> m_strings;
};
//------------------------------------------------------------------------------
SourceStrings* SourceStrings::New (
beast::String name, beast::StringArray const& strings)
std::string const& name, std::vector <std::string> const& strings)
{
return new SourceStringsImp (name, strings);
}

View File

@@ -30,7 +30,7 @@ class SourceStrings : public Source
{
public:
static SourceStrings* New (
beast::String name, beast::StringArray const& strings);
std::string const& name, std::vector <std::string> const& strings);
};
}

View File

@@ -46,14 +46,14 @@ public:
return ss.str();
}
beast::String uniqueID () const
std::string uniqueID () const
{
return "URL," + m_url.toString();
return "URL," + m_url.to_string();
}
beast::String createParam ()
std::string createParam ()
{
return m_url.toString();
return m_url.to_string();
}
void cancel ()

View File

@@ -17,6 +17,8 @@
*/
//==============================================================================
#include <boost/regex.hpp>
namespace ripple {
namespace Validators {
@@ -65,10 +67,10 @@ void StoreSqdb::insert (SourceDesc& desc)
{
beast::Error error;
beast::String const sourceID (desc.source->uniqueID().toStdString());
beast::String const createParam (desc.source->createParam().toStdString());
beast::String const lastFetchTime (Utilities::timeToString (desc.lastFetchTime));
beast::String const expirationTime (Utilities::timeToString (desc.expirationTime));
auto const sourceID (desc.source->uniqueID());
auto const createParam (desc.source->createParam());
auto const lastFetchTime (timeToString (desc.lastFetchTime));
auto const expirationTime (timeToString (desc.expirationTime));
beast::sqdb::statement st = (m_session.prepare <<
"INSERT INTO Validators_Source ( "
@@ -101,14 +103,80 @@ void StoreSqdb::insert (SourceDesc& desc)
}
//--------------------------------------------------------------------------
std::string StoreSqdb::itos (int i, std::size_t width)
{
auto s = std::to_string (i);
if (s.length () < width)
s = std::string (width - s.length(), '0').append (s);
return s;
}
beast::Time StoreSqdb::stringToTime (std::string const& s)
{
static boost::regex const date_pattern (
"^" // the beginning of the string
"(19[789][0-9]|[2-9][0-9][0-9][0-9])-" // 1970-9999 followed by -
"(0[0-9]|1[01])-" // 0-11 followed by -
"(0[1-9]|[12][0-9]|3[01]) " // 1-31 followed by space
"([01][0-9]|2[0-3]):" // 0-23 followed by :
"([0-5][0-9]):" // 0-59 followed by :
"([0-5][0-9])" // 0-59
"$",
boost::regex_constants::optimize);
boost::smatch match;
if (boost::regex_match (s, match, date_pattern))
{
int const year = beast::lexicalCast<int> (std::string (match[1]), -1);
int const mon = beast::lexicalCast<int> (std::string (match[2]), -1);
int const day = beast::lexicalCast<int> (std::string (match[3]), -1);
int const hour = beast::lexicalCast<int> (std::string (match[4]), -1);
int const min = beast::lexicalCast<int> (std::string (match[5]), -1);
int const sec = beast::lexicalCast<int> (std::string (match[6]), -1);
if (year != -1 &&
mon != -1 &&
day != -1 &&
hour != -1 &&
min != -1 &&
sec != -1)
{
// local time
return beast::Time (year, mon, day, hour, min, sec, 0, true);
}
}
return beast::Time (0);
}
std::string StoreSqdb::timeToString (beast::Time const& t)
{
std::string ret;
if (t.isNotNull ())
{
ret = itos (t.getYear(), 4) + "-" +
itos (t.getMonth(), 2) + "-" +
itos (t.getDayOfMonth (), 2) + " " +
itos (t.getHours () , 2) + ":" +
itos (t.getMinutes (), 2) + ":" +
itos (t.getSeconds(), 2);
}
return ret;
}
//--------------------------------------------------------------------------
void StoreSqdb::update (SourceDesc& desc, bool updateFetchResults)
{
beast::Error error;
beast::String const sourceID (desc.source->uniqueID());
beast::String const lastFetchTime (Utilities::timeToString (desc.lastFetchTime));
beast::String const expirationTime (Utilities::timeToString (desc.expirationTime));
std::string const sourceID (desc.source->uniqueID());
std::string const lastFetchTime (timeToString (desc.lastFetchTime));
std::string const expirationTime (timeToString (desc.expirationTime));
beast::sqdb::transaction tr (m_session);
@@ -136,7 +204,7 @@ void StoreSqdb::update (SourceDesc& desc, bool updateFetchResults)
if (! error)
{
std::string publicKeyString;
beast::String label;
std::string label;
beast::sqdb::statement st = (m_session.prepare <<
"INSERT INTO Validators_SourceItem ( "
@@ -197,9 +265,10 @@ bool StoreSqdb::select (SourceDesc& desc)
beast::Error error;
beast::String const sourceID (desc.source->uniqueID());
beast::String lastFetchTime;
beast::String expirationTime;
std::string const sourceID (desc.source->uniqueID());
std::string lastFetchTime;
std::string expirationTime;
beast::sqdb::statement st = (m_session.prepare <<
"SELECT "
" lastFetchTime, "
@@ -217,8 +286,8 @@ bool StoreSqdb::select (SourceDesc& desc)
"Found record for " << *desc.source;
found = true;
desc.lastFetchTime = Utilities::stringToTime (lastFetchTime);
desc.expirationTime = Utilities::stringToTime (expirationTime);
desc.lastFetchTime = stringToTime (lastFetchTime);
desc.expirationTime = stringToTime (expirationTime);
}
else if (! error)
{
@@ -243,7 +312,7 @@ void StoreSqdb::selectList (SourceDesc& desc)
{
beast::Error error;
beast::String const sourceID (desc.source->uniqueID());
std::string const sourceID (desc.source->uniqueID());
// Get the count
std::size_t count;

View File

@@ -62,6 +62,12 @@ private:
beast::Journal m_journal;
beast::sqdb::session m_session;
// DEPRECATED
static std::string itos (int i, std::size_t fieldSize = 0);
static std::string timeToString (beast::Time const& t);
static beast::Time stringToTime (std::string const& s);
};
}

View File

@@ -35,7 +35,7 @@ public:
struct TestSource : Source
{
TestSource (beast::String const& name, std::uint32_t start, std::uint32_t end)
TestSource (std::string const& name, std::uint32_t start, std::uint32_t end)
: m_name (name)
, m_start (start)
, m_end (end)
@@ -44,38 +44,37 @@ public:
std::string to_string () const
{
return uniqueID().toStdString();
return uniqueID();
}
beast::String uniqueID () const
std::string uniqueID () const
{
using beast::String;
return String ("Test,") + m_name + "," +
String::fromNumber (m_start) + "," +
String::fromNumber (m_end);
return "Test," + m_name + "," +
std::to_string (m_start) + "," +
std::to_string (m_end);
}
beast::String createParam ()
std::string createParam ()
{
return beast::String::empty;
return std::string{};
}
void fetch (Results& results, beast::Journal)
{
results.success = true;
results.message = beast::String::empty;
results.message = std::string{};
results.list.reserve (numberOfTestValidators);
for (std::uint32_t i = m_start ; i < m_end; ++i)
{
Item item;;
Item item;
item.publicKey = RipplePublicKey::createFromInteger (i);
item.label = beast::String::fromNumber (i);
item.label = std::to_string (i);
results.list.push_back (item);
}
}
beast::String m_name;
std::string m_name;
std::size_t m_start;
std::size_t m_end;
};
@@ -113,7 +112,7 @@ public:
beast::Random r;
for (int i = 1; i <= numberofTestSources; ++i)
{
beast::String const name (beast::String::fromNumber (i));
std::string const name (std::to_string (i));
std::uint32_t const start = r.nextInt (numberOfTestValidators);
std::uint32_t const end = start + r.nextInt (numberOfTestValidators);
logic.add (new TestSource (name, start, end));

View File

@@ -136,74 +136,5 @@ void Utilities::parseResultLine (
}
}
//--------------------------------------------------------------------------
beast::String Utilities::itos (int i, int fieldSize)
{
return beast::String::fromNumber (i).paddedLeft (beast::beast_wchar('0'), fieldSize);
}
beast::String Utilities::timeToString (beast::Time const& t)
{
if (t.isNotNull ())
{
return
itos (t.getYear(), 4) + "-" +
itos (t.getMonth(), 2) + "-" +
itos (t.getDayOfMonth (), 2) + " " +
itos (t.getHours () , 2) + ":" +
itos (t.getMinutes (), 2) + ":" +
itos (t.getSeconds(), 2);
}
return beast::String::empty;
}
int Utilities::stoi (beast::String& s, int fieldSize, int minValue, int maxValue,
beast::beast_wchar delimiter)
{
int const needed (fieldSize + ((delimiter != 0) ? 1 : 0));
beast::String const v (s.substring (0, needed));
s = s.substring (v.length ());
if (s.length() == needed)
{
int const v (s.getIntValue());
if (s.startsWith (itos (v, fieldSize)) &&
v >= minValue && v <= maxValue &&
(delimiter == 0 || s.endsWithChar (delimiter)))
{
return v;
}
}
return -1; // fail
}
beast::Time Utilities::stringToTime (beast::String s)
{
if (s.isNotEmpty ())
{
int const year (stoi (s, 4, 1970, 9999, '-'));
int const mon (stoi (s, 2, 0, 11, '-'));
int const day (stoi (s, 2, 1, 31, ' '));
int const hour (stoi (s, 2, 0, 23, ':'));
int const min (stoi (s, 2, 0, 59, ':'));
int const sec (stoi (s, 2, 0, 59, 0 ));
if (year != -1 &&
mon != -1 &&
day != -1 &&
hour != -1 &&
min != -1 &&
sec != -1)
{
// local time
return beast::Time (year, mon, day, hour, min, sec, 0, true);
}
}
return beast::Time (0);
}
//------------------------------------------------------------------------------
}
}

View File

@@ -121,15 +121,6 @@ public:
std::string const& line,
beast::Journal journal = beast::Journal());
// helpers
static beast::String itos (int i, int fieldSize = 0);
static int stoi (beast::String& s, int fieldSize, int minValue, int maxValue,
beast::beast_wchar delimiter);
// conversion betwen Time and String
static beast::String timeToString (beast::Time const& t);
static beast::Time stringToTime (beast::String s);
struct Helpers;
/** Parse a string into a Source::Item.