2012-09-08 02:50:37 +00:00
|
|
|
#include <fc/string.hpp>
|
|
|
|
|
#include <fc/utility.hpp>
|
2012-12-03 19:51:31 +00:00
|
|
|
#include <fc/fwd_impl.hpp>
|
2014-06-05 15:31:42 +00:00
|
|
|
#include <fc/exception/exception.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2013-12-25 01:22:21 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2012-09-08 02:50:37 +00:00
|
|
|
|
|
|
|
|
#include <string>
|
2014-08-13 20:06:42 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <iomanip>
|
2014-06-03 15:09:15 +00:00
|
|
|
#include <locale>
|
2012-09-08 02:50:37 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implemented with std::string for now.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace fc {
|
2014-06-03 15:09:15 +00:00
|
|
|
class comma_numpunct : public std::numpunct<char>
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
virtual char do_thousands_sep() const { return ','; }
|
|
|
|
|
virtual std::string do_grouping() const { return "\03"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::string to_pretty_string( int64_t value )
|
|
|
|
|
{
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
ss.imbue( {std::locale(), new comma_numpunct} );
|
|
|
|
|
ss << std::fixed << value;
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 18:19:08 +00:00
|
|
|
#ifdef USE_FC_STRING
|
2012-12-03 19:51:31 +00:00
|
|
|
string::string(const char* s, int l) :my(s,l){ }
|
|
|
|
|
string::string(){}
|
2012-12-18 19:37:14 +00:00
|
|
|
string::string( const fc::string& c ):my(*c.my) { }
|
|
|
|
|
string::string( string&& m ):my(fc::move(*m.my)) {}
|
2012-12-03 19:51:31 +00:00
|
|
|
string::string( const char* c ):my(c){}
|
|
|
|
|
string::string( const_iterator b, const_iterator e ):my(b,e){}
|
|
|
|
|
string::string( const std::string& s ):my(s) {}
|
|
|
|
|
string::string( std::string&& s ):my(fc::move(s)) {}
|
|
|
|
|
string::~string() { }
|
|
|
|
|
string::operator std::string&() { return *my; }
|
|
|
|
|
string::operator const std::string&()const { return *my; }
|
2013-06-05 19:19:00 +00:00
|
|
|
char* string::data() { return &my->front(); }
|
2012-09-08 02:50:37 +00:00
|
|
|
|
2012-09-10 05:48:08 +00:00
|
|
|
string::iterator string::begin() { return &(*this)[0]; }
|
|
|
|
|
string::iterator string::end() { return &(*this)[size()]; }
|
2012-12-03 19:51:31 +00:00
|
|
|
string::const_iterator string::begin()const { return my->c_str(); }
|
|
|
|
|
string::const_iterator string::end()const { return my->c_str() + my->size(); }
|
2012-09-08 02:50:37 +00:00
|
|
|
|
2012-12-18 19:37:14 +00:00
|
|
|
char& string::operator[](size_t idx) { return (*my)[idx]; }
|
|
|
|
|
const char& string::operator[](size_t idx)const { return (*my)[idx]; }
|
2012-09-08 02:50:37 +00:00
|
|
|
|
2012-12-18 19:37:14 +00:00
|
|
|
void string::reserve(size_t r) { my->reserve(r); }
|
|
|
|
|
size_t string::size()const { return my->size(); }
|
|
|
|
|
size_t string::find(char c, size_t p)const { return my->find(c,p); }
|
2013-06-05 19:19:00 +00:00
|
|
|
size_t string::find(const fc::string& str, size_t pos /* = 0 */) const { return my->find(str, pos); }
|
|
|
|
|
size_t string::find(const char* s, size_t pos /* = 0 */) const { return my->find(s,pos); }
|
2012-12-19 17:23:12 +00:00
|
|
|
size_t string::rfind(char c, size_t p)const { return my->rfind(c,p); }
|
2012-12-31 16:06:10 +00:00
|
|
|
size_t string::rfind(const char* c, size_t p)const { return my->rfind(c,p); }
|
2012-12-19 17:23:12 +00:00
|
|
|
size_t string::rfind(const fc::string& c, size_t p)const { return my->rfind(c,p); }
|
2013-06-05 19:19:00 +00:00
|
|
|
size_t string::find_first_of(const fc::string& str, size_t pos /* = 0 */) const { return my->find_first_of(str, pos); }
|
|
|
|
|
size_t string::find_first_of(const char* s, size_t pos /* = 0 */) const { return my->find_first_of(s, pos); }
|
|
|
|
|
|
|
|
|
|
fc::string& string::replace(size_t pos, size_t len, const string& str) { my->replace(pos, len, str); return *this; }
|
|
|
|
|
fc::string& string::replace(size_t pos, size_t len, const char* s) { my->replace(pos, len, s); return *this; }
|
|
|
|
|
|
2012-12-03 19:51:31 +00:00
|
|
|
void string::clear() { my->clear(); }
|
2012-12-18 19:37:14 +00:00
|
|
|
void string::resize( size_t s ) { my->resize(s); }
|
2012-09-08 02:50:37 +00:00
|
|
|
|
2012-12-18 19:37:14 +00:00
|
|
|
fc::string string::substr( size_t start, size_t len )const { return my->substr(start,len); }
|
2012-12-03 19:51:31 +00:00
|
|
|
const char* string::c_str()const { return my->c_str(); }
|
2012-10-10 01:40:29 +00:00
|
|
|
|
2012-12-03 19:51:31 +00:00
|
|
|
bool string::operator == ( const char* s )const { return *my == s; }
|
|
|
|
|
bool string::operator == ( const string& s )const { return *my == *s.my; }
|
|
|
|
|
bool string::operator != ( const string& s )const { return *my != *s.my; }
|
2012-09-08 02:50:37 +00:00
|
|
|
|
2012-12-03 19:51:31 +00:00
|
|
|
string& string::operator =( const string& c ) { *my = *c.my; return *this; }
|
|
|
|
|
string& string::operator =( string&& c ) { *my = fc::move( *c.my ); return *this; }
|
2012-09-08 02:50:37 +00:00
|
|
|
|
2012-12-03 19:51:31 +00:00
|
|
|
string& string::operator+=( const string& s ) { *my += *s.my; return *this; }
|
|
|
|
|
string& string::operator+=( char c ) { *my += c; return *this; }
|
2012-09-08 02:50:37 +00:00
|
|
|
|
2012-12-03 19:51:31 +00:00
|
|
|
bool operator < ( const string& a, const string& b ) { return *a.my < *b.my; }
|
|
|
|
|
string operator + ( const string& s, const string& c ) { return string(s) += c; }
|
|
|
|
|
string operator + ( const string& s, char c ) { return string(s) += c; }
|
2013-06-27 18:19:08 +00:00
|
|
|
#endif // USE_FC_STRING
|
2012-09-08 02:50:37 +00:00
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
int64_t to_int64( const fc::string& i )
|
2014-06-05 15:31:42 +00:00
|
|
|
{ try {
|
2013-06-05 19:19:00 +00:00
|
|
|
return boost::lexical_cast<int64_t>(i.c_str());
|
2014-06-05 15:31:42 +00:00
|
|
|
} FC_RETHROW_EXCEPTIONS( warn, "${i} => int64_t", ("i",i) ) }
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
uint64_t to_uint64( const fc::string& i )
|
2014-06-05 15:31:42 +00:00
|
|
|
{ try {
|
2013-06-05 19:19:00 +00:00
|
|
|
return boost::lexical_cast<uint64_t>(i.c_str());
|
2014-06-05 15:31:42 +00:00
|
|
|
} FC_RETHROW_EXCEPTIONS( warn, "${i} => uint64_t", ("i",i) ) }
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
double to_double( const fc::string& i)
|
2014-06-05 15:31:42 +00:00
|
|
|
{ try {
|
2013-06-05 19:19:00 +00:00
|
|
|
return boost::lexical_cast<double>(i.c_str());
|
2014-06-05 15:31:42 +00:00
|
|
|
} FC_RETHROW_EXCEPTIONS( warn, "${i} => double", ("i",i) ) }
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
fc::string to_string( double d)
|
|
|
|
|
{
|
2014-08-13 20:06:42 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << std::setprecision(12) << std::fixed << d;
|
|
|
|
|
return ss.str(); //boost::lexical_cast<std::string>(d);
|
2013-06-05 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fc::string to_string( uint64_t d)
|
|
|
|
|
{
|
|
|
|
|
return boost::lexical_cast<std::string>(d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fc::string to_string( int64_t d)
|
|
|
|
|
{
|
|
|
|
|
return boost::lexical_cast<std::string>(d);
|
|
|
|
|
}
|
2014-03-11 15:29:29 +00:00
|
|
|
fc::string to_string( uint16_t d)
|
|
|
|
|
{
|
|
|
|
|
return boost::lexical_cast<std::string>(d);
|
|
|
|
|
}
|
2013-12-25 01:22:21 +00:00
|
|
|
std::string trim( const std::string& s )
|
|
|
|
|
{
|
2014-03-27 05:09:08 +00:00
|
|
|
return boost::algorithm::trim_copy(s);
|
|
|
|
|
/*
|
2013-12-25 01:22:21 +00:00
|
|
|
std::string cpy(s);
|
|
|
|
|
boost::algorithm::trim(cpy);
|
|
|
|
|
return cpy;
|
2014-03-27 05:09:08 +00:00
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
std::string to_lower( const std::string& s )
|
|
|
|
|
{
|
|
|
|
|
auto tmp = s;
|
|
|
|
|
boost::algorithm::to_lower(tmp);
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
string trim_and_normalize_spaces( const string& s )
|
|
|
|
|
{
|
|
|
|
|
string result = boost::algorithm::trim_copy( s );
|
|
|
|
|
while( result.find( " " ) != result.npos )
|
|
|
|
|
boost::algorithm::replace_all( result, " ", " " );
|
|
|
|
|
return result;
|
2013-12-25 01:22:21 +00:00
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2012-09-08 02:50:37 +00:00
|
|
|
} // namespace fc
|
|
|
|
|
|
|
|
|
|
|