From 626bfe230d69fe477fc50b7b3e4d53152bf48567 Mon Sep 17 00:00:00 2001 From: Robin Christ Date: Sat, 30 Nov 2019 14:27:44 +0100 Subject: [PATCH] Add variant of Nan::New which takes a C++17 string_view --- nan.h | 7 +++++++ nan_new.h | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/nan.h b/nan.h index 3b907a3c..c616ff5f 100644 --- a/nan.h +++ b/nan.h @@ -53,6 +53,10 @@ #define NAN_HAS_CPLUSPLUS_17 (__cplusplus >= 201703L) +#ifndef NAN_ENABLE_STRING_VIEW +# define NAN_ENABLE_STRING_VIEW NAN_HAS_CPLUSPLUS_17 +#endif + #include #include #include @@ -74,6 +78,9 @@ # include # include #endif +#if NAN_ENABLE_STRING_VIEW +# include +#endif // uv helpers #ifdef UV_VERSION_MAJOR diff --git a/nan_new.h b/nan_new.h index cdf8bbe4..5561293d 100644 --- a/nan_new.h +++ b/nan_new.h @@ -161,6 +161,9 @@ struct Factory : MaybeFactoryBase { static inline return_t New(const char *value, int length = -1); static inline return_t New(const uint16_t *value, int length = -1); static inline return_t New(std::string const& value); + #if NAN_ENABLE_STRING_VIEW + static inline return_t New(std::string_view value); + #endif static inline return_t New(v8::String::ExternalStringResource * value); static inline return_t New(ExternalOneByteStringResource * value); @@ -289,6 +292,14 @@ New(double value) { return New(value); } +#if NAN_ENABLE_STRING_VIEW +inline +imp::Factory::return_t +New(std::string_view value) { + return New(value.data(), value.length()); +} +#endif + inline imp::Factory::return_t New(std::string const& value) { // NOLINT(build/include_what_you_use)