Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
string.cpp
Go to the documentation of this file.
1#include <fc/string.hpp>
2#include <fc/utility.hpp>
3#include <fc/fwd_impl.hpp>
5#include <boost/lexical_cast.hpp>
6#include <boost/algorithm/string.hpp>
7
8#include <string>
9#include <sstream>
10#include <iomanip>
11#include <locale>
12#include <limits>
13
18namespace fc {
19 class comma_numpunct : public std::numpunct<char>
20 {
21 protected:
22 virtual char do_thousands_sep() const { return ','; }
23 virtual std::string do_grouping() const { return "\03"; }
24 };
25
27 {
28 std::stringstream ss;
29 ss.imbue( {std::locale(), new comma_numpunct} );
30 ss << std::fixed << value;
31 return ss.str();
32 }
33
34#ifdef USE_FC_STRING
35 string::string(const char* s, int l) :my(s,l){ }
36 string::string(){}
37 string::string( const fc::string& c ):my(*c.my) { }
38 string::string( string&& m ):my(fc::move(*m.my)) {}
39 string::string( const char* c ):my(c){}
40 string::string( const_iterator b, const_iterator e ):my(b,e){}
41 string::string( const std::string& s ):my(s) {}
42 string::string( std::string&& s ):my(fc::move(s)) {}
43 string::~string() { }
44 string::operator std::string&() { return *my; }
45 string::operator const std::string&()const { return *my; }
46 char* string::data() { return &my->front(); }
47
48 string::iterator string::begin() { return &(*this)[0]; }
49 string::iterator string::end() { return &(*this)[size()]; }
50 string::const_iterator string::begin()const { return my->c_str(); }
51 string::const_iterator string::end()const { return my->c_str() + my->size(); }
52
53 char& string::operator[](size_t idx) { return (*my)[idx]; }
54 const char& string::operator[](size_t idx)const { return (*my)[idx]; }
55
56 void string::reserve(size_t r) { my->reserve(r); }
57 size_t string::size()const { return my->size(); }
58 size_t string::find(char c, size_t p)const { return my->find(c,p); }
59 size_t string::find(const fc::string& str, size_t pos /* = 0 */) const { return my->find(str, pos); }
60 size_t string::find(const char* s, size_t pos /* = 0 */) const { return my->find(s,pos); }
61 size_t string::rfind(char c, size_t p)const { return my->rfind(c,p); }
62 size_t string::rfind(const char* c, size_t p)const { return my->rfind(c,p); }
63 size_t string::rfind(const fc::string& c, size_t p)const { return my->rfind(c,p); }
64 size_t string::find_first_of(const fc::string& str, size_t pos /* = 0 */) const { return my->find_first_of(str, pos); }
65 size_t string::find_first_of(const char* s, size_t pos /* = 0 */) const { return my->find_first_of(s, pos); }
66
67 fc::string& string::replace(size_t pos, size_t len, const string& str) { my->replace(pos, len, str); return *this; }
68 fc::string& string::replace(size_t pos, size_t len, const char* s) { my->replace(pos, len, s); return *this; }
69
70 void string::clear() { my->clear(); }
71 void string::resize( size_t s ) { my->resize(s); }
72
73 fc::string string::substr( size_t start, size_t len )const { return my->substr(start,len); }
74 const char* string::c_str()const { return my->c_str(); }
75
76 bool string::operator == ( const char* s )const { return *my == s; }
77 bool string::operator == ( const string& s )const { return *my == *s.my; }
78 bool string::operator != ( const string& s )const { return *my != *s.my; }
79
80 string& string::operator =( const string& c ) { *my = *c.my; return *this; }
81 string& string::operator =( string&& c ) { *my = fc::move( *c.my ); return *this; }
82
83 string& string::operator+=( const string& s ) { *my += *s.my; return *this; }
84 string& string::operator+=( char c ) { *my += c; return *this; }
85
86 bool operator < ( const string& a, const string& b ) { return *a.my < *b.my; }
87 string operator + ( const string& s, const string& c ) { return string(s) += c; }
88 string operator + ( const string& s, char c ) { return string(s) += c; }
89#endif // USE_FC_STRING
90
91
93 {
94 try
95 {
96 return boost::lexical_cast<int64_t>(i.c_str(), i.size());
97 }
98 catch( const boost::bad_lexical_cast& e )
99 {
100 FC_THROW_EXCEPTION( parse_error_exception, "Couldn't parse int64_t" );
101 }
102 FC_RETHROW_EXCEPTIONS( warn, "${i} => int64_t", ("i",i) )
103 }
104
106 { try {
107 try
108 {
109 return boost::lexical_cast<uint64_t>(i.c_str(), i.size());
110 }
111 catch( const boost::bad_lexical_cast& e )
112 {
113 FC_THROW_EXCEPTION( parse_error_exception, "Couldn't parse uint64_t" );
114 }
115 FC_RETHROW_EXCEPTIONS( warn, "${i} => uint64_t", ("i",i) )
116 } FC_CAPTURE_AND_RETHROW( (i) ) }
117
118 double to_double( const fc::string& i)
119 {
120 try
121 {
122 return boost::lexical_cast<double>(i.c_str(), i.size());
123 }
124 catch( const boost::bad_lexical_cast& e )
125 {
126 FC_THROW_EXCEPTION( parse_error_exception, "Couldn't parse double" );
127 }
128 FC_RETHROW_EXCEPTIONS( warn, "${i} => double", ("i",i) )
129 }
130
131 fc::string to_string(double d)
132 {
133 // +2 is required to ensure that the double is rounded correctly when read back in. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
134 std::stringstream ss;
135 ss << std::setprecision(std::numeric_limits<double>::digits10 + 2) << std::fixed << d;
136 return ss.str();
137 }
138
139 fc::string to_string( uint64_t d)
140 {
141 return boost::lexical_cast<std::string>(d);
142 }
143
144 fc::string to_string( int64_t d)
145 {
146 return boost::lexical_cast<std::string>(d);
147 }
148 fc::string to_string( uint16_t d)
149 {
150 return boost::lexical_cast<std::string>(d);
151 }
152 std::string trim( const std::string& s )
153 {
154 return boost::algorithm::trim_copy(s);
155 /*
156 std::string cpy(s);
157 boost::algorithm::trim(cpy);
158 return cpy;
159 */
160 }
161 std::string to_lower( const std::string& s )
162 {
163 auto tmp = s;
164 boost::algorithm::to_lower(tmp);
165 return tmp;
166 }
167 string trim_and_normalize_spaces( const string& s )
168 {
169 string result = boost::algorithm::trim_copy( s );
170 while( result.find( " " ) != result.npos )
171 boost::algorithm::replace_all( result, " ", " " );
172 return result;
173 }
174
175} // namespace fc
176
177
bool operator<(const CBigNum &a, const CBigNum &b)
Definition base58.cpp:498
const CBigNum operator+(const CBigNum &a, const CBigNum &b)
Definition base58.cpp:429
const mie::Vuint & p
Definition bn.cpp:27
const mie::Vuint & r
Definition bn.cpp:28
virtual char do_thousands_sep() const
Definition string.cpp:22
virtual std::string do_grouping() const
Definition string.cpp:23
Defines exception's used by fc.
#define FC_CAPTURE_AND_RETHROW(...)
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
std::string to_pretty_string(int64_t)
Definition string.cpp:26
string trim_and_normalize_spaces(const string &s)
Definition string.cpp:167
double to_double(const fc::string &)
Definition string.cpp:118
uint64_t to_uint64(const fc::string &)
Definition string.cpp:105
fc::string to_lower(const fc::string &)
Definition string.cpp:161
fc::string trim(const fc::string &)
Definition string.cpp:152
int64_t to_int64(const fc::string &)
Definition string.cpp:92
#define value
Definition pkcs11.h:157
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
unsigned short uint16_t
Definition stdint.h:125
signed __int64 int64_t
Definition stdint.h:135
unsigned __int64 uint64_t
Definition stdint.h:136
CK_ULONG d
char * s
size_t len
int l