Created
March 30, 2020 22:14
-
-
Save jafri/fe5c4871b734d77b06daa32bac826b72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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