Skip to content

Commit 626bfe2

Browse files
committed
Add variant of Nan::New<v8::String> which takes a C++17 string_view
1 parent 8eedb5f commit 626bfe2

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

nan.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353

5454
#define NAN_HAS_CPLUSPLUS_17 (__cplusplus >= 201703L)
5555

56+
#ifndef NAN_ENABLE_STRING_VIEW
57+
# define NAN_ENABLE_STRING_VIEW NAN_HAS_CPLUSPLUS_17
58+
#endif
59+
5660
#include <uv.h>
5761
#include <node.h>
5862
#include <node_buffer.h>
@@ -74,6 +78,9 @@
7478
# include <string>
7579
# include <vector>
7680
#endif
81+
#if NAN_ENABLE_STRING_VIEW
82+
# include <string_view>
83+
#endif
7784

7885
// uv helpers
7986
#ifdef UV_VERSION_MAJOR

nan_new.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ struct Factory<v8::String> : MaybeFactoryBase<v8::String> {
161161
static inline return_t New(const char *value, int length = -1);
162162
static inline return_t New(const uint16_t *value, int length = -1);
163163
static inline return_t New(std::string const& value);
164+
#if NAN_ENABLE_STRING_VIEW
165+
static inline return_t New(std::string_view value);
166+
#endif
164167

165168
static inline return_t New(v8::String::ExternalStringResource * value);
166169
static inline return_t New(ExternalOneByteStringResource * value);
@@ -289,6 +292,14 @@ New(double value) {
289292
return New<v8::Number>(value);
290293
}
291294

295+
#if NAN_ENABLE_STRING_VIEW
296+
inline
297+
imp::Factory<v8::String>::return_t
298+
New(std::string_view value) {
299+
return New<v8::String>(value.data(), value.length());
300+
}
301+
#endif
302+
292303
inline
293304
imp::Factory<v8::String>::return_t
294305
New(std::string const& value) { // NOLINT(build/include_what_you_use)

0 commit comments

Comments
 (0)