Skip to content

Commit 93333fe

Browse files
committed
Add String::New overload for string_view
1 parent 86a0524 commit 93333fe

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

napi-inl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#endif // NAPI_HAS_THREADS
2121
#include <type_traits>
2222
#include <utility>
23+
#if __cplusplus >= 201703L
24+
#include <string_view>
25+
#endif
2326

2427
#if defined(__clang__) || defined(__GNUC__)
2528
#define NAPI_NO_SANITIZE_VPTR __attribute__((no_sanitize("vptr")))
@@ -1255,6 +1258,12 @@ inline String String::New(napi_env env, const std::u16string& val) {
12551258
return String::New(env, val.c_str(), val.size());
12561259
}
12571260

1261+
#if __cplusplus >= 201703L
1262+
inline String String::New(napi_env env, const std::string_view& val) {
1263+
return String::New(env, val.data(), val.size());
1264+
}
1265+
#endif
1266+
12581267
inline String String::New(napi_env env, const char* val) {
12591268
// TODO(@gabrielschulhof) Remove if-statement when core's error handling is
12601269
// available in all supported versions.

napi.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#endif // NAPI_HAS_THREADS
2020
#include <string>
2121
#include <vector>
22+
#if __cplusplus >= 201703L
23+
#include <string_view>
24+
#endif
2225

2326
// VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known
2427
// good version)
@@ -718,6 +721,14 @@ class String : public Name {
718721
const std::u16string& value ///< UTF-16 encoded C++ string
719722
);
720723

724+
#if __cplusplus >= 201703L
725+
/// Creates a new String value from a UTF-8 encoded C++ string view.
726+
static String New(
727+
napi_env env, ///< Node-API environment
728+
const std::string_view& value ///< UTF-8 encoded C++ string view
729+
);
730+
#endif
731+
721732
/// Creates a new String value from a UTF-8 encoded C string.
722733
static String New(
723734
napi_env env, ///< Node-API environment

0 commit comments

Comments
 (0)