Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variant of Nan::New<v8::String> which takes a C++17 string_view #880

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <uv.h>
#include <node.h>
#include <node_buffer.h>
Expand All @@ -72,6 +78,9 @@
# include <string>
# include <vector>
#endif
#if NAN_ENABLE_STRING_VIEW
# include <string_view>
#endif

// uv helpers
#ifdef UV_VERSION_MAJOR
Expand Down
11 changes: 11 additions & 0 deletions nan_new.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ struct Factory<v8::String> : MaybeFactoryBase<v8::String> {
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);
Expand Down Expand Up @@ -289,6 +292,14 @@ New(double value) {
return New<v8::Number>(value);
}

#if NAN_ENABLE_STRING_VIEW
inline
imp::Factory<v8::String>::return_t
New(std::string_view value) {
return New<v8::String>(value.data(), value.length());
}
#endif

inline
imp::Factory<v8::String>::return_t
New(std::string const& value) { // NOLINT(build/include_what_you_use)
Expand Down