diff --git a/sprout/optional/nullopt.hpp b/sprout/optional/nullopt.hpp index 82dc2063..97829c3f 100644 --- a/sprout/optional/nullopt.hpp +++ b/sprout/optional/nullopt.hpp @@ -6,8 +6,8 @@ namespace sprout { // - // none_t - // none + // nullopt_t + // nullopt // typedef sprout::none_t nullopt_t; namespace { diff --git a/sprout/string/from_string.hpp b/sprout/string/from_string.hpp index 3775ca16..43642b82 100644 --- a/sprout/string/from_string.hpp +++ b/sprout/string/from_string.hpp @@ -1,7 +1,7 @@ #ifndef SPROUT_STRING_FROM_STRING_HPP #define SPROUT_STRING_FROM_STRING_HPP -#include +#include #include #include diff --git a/sprout/string/npos.hpp b/sprout/string/npos.hpp index 1761dffc..61968484 100644 --- a/sprout/string/npos.hpp +++ b/sprout/string/npos.hpp @@ -1,16 +1,118 @@ #ifndef SPROUT_STRING_NPOS_HPP #define SPROUT_STRING_NPOS_HPP -#include #include +#include +#include namespace sprout { // + // npos_t // npos // + struct npos_t { + public: + template::value>::type = sprout::enabler> + SPROUT_CONSTEXPR operator UIntType() const { + return UIntType(-1); + } + }; namespace { - SPROUT_STATIC_CONSTEXPR std::size_t npos = -1; + SPROUT_STATIC_CONSTEXPR sprout::npos_t npos{}; } // anonymous-namespace + + // + // operator== + // operator!= + // operator< + // operator> + // operator<= + // operator>= + // + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator==(UIntType const& lhs, sprout::npos_t rhs) { + return lhs == UIntType(rhs); + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator==(sprout::npos_t lhs, UIntType const& rhs) { + return rhs == lhs; + } + inline SPROUT_CONSTEXPR bool + operator==(sprout::npos_t lhs, sprout::npos_t rhs) { + return true; + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator!=(UIntType const& lhs, sprout::npos_t rhs) { + return !(lhs == rhs); + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator!=(sprout::npos_t lhs, UIntType const& rhs) { + return !(lhs == rhs); + } + inline SPROUT_CONSTEXPR bool + operator!=(sprout::npos_t lhs, sprout::npos_t rhs) { + return !(lhs == rhs); + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator<(UIntType const& lhs, sprout::npos_t rhs) { + return lhs < UIntType(rhs); + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator<(sprout::npos_t lhs, UIntType const& rhs) { + return rhs == lhs; + } + inline SPROUT_CONSTEXPR bool + operator<(sprout::npos_t lhs, sprout::npos_t rhs) { + return false; + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator>(UIntType const& lhs, sprout::npos_t rhs) { + return rhs < lhs; + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator>(sprout::npos_t lhs, UIntType const& rhs) { + return rhs < lhs; + } + inline SPROUT_CONSTEXPR bool + operator>(sprout::npos_t lhs, sprout::npos_t rhs) { + return rhs < lhs; + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator<=(UIntType const& lhs, sprout::npos_t rhs) { + return !(rhs < lhs); + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator<=(sprout::npos_t lhs, UIntType const& rhs) { + return !(rhs < lhs); + } + inline SPROUT_CONSTEXPR bool + operator<=(sprout::npos_t lhs, sprout::npos_t rhs) { + return !(rhs < lhs); + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator>=(UIntType const& lhs, sprout::npos_t rhs) { + return !(lhs < rhs); + } + template::value>::type = sprout::enabler> + inline SPROUT_CONSTEXPR bool + operator>=(sprout::npos_t lhs, UIntType const& rhs) { + return !(lhs < rhs); + } + inline SPROUT_CONSTEXPR bool + operator>=(sprout::npos_t lhs, sprout::npos_t rhs) { + return !(lhs < rhs); + } } // namespace sprout #endif // #ifndef SPROUT_STRING_NPOS_HPP diff --git a/sprout/string/to_string.hpp b/sprout/string/to_string.hpp index 9be354e5..68761558 100644 --- a/sprout/string/to_string.hpp +++ b/sprout/string/to_string.hpp @@ -1,7 +1,7 @@ #ifndef SPROUT_STRING_TO_STRING_HPP #define SPROUT_STRING_TO_STRING_HPP -#include +#include #include #include diff --git a/sprout/utility/string_ref.hpp b/sprout/utility/string_ref.hpp index 1f8c1382..ef5ccbdb 100644 --- a/sprout/utility/string_ref.hpp +++ b/sprout/utility/string_ref.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include diff --git a/sprout/utility/string_ref/conversion.hpp b/sprout/utility/string_ref/conversion.hpp new file mode 100644 index 00000000..394f1753 --- /dev/null +++ b/sprout/utility/string_ref/conversion.hpp @@ -0,0 +1,7 @@ +#ifndef SPROUT_UTILITY_STRING_REF_CONVERSION_HPP +#define SPROUT_UTILITY_STRING_REF_CONVERSION_HPP + +#include +#include + +#endif // #ifndef SPROUT_UTILITY_STRING_REF_CONVERSION_HPP diff --git a/sprout/utility/string_ref/from_string.hpp b/sprout/utility/string_ref/from_string.hpp new file mode 100644 index 00000000..ddde8500 --- /dev/null +++ b/sprout/utility/string_ref/from_string.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_UTILITY_STRING_REF_FROM_STRING_HPP +#define SPROUT_UTILITY_STRING_REF_FROM_STRING_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_UTILITY_STRING_REF_FROM_STRING_HPP diff --git a/sprout/utility/string_ref/string_ref.hpp b/sprout/utility/string_ref/string_ref.hpp index f7a56b30..da1754e8 100644 --- a/sprout/utility/string_ref/string_ref.hpp +++ b/sprout/utility/string_ref/string_ref.hpp @@ -82,11 +82,11 @@ namespace sprout { {} template SPROUT_CONSTEXPR basic_string_ref(sprout::basic_string const& str) - : ptr_(str.data()), len_(str.length()) + : ptr_(str.data()), len_(str.size()) {} template SPROUT_CONSTEXPR basic_string_ref(std::basic_string const& str) - : ptr_(str.data()), len_(str.length()) + : ptr_(str.data()), len_(str.size()) {} SPROUT_CONSTEXPR basic_string_ref(const_pointer str, size_type len) : ptr_(str), len_(len) @@ -550,6 +550,30 @@ namespace sprout { { lhs.swap(rhs); } + + // + // to_string_ref + // + template + inline SPROUT_CONSTEXPR sprout::basic_string_ref + to_string_ref(sprout::basic_string const& s) { + return sprout::basic_string_ref(s); + } + template + inline SPROUT_CONSTEXPR sprout::basic_string_ref + to_string_ref(std::basic_string const& s) { + return sprout::basic_string_ref(s); + } + template + inline SPROUT_CONSTEXPR sprout::basic_string_ref + to_string_ref(T const* str) { + return sprout::basic_string_ref(str); + } + template + inline SPROUT_CONSTEXPR sprout::basic_string_ref + to_string_ref(T const* str, std::size_t len) { + return sprout::basic_string_ref(str, len); + } } // namespace sprout #endif // #ifndef SPROUT_UTILITY_STRING_REF_STRING_REF_HPP diff --git a/sprout/utility/string_ref/string_to_float.hpp b/sprout/utility/string_ref/string_to_float.hpp new file mode 100644 index 00000000..e829ef92 --- /dev/null +++ b/sprout/utility/string_ref/string_to_float.hpp @@ -0,0 +1,109 @@ +#ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_FLOAT_HPP +#define SPROUT_UTILITY_STRING_REF_STRING_TO_FLOAT_HPP + +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template< + typename FloatType, typename Elem, typename Traits + > + inline FloatType string_to_float_dynamic(sprout::basic_string_ref const& str, std::size_t* idx) { + Elem* endptr = nullptr; + FloatType result = sprout::detail::str_to_float(str.c_str(), &endptr); + *idx = endptr - str.c_str(); + return result; + } + } + + // + // string_to_float + // + template< + typename FloatType, typename Elem, typename Traits, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + string_to_float(sprout::basic_string_ref const& str, std::size_t* idx) { + return !idx ? sprout::detail::str_to_float(str.begin()) + : sprout::detail::string_to_float_dynamic(str, idx) + ; + } + template< + typename FloatType, typename Elem, typename Traits, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + string_to_float(sprout::basic_string_ref const& str) { + return sprout::detail::str_to_float(str.begin()); + } + + // + // stof + // + template + inline SPROUT_CONSTEXPR float + stof(sprout::basic_string_ref const& str, std::size_t* idx) { + return sprout::string_to_float(str, idx); + } + template + inline SPROUT_CONSTEXPR float + stof(sprout::basic_string_ref const& str) { + return sprout::string_to_float(str); + } + + // + // stod + // + template + inline SPROUT_CONSTEXPR double + stod(sprout::basic_string_ref const& str, std::size_t* idx) { + return sprout::string_to_float(str, idx); + } + template + inline SPROUT_CONSTEXPR double + stod(sprout::basic_string_ref const& str) { + return sprout::string_to_float(str); + } + + // + // stold + // + template + inline SPROUT_CONSTEXPR long double + stold(sprout::basic_string_ref const& str, std::size_t* idx) { + return sprout::string_to_float(str, idx); + } + template + inline SPROUT_CONSTEXPR long double + stold(sprout::basic_string_ref const& str) { + return sprout::string_to_float(str); + } + + // + // from_string + // + template< + typename FloatType, typename Elem, typename Traits, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + from_string(sprout::basic_string_ref const& str, std::size_t* idx) { + return sprout::string_to_float(str, idx); + } + template< + typename FloatType, typename Elem, typename Traits, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + from_string(sprout::basic_string_ref const& str) { + return sprout::string_to_float(str); + } +} // namespace sprout + +#endif // #ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_FLOAT_HPP diff --git a/sprout/utility/string_ref/string_to_int.hpp b/sprout/utility/string_ref/string_to_int.hpp new file mode 100644 index 00000000..52d48303 --- /dev/null +++ b/sprout/utility/string_ref/string_to_int.hpp @@ -0,0 +1,165 @@ +#ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_INT_HPP +#define SPROUT_UTILITY_STRING_REF_STRING_TO_INT_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template + inline IntType + string_to_int_dynamic(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { + Elem* endptr = nullptr; + IntType result = sprout::detail::str_to_int(str.c_str(), &endptr, base); + *idx = endptr - str.c_str(); + return result; + } + } + + // + // string_to_int + // + template< + typename IntType, typename Elem, typename Traits, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR IntType + string_to_int(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { + return !idx ? sprout::detail::str_to_int(str.begin(), base) + : sprout::detail::string_to_int_dynamic(str, idx, base) + ; + } + template< + typename IntType, typename Elem, typename Traits, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR IntType + string_to_int(sprout::basic_string_ref const& str, int base = 10) { + return sprout::detail::str_to_int(str.begin(), base); + } + + // + // stoi + // + template + inline SPROUT_CONSTEXPR int + stoi(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { + return sprout::string_to_int(str, idx, base); + } + template + inline SPROUT_CONSTEXPR int + stoi(sprout::basic_string_ref const& str, int base = 10) { + return sprout::string_to_int(str, base); + } + + // + // stol + // + template + inline SPROUT_CONSTEXPR long + stol(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { + return sprout::string_to_int(str, idx, base); + } + template + inline SPROUT_CONSTEXPR long + stol(sprout::basic_string_ref const& str, int base = 10) { + return sprout::string_to_int(str, base); + } + + // + // stoul + // + template + inline SPROUT_CONSTEXPR unsigned long + stoul(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { + return sprout::string_to_int(str, idx, base); + } + template + inline SPROUT_CONSTEXPR unsigned long + stoul(sprout::basic_string_ref const& str, int base = 10) { + return sprout::string_to_int(str, base); + } + + // + // stoll + // + template + inline SPROUT_CONSTEXPR long long + stoll(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { + return sprout::string_to_int(str, idx, base); + } + template + inline SPROUT_CONSTEXPR long long + stoll(sprout::basic_string_ref const& str, int base = 10) { + return sprout::string_to_int(str, base); + } + + // + // stoull + // + template + inline SPROUT_CONSTEXPR unsigned long long + stoull(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { + return sprout::string_to_int(str, idx, base); + } + template + inline SPROUT_CONSTEXPR unsigned long long + stoull(sprout::basic_string_ref const& str, int base = 10) { + return sprout::string_to_int(str, base); + } + + // + // stoimax + // + template + inline SPROUT_CONSTEXPR std::intmax_t + stoimax(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { + return sprout::string_to_int(str, idx, base); + } + template + inline SPROUT_CONSTEXPR std::intmax_t + stoimax(sprout::basic_string_ref const& str, int base = 10) { + return sprout::string_to_int(str, base); + } + + // + // stoumax + // + template + inline SPROUT_CONSTEXPR std::uintmax_t + stoumax(sprout::basic_string_ref const& str, std::size_t* idx, int base = 10) { + return sprout::string_to_int(str, idx, base); + } + template + inline SPROUT_CONSTEXPR std::uintmax_t + stoumax(sprout::basic_string_ref const& str, int base = 10) { + return sprout::string_to_int(str, base); + } + + // + // from_string + // + template< + typename IntType, typename Elem, typename Traits, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR IntType + from_string(sprout::basic_string_ref const& str, std::size_t* idx) { + return sprout::string_to_int(str, idx, 0); + } + template< + typename IntType, typename Elem, typename Traits, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR IntType + from_string(sprout::basic_string_ref const& str) { + return sprout::string_to_int(str, 0); + } +} // namespace sprout + +#endif // #ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_INT_HPP