/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <type_traits>
#include <string>
#include <regex>
namespace std {
template<typename Type>
struct not_specialized : false_type {};
template<typename Type>
Type stot(const std::string &string) {
static_assert(not_specialized<Type>::value,
"\n<Type> is not specialized\nFix: specialize stot<Type> to this <Type>");
}
template<>
int16_t stot<int16_t>(const string &string) {
int16_t value = 0;
sscanf(string.c_str(), "%hd", &value);
return value;
}
template<>
uint16_t stot<uint16_t>(const string &string) {
uint16_t value = 0;
sscanf(string.c_str(), "%hu", &value);
return value;
}
template<>
int32_t stot<int32_t>(const string &string) {
int32_t value = 0;
sscanf(string.c_str(), "%d", &value);
return value;
}
template<>
uint32_t stot<uint32_t>(const string &string) {
uint32_t value = 0U;
sscanf(string.c_str(), "%u", &value);
return value;
}
template<>
int64_t stot<int64_t>(const string &string) {
int64_t value = 0L;
sscanf(string.c_str(), "%ld", &value);
return value;
}
template<>
uint64_t stot<uint64_t>(const string &string) {
uint64_t value = 0UL;
sscanf(string.c_str(), "%lu", &value);
return value;
}
template<>
float stot<float>(const string &string) {
float value = 0.0F;
sscanf(string.c_str(), "%f", &value);
return value;
}
template<>
double stot<double>(const string &string) {
double value = 0.0;
sscanf(string.c_str(), "%lf", &value);
return value;
}
template<>
bool stot<bool>(const string &string) {
if (std::regex_match(string, std::regex(R"(^(false|-?0\.?0?))"))) {
return false;
} else if (std::regex_match(string, std::regex(R"(^(true|-?\d+\.?\d*))"))) {
return true;
} else {
throw std::invalid_argument("invalid argument");
}
}
const auto stos = stot<int16_t>;
const auto stous = stot<uint16_t>;
const auto stoi = stot<int32_t>;
const auto stoui = stot<uint32_t>;
const auto stol = stot<int64_t>;
const auto stoul = stot<uint64_t>;
const auto stof = stot<float>;
const auto stod = stot<double>;
const auto stob = stot<bool>;
}
int32_t main(int32_t argc, const char **argv) {
printf("String To Int16: %i\n", std::stot<int16_t>("1234"));
printf("String To Int32: %i\n", std::stot<int32_t>("12345678"));
printf("String To Int64: %li\n", std::stot<int64_t>("1245784512"));
printf("String To Float: %f\n", std::stot<float>("404.1212"));
printf("String To Double: %lf\n", std::stot<double>("404.1245"));
printf("String To Bool: %d\n", std::stot<bool>("true"));
printf("String To Bool: %d\n", std::stot<bool>("0.0"));
printf("String To Bool: %d\n", std::stot<bool>("false"));
printf("String To Bool: %d\n", std::stot<bool>("2545"));
return 0;
}