Skip to content

Instantly share code, notes, and snippets.

@jafri
Created March 30, 2020 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jafri/fe5c4871b734d77b06daa32bac826b72 to your computer and use it in GitHub Desktop.
Save jafri/fe5c4871b734d77b06daa32bac826b72 to your computer and use it in GitHub Desktop.
// MIT License Syed Jafri Copyright 2020
#include <test/test.hpp>
namespace eosio_test {
std::vector<std::string> split(std::string str, std::string token){
std::vector<std::string> result;
while (str.size()) {
int index = str.find(token);
if (index != std::string::npos) {
result.push_back(str.substr(0,index));
str = str.substr(index + token.size());
if (str.size() == 0) {
result.push_back(str);
}
} else {
result.push_back(str);
str = "";
}
}
return result;
}
void test::bench()
{
std::string test = "2.0000 EOS";
auto split_tokens = split(test, " ");
double amount = strtof(split_tokens[0].c_str(), nullptr);
auto final_amount = static_cast<int64_t>(amount * 10000);
// Validation
eosio::check(split_tokens.size() == 2, "Invalid entry");
eosio::check(final_amount > 0, "Invalid entry");
eosio::check(split_tokens[1] == "EOS", "Invalid entry");
auto symbol_code = eosio::symbol_code{split_tokens[1]};
auto symbol = eosio::symbol{symbol_code, 4};
auto final_asset = eosio::asset{final_amount, symbol};
eosio::print(final_asset);
}
} /** eosio_test **/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment