diff --git a/nan.h b/nan.h index 2423ec92..c616ff5f 100644 --- a/nan.h +++ b/nan.h @@ -51,6 +51,12 @@ # error This version of node/NAN/v8 requires a C++11 compiler #endif +#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 @@ -72,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)