mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-12-16 03:08:52 +08:00
add from_string to utility
This commit is contained in:
@@ -68,3 +68,15 @@ BOOST_AUTO_TEST_CASE(test_concat_to_string)
|
|||||||
const std::string cat = toml::concat_to_string("foo", "bar", 42);
|
const std::string cat = toml::concat_to_string("foo", "bar", 42);
|
||||||
BOOST_CHECK(cat == "foobar42");
|
BOOST_CHECK(cat == "foobar42");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_from_string)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
const std::string str("123");
|
||||||
|
BOOST_CHECK_EQUAL(toml::from_string<int>(str, 0), 123);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string str("01");
|
||||||
|
BOOST_CHECK_EQUAL(toml::from_string<int>(str, 0), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -62,5 +62,16 @@ std::string concat_to_string(Ts&& ... args)
|
|||||||
return detail::concat_to_string_impl(oss, std::forward<Ts>(args) ...);
|
return detail::concat_to_string_impl(oss, std::forward<Ts>(args) ...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T, typename U>
|
||||||
|
T from_string(const std::string& str, U&& opt)
|
||||||
|
{
|
||||||
|
T v(std::forward<U>(opt));
|
||||||
|
std::istringstream iss(str);
|
||||||
|
iss >> v;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}// toml
|
}// toml
|
||||||
#endif // TOML11_UTILITY
|
#endif // TOML11_UTILITY
|
||||||
|
|||||||
Reference in New Issue
Block a user