mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 09:08:08 +08:00
Merge pull request #60 from ToruNiina/string-view
support std::string_view
This commit is contained in:
12
toml/get.hpp
12
toml/get.hpp
@@ -108,6 +108,18 @@ inline std::string get(value&& v)
|
||||
return std::move(v.cast<value_t::String>().str);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// std::string_view
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
template<typename T, typename std::enable_if<
|
||||
std::is_same<T, std::string_view>::value, std::nullptr_t>::type = nullptr>
|
||||
inline std::string_view get(const value& v)
|
||||
{
|
||||
return std::string_view(v.cast<value_t::String>().str);
|
||||
}
|
||||
#endif
|
||||
|
||||
// ============================================================================
|
||||
// std::chrono::duration from toml::local_time.
|
||||
|
||||
|
@@ -4,6 +4,11 @@
|
||||
#define TOML11_STRING_HPP
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#if __cplusplus >= 201703L
|
||||
#if __has_include(<string_view>)
|
||||
#include <string_view>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace toml
|
||||
{
|
||||
@@ -40,6 +45,17 @@ struct string
|
||||
operator std::string const& () const& noexcept {return str;}
|
||||
operator std::string&& () && noexcept {return std::move(str);}
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
explicit string(std::string_view s): kind(string_t::basic), str(s){}
|
||||
string(std::string_view s, string_t k): kind(k), str(s){}
|
||||
|
||||
string& operator=(std::string_view s)
|
||||
{kind = string_t::basic; str = s; return *this;}
|
||||
|
||||
explicit operator std::string_view() const noexcept
|
||||
{return std::string_view(str);}
|
||||
#endif
|
||||
|
||||
string_t kind;
|
||||
std::string str;
|
||||
};
|
||||
|
@@ -7,6 +7,11 @@
|
||||
#include "traits.hpp"
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#if __cplusplus >= 201703L
|
||||
#if __has_include(<string_view>)
|
||||
#include <string_view>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace toml
|
||||
{
|
||||
@@ -172,6 +177,9 @@ template<typename T>
|
||||
struct is_container : conjunction<
|
||||
negation<is_map<T>>,
|
||||
negation<std::is_same<T, std::string>>,
|
||||
#if __cplusplus >= 201703L
|
||||
negation<std::is_same<T, std::string_view>>,
|
||||
#endif
|
||||
has_iterator<T>,
|
||||
has_value_type<T>
|
||||
>{};
|
||||
|
@@ -14,6 +14,9 @@
|
||||
#include <unordered_map>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#if __cplusplus >= 201703L
|
||||
#include <string_view>
|
||||
#endif
|
||||
|
||||
namespace toml
|
||||
{
|
||||
@@ -293,6 +296,29 @@ class value
|
||||
assigner(this->string_, toml::string(std::string(s), kind));
|
||||
}
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
value(std::string_view s)
|
||||
: type_(value_t::String),
|
||||
region_info_(std::make_shared<region_base>(region_base{}))
|
||||
{
|
||||
assigner(this->string_, toml::string(s));
|
||||
}
|
||||
value& operator=(std::string_view s)
|
||||
{
|
||||
this->cleanup();
|
||||
this->type_ = value_t::String;
|
||||
this->region_info_ = std::make_shared<region_base>(region_base{});
|
||||
assigner(this->string_, toml::string(s));
|
||||
return *this;
|
||||
}
|
||||
value(std::string_view s, string_t kind)
|
||||
: type_(value_t::String),
|
||||
region_info_(std::make_shared<region_base>(region_base{}))
|
||||
{
|
||||
assigner(this->string_, toml::string(s, kind));
|
||||
}
|
||||
#endif
|
||||
|
||||
// local date ===========================================================
|
||||
|
||||
value(const local_date& ld)
|
||||
|
Reference in New Issue
Block a user