diff --git a/napi-inl.h b/napi-inl.h index 0f1717ecd..b7cf63c1b 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -20,6 +20,9 @@ #endif // NAPI_HAS_THREADS #include #include +#if __cplusplus >= 201703L +#include +#endif #if defined(__clang__) || defined(__GNUC__) #define NAPI_NO_SANITIZE_VPTR __attribute__((no_sanitize("vptr"))) @@ -1255,6 +1258,12 @@ inline String String::New(napi_env env, const std::u16string& val) { return String::New(env, val.c_str(), val.size()); } +#if __cplusplus >= 201703L +inline String String::New(napi_env env, std::string_view val) { + return String::New(env, val.data(), val.size()); +} +#endif + inline String String::New(napi_env env, const char* val) { // TODO(@gabrielschulhof) Remove if-statement when core's error handling is // available in all supported versions. @@ -1364,6 +1373,13 @@ inline Symbol Symbol::New(napi_env env, const std::string& description) { return Symbol::New(env, descriptionValue); } +#if __cplusplus >= 201703L +inline Symbol Symbol::New(napi_env env, std::string_view description) { + napi_value descriptionValue = String::New(env, description); + return Symbol::New(env, descriptionValue); +} +#endif + inline Symbol Symbol::New(napi_env env, String description) { napi_value descriptionValue = description; return Symbol::New(env, descriptionValue); diff --git a/napi.h b/napi.h index 013a9114d..d9c4c4add 100644 --- a/napi.h +++ b/napi.h @@ -19,6 +19,9 @@ #endif // NAPI_HAS_THREADS #include #include +#if __cplusplus >= 201703L +#include +#endif // VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known // good version) @@ -718,6 +721,13 @@ class String : public Name { const std::u16string& value ///< UTF-16 encoded C++ string ); +#if __cplusplus >= 201703L + /// Creates a new String value from a UTF-8 encoded C++ string view. + static String New(napi_env env, ///< Node-API environment + std::string_view value ///< UTF-8 encoded C++ string view + ); +#endif + /// Creates a new String value from a UTF-8 encoded C string. static String New( napi_env env, ///< Node-API environment @@ -791,6 +801,15 @@ class Symbol : public Name { description ///< UTF-8 encoded C++ string describing the symbol ); +#if __cplusplus >= 201703L + /// Creates a new Symbol value with a description. + static Symbol New( + napi_env env, ///< Node-API environment + std::string_view + description ///< UTF-8 encoded C++ string view describing the symbol + ); +#endif + /// Creates a new Symbol value with a description. static Symbol New(napi_env env, ///< Node-API environment String description ///< String value describing the symbol