Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

move is_string_valid_name to cpp file #9445

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 1 addition & 25 deletions libraries/chain/include/eosio/chain/name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,7 @@ namespace eosio::chain {
}

// true if std::string can be converted to name
static bool is_string_valid_name(std::string_view str)
{
size_t slen = str.size();
if( slen > 13)
return false;

size_t len = (slen <= 12) ? slen : 12;
for( size_t i = 0; i < len; ++i ) {
char c = str[i];
if ((c >= 'a' && c <= 'z') || (c >= '1' && c <= '5') || (c == '.'))
continue;
else
return false;
}

if( slen == 13) {
char c = str[12];
if ((c >= 'a' && c <= 'j') || (c >= '1' && c <= '5') || (c == '.'))
return true;
else
return false;
}

return true;
}
bool is_string_valid_name(std::string_view str);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making it a constexpr instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it constexpr only make if it is called with the input parameter can be known at compile time. I don't see any of callers of the function meet that criteria except those in unit tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, use case is likely not there.


static constexpr uint64_t string_to_uint64_t( std::string_view str ) {
EOS_ASSERT(str.size() <= 13, name_type_exception, "Name is longer than 13 characters (${name}) ", ("name", std::string(str)));
Expand Down
26 changes: 26 additions & 0 deletions libraries/chain/name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ namespace eosio::chain {
return str;
}

bool is_string_valid_name(std::string_view str)
{
size_t slen = str.size();
if( slen > 13)
return false;

size_t len = (slen <= 12) ? slen : 12;
for( size_t i = 0; i < len; ++i ) {
char c = str[i];
if ((c >= 'a' && c <= 'z') || (c >= '1' && c <= '5') || (c == '.'))
continue;
else
return false;
}

if( slen == 13) {
char c = str[12];
if ((c >= 'a' && c <= 'j') || (c >= '1' && c <= '5') || (c == '.'))
return true;
else
return false;
}

return true;
}

} // eosio::chain

namespace fc {
Expand Down