From a5e5a233b7bde503a12e8666b3880d561c1b622f Mon Sep 17 00:00:00 2001 From: zxey Date: Mon, 17 Jun 2019 17:31:06 +0200 Subject: [PATCH] Use std::string_view if c+17 is available --- README.md | 2 +- include/jwt/jwt.hpp | 12 ++++++------ include/jwt/string_view.hpp | 19 ++++++++++++------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7c21a03..7292e85 100644 --- a/README.md +++ b/README.md @@ -660,7 +660,7 @@ Things one may have questions about - There is a string_view implementation. Why not use boost::string_ref ? Sorry, I love boost! But, do not want it to be part of dependency. - Having said that, I can use some MACRO to use boost::string_ref 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 ? diff --git a/include/jwt/jwt.hpp b/include/jwt/jwt.hpp index 7cc0ecc..3c90549 100644 --- a/include/jwt/jwt.hpp +++ b/include/jwt/jwt.hpp @@ -302,8 +302,8 @@ public: // 'tors : alg_(alg) , typ_(typ) { - payload_["typ"] = type_to_str(typ_).to_string(); - payload_["alg"] = alg_to_str(alg_).to_string(); + payload_["typ"] = std::string(type_to_str(typ_)); + payload_["alg"] = std::string(alg_to_str(alg_)); } /** @@ -331,7 +331,7 @@ public: // Exposed APIs void algo(SCOPED_ENUM algorithm 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) { 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 { 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) { typ_ = str_to_type(sv.data()); - payload_["typ"] = type_to_str(typ_).to_string(); + payload_["typ"] = std::string(type_to_str(typ_)); } /** diff --git a/include/jwt/string_view.hpp b/include/jwt/string_view.hpp index f6e5730..65e0c37 100644 --- a/include/jwt/string_view.hpp +++ b/include/jwt/string_view.hpp @@ -23,6 +23,16 @@ SOFTWARE. #ifndef JWT_STRING_VIEW_HPP #define JWT_STRING_VIEW_HPP +#if __cplusplus >= 201703L + +#include + +namespace jwt { + using string_view = std::string_view; +} + +#else // __cplusplus >= 201703L + #include #include #include @@ -177,13 +187,6 @@ public: // Exposed APIs return {data_, len_}; } - template > - std::basic_string - to_string(const Allocator& alloc = Allocator()) const - { - return {data_, len_, alloc}; - } - // NOTE: Does not throw size_type copy(CharT* dest, size_type n, size_type pos = 0) const noexcept { @@ -372,4 +375,6 @@ using string_view = basic_string_view; #include "jwt/impl/string_view.ipp" +#endif // __cplusplus >= 201703L + #endif