123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- #include "rttr/detail/conversion/std_conversion_functions.h"
- #include "rttr/detail/conversion/number_conversion.h"
- #include <sstream>
- #include <locale>
- #include <limits>
- #include <algorithm>
- #include <climits>
- #include <iomanip>
- namespace rttr
- {
- namespace detail
- {
- template<typename T>
- std::string to_string_impl(T value, bool* ok)
- {
- try
- {
- std::string text = std::to_string(value);
- if (ok)
- *ok = true;
- return text;
- }
- catch (...)
- {
- if (ok)
- *ok = false;
- return std::string();
- }
- }
- std::string to_string(int value, bool* ok)
- {
- return to_string_impl(value, ok);
- }
- std::string to_string(long value, bool* ok)
- {
- return to_string_impl(value, ok);
- }
- std::string to_string(long long value, bool* ok)
- {
- return to_string_impl(value, ok);
- }
- std::string to_string(unsigned value, bool* ok)
- {
- return to_string_impl(value, ok);
- }
- std::string to_string(unsigned long value, bool* ok)
- {
- return to_string_impl(value, ok);
- }
- std::string to_string(unsigned long long value, bool* ok)
- {
- return to_string_impl(value, ok);
- }
- std::string to_string(float value, bool* ok)
- {
- try
- {
- std::stringstream ss;
- ss << std::setprecision(std::numeric_limits<float>::digits10) << value;
- if (ok)
- *ok = true;
- return ss.str();
- }
- catch (...)
- {
- if (ok)
- *ok = false;
- return std::string();
- }
- }
- std::string to_string(double value, bool* ok)
- {
- try
- {
- std::stringstream ss;
- ss << std::setprecision(std::numeric_limits<double>::digits10) << value;
- if (ok)
- *ok = true;
- return ss.str();
- }
- catch (...)
- {
- if (ok)
- *ok = false;
- return std::string();
- }
- }
- bool string_to_bool(std::string text, bool* ok)
- {
- std::transform(text.begin(), text.end(), text.begin(), [](char ch) { return std::tolower(ch, std::locale::classic()); });
- text.erase( std::remove_if( text.begin(), text.end(), [](char ch) { return std::isspace(ch, std::locale::classic() ); } ), text.end() );
- if (text == "false" || text == "0" || text.empty())
- {
- if (ok)
- *ok = true;
- return false;
- }
- if (ok)
- *ok = true;
- return true;
- }
- int string_to_int(const std::string& source, bool* ok)
- {
- try
- {
- std::size_t pos = 0;
- const int value = std::stoi(source, &pos);
- if (pos == source.length())
- {
- if (ok)
- *ok = true;
- return value;
- }
- }
- catch (...)
- {
- if (ok)
- *ok = false;
- return 0;
- }
- if (ok)
- *ok = false;
- return 0;
- }
- unsigned long string_to_ulong(const std::string& source, bool* ok)
- {
- try
- {
- std::size_t pos = 0;
- const long long value = std::stoll(source, &pos);
- unsigned long result = 0;
- if (pos == source.length() && convert_to(value, result))
- {
- if (ok)
- *ok = true;
- return result;
- }
- }
- catch (...)
- {
- if (ok)
- *ok = false;
- return 0;
- }
- if (ok)
- *ok = false;
- return 0;
- }
- long long string_to_long_long(const std::string& source, bool* ok)
- {
- try
- {
- std::size_t pos = 0;
- const long long value = std::stoll(source, &pos);
- if (pos == source.length())
- {
- if (ok)
- *ok = true;
- return value;
- }
- }
- catch (...)
- {
- if (ok)
- *ok = false;
- return 0;
- }
- if (ok)
- *ok = false;
- return 0;
- }
- unsigned long long string_to_ulong_long(const std::string& source, bool* ok)
- {
- try
- {
- std::size_t pos = 0;
- const auto itr = std::find_if(source.begin(), source.end(), [](char c){ return !std::isdigit(c, std::locale()); });
- if (itr == source.end())
- {
- const unsigned long long value = std::stoull(source, &pos);
- if (pos == source.length())
- {
- if (ok)
- *ok = true;
- return value;
- }
- }
- }
- catch (...)
- {
- if (ok)
- *ok = false;
- return 0;
- }
- if (ok)
- *ok = false;
- return 0;
- }
- float string_to_float(const std::string& source, bool* ok)
- {
- try
- {
- std::size_t pos = 0;
- const float value = std::stof(source, &pos);
- if (pos == source.length())
- {
- if (ok)
- *ok = true;
- return value;
- }
- }
- catch (...)
- {
- if (ok)
- *ok = false;
- return 0;
- }
- if (ok)
- *ok = false;
- return 0;
- }
- double string_to_double(const std::string& source, bool* ok)
- {
- try
- {
- std::size_t pos = 0;
- const double value = std::stod(source, &pos);
- if (pos == source.length())
- {
- if (ok)
- *ok = true;
- return value;
- }
- }
- catch (...)
- {
- if (ok)
- *ok = false;
- return 0;
- }
- if (ok)
- *ok = false;
- return 0;
- }
- }
- }
|