Skip to content

Commit

Permalink
Add variant of Nan::New<v8::String> which takes a C++17 string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
robinchrist committed Nov 30, 2019
1 parent 8eedb5f commit 626bfe2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <uv.h>
#include <node.h>
#include <node_buffer.h>
Expand All @@ -74,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

0 comments on commit 626bfe2

Please sign in to comment.