Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
name.cpp
Go to the documentation of this file.
2#include <fc/variant.hpp>
3#include <boost/algorithm/string.hpp>
6
7namespace sysio::chain {
8
9 void name::set( std::string_view str ) {
10 const auto len = str.size();
11 SYS_ASSERT(len <= 13, name_type_exception, "Name is longer than 13 characters (${name}) ", ("name", std::string(str)));
12 value = string_to_uint64_t(str);
13 SYS_ASSERT(to_string() == str, name_type_exception,
14 "Name not properly normalized (name: ${name}, normalized: ${normalized}) ",
15 ("name", std::string(str))("normalized", to_string()));
16 }
17
18 // keep in sync with name::to_string() in contract definition for name
19 std::string name::to_string()const {
20 static const char* charmap = ".12345abcdefghijklmnopqrstuvwxyz";
21
22 std::string str(13,'.');
23
24 uint64_t tmp = value;
25 for( uint32_t i = 0; i <= 12; ++i ) {
26 char c = charmap[tmp & (i == 0 ? 0x0f : 0x1f)];
27 str[12-i] = c;
28 tmp >>= (i == 0 ? 4 : 5);
29 }
30
31 boost::algorithm::trim_right_if( str, []( char c ){ return c == '.'; } );
32 return str;
33 }
34
35} // sysio::chain
36
37namespace fc {
38 void to_variant(const sysio::chain::name& c, fc::variant& v) { v = c.to_string(); }
39 void from_variant(const fc::variant& v, sysio::chain::name& check) { check.set( v.get_string() ); }
40} // fc
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
const string & get_string() const
Definition variant.cpp:606
Defines exception's used by fc.
return str
Definition CLI11.hpp:1359
namespace sysio::chain
Definition authority.cpp:3
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
constexpr uint64_t string_to_uint64_t(std::string_view str)
Definition name.hpp:24
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
Immutable except for fc::from_variant.
Definition name.hpp:43
std::string to_string() const
Definition name.cpp:19
size_t len