Use std::string_view if c+17 is available

This commit is contained in:
zxey 2019-06-17 17:31:06 +02:00
parent af3fef94f1
commit a5e5a233b7
3 changed files with 19 additions and 14 deletions

View file

@ -660,7 +660,7 @@ Things one may have questions about
- There is a string_view implementation. Why not use <code>boost::string_ref</code> ? - There is a string_view implementation. Why not use <code>boost::string_ref</code> ?
Sorry, I love boost! But, do not want it to be part of dependency. Sorry, I love boost! But, do not want it to be part of dependency.
Having said that, I can use some MACRO to use <code>boost::string_ref</code> or C++17 string_view if available. Perhaps in future. If you use C++17 or greater `std::string_view` gets used instead and `jwt::string_view` implementation does not get included.
- You are not using the stack allocator or the shart string anywhere. Why to include it then ? - You are not using the stack allocator or the shart string anywhere. Why to include it then ?

View file

@ -302,8 +302,8 @@ public: // 'tors
: alg_(alg) : alg_(alg)
, typ_(typ) , typ_(typ)
{ {
payload_["typ"] = type_to_str(typ_).to_string(); payload_["typ"] = std::string(type_to_str(typ_));
payload_["alg"] = alg_to_str(alg_).to_string(); payload_["alg"] = std::string(alg_to_str(alg_));
} }
/** /**
@ -331,7 +331,7 @@ public: // Exposed APIs
void algo(SCOPED_ENUM algorithm alg) void algo(SCOPED_ENUM algorithm alg)
{ {
alg_ = alg; alg_ = alg;
payload_["alg"] = alg_to_str(alg_).to_string(); payload_["alg"] = std::string(alg_to_str(alg_));
} }
/** /**
@ -340,7 +340,7 @@ public: // Exposed APIs
void algo(const jwt::string_view sv) void algo(const jwt::string_view sv)
{ {
alg_ = str_to_alg(sv.data()); alg_ = str_to_alg(sv.data());
payload_["alg"] = alg_to_str(alg_).to_string(); payload_["alg"] = std::string(alg_to_str(alg_));
} }
/** /**
@ -361,7 +361,7 @@ public: // Exposed APIs
void typ(SCOPED_ENUM type typ) noexcept void typ(SCOPED_ENUM type typ) noexcept
{ {
typ_ = typ; typ_ = typ;
payload_["typ"] = type_to_str(typ_).to_string(); payload_["typ"] = std::string(type_to_str(typ_));
} }
/** /**
@ -370,7 +370,7 @@ public: // Exposed APIs
void typ(const jwt::string_view sv) void typ(const jwt::string_view sv)
{ {
typ_ = str_to_type(sv.data()); typ_ = str_to_type(sv.data());
payload_["typ"] = type_to_str(typ_).to_string(); payload_["typ"] = std::string(type_to_str(typ_));
} }
/** /**

View file

@ -23,6 +23,16 @@ SOFTWARE.
#ifndef JWT_STRING_VIEW_HPP #ifndef JWT_STRING_VIEW_HPP
#define JWT_STRING_VIEW_HPP #define JWT_STRING_VIEW_HPP
#if __cplusplus >= 201703L
#include <string_view>
namespace jwt {
using string_view = std::string_view;
}
#else // __cplusplus >= 201703L
#include <limits> #include <limits>
#include <string> #include <string>
#include <cassert> #include <cassert>
@ -177,13 +187,6 @@ public: // Exposed APIs
return {data_, len_}; return {data_, len_};
} }
template <typename Allocator = std::allocator<CharT>>
std::basic_string<CharT, Traits, Allocator>
to_string(const Allocator& alloc = Allocator()) const
{
return {data_, len_, alloc};
}
// NOTE: Does not throw // NOTE: Does not throw
size_type copy(CharT* dest, size_type n, size_type pos = 0) const noexcept size_type copy(CharT* dest, size_type n, size_type pos = 0) const noexcept
{ {
@ -372,4 +375,6 @@ using string_view = basic_string_view<char>;
#include "jwt/impl/string_view.ipp" #include "jwt/impl/string_view.ipp"
#endif // __cplusplus >= 201703L
#endif #endif