Skip to content

Commit 0add130

Browse files
authored
feat: add String::New overload for string_view (#1706)
* Add String::New overload for string_view * Add Symbol::New with string_view * Add String/Symbol::New with string view unit tests * Add doc for String and Symbol string_view constructors
1 parent 0eafea5 commit 0add130

File tree

6 files changed

+47
-1
lines changed

6 files changed

+47
-1
lines changed

doc/string.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Napi::String::New(napi_env env, const char* value);
6060
Napi::String::New(napi_env env, const char16_t* value);
6161
Napi::String::New(napi_env env, const char* value, size_t length);
6262
Napi::String::New(napi_env env, const char16_t* value, size_t length);
63+
Napi::String::New(napi_env env, std::string_view value);
6364
```
6465
6566
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Value` object.
@@ -68,6 +69,7 @@ Napi::String::New(napi_env env, const char16_t* value, size_t length);
6869
- `std::u16string&` - represents a UTF16-LE string.
6970
- `const char*` - represents a UTF8 string.
7071
- `const char16_t*` - represents a UTF16-LE string.
72+
- `std::string_view` - represents a UTF8 string view.
7173
- `[in] length`: The length of the string (not necessarily null-terminated) in code units.
7274
7375
Returns a new `Napi::String` that represents the passed in C++ string.

doc/symbol.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Returns a new empty `Napi::Symbol`.
1818
```cpp
1919
Napi::Symbol::New(napi_env env, const std::string& description);
2020
Napi::Symbol::New(napi_env env, const char* description);
21+
Napi::Symbol::New(napi_env env, std::string_view description);
2122
Napi::Symbol::New(napi_env env, Napi::String description);
2223
Napi::Symbol::New(napi_env env, napi_value description);
2324
```
@@ -27,6 +28,7 @@ Napi::Symbol::New(napi_env env, napi_value description);
2728
`description` may be any of:
2829
- `std::string&` - UTF8 string description.
2930
- `const char*` - represents a UTF8 string description.
31+
- `std::string_view` - represents a UTF8 string view.
3032
- `String` - Node addon API String description.
3133
- `napi_value` - Node-API `napi_value` description.
3234
@@ -58,4 +60,4 @@ static Napi::Symbol Napi::Symbol::For(napi_env env, napi_value description);
5860
5961
Searches in the global registry for existing symbol with the given name. If the symbol already exist it will be returned, otherwise a new symbol will be created in the registry. It's equivalent to Symbol.for() called from JavaScript.
6062
61-
[`Napi::Name`]: ./name.md
63+
[`Napi::Name`]: ./name.md

napi-inl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#if NAPI_HAS_THREADS
1919
#include <mutex>
2020
#endif // NAPI_HAS_THREADS
21+
#include <string_view>
2122
#include <type_traits>
2223
#include <utility>
2324

@@ -1262,6 +1263,10 @@ inline String String::New(napi_env env, const std::u16string& val) {
12621263
return String::New(env, val.c_str(), val.size());
12631264
}
12641265

1266+
inline String String::New(napi_env env, std::string_view val) {
1267+
return String::New(env, val.data(), val.size());
1268+
}
1269+
12651270
inline String String::New(napi_env env, const char* val) {
12661271
// TODO(@gabrielschulhof) Remove if-statement when core's error handling is
12671272
// available in all supported versions.
@@ -1371,6 +1376,11 @@ inline Symbol Symbol::New(napi_env env, const std::string& description) {
13711376
return Symbol::New(env, descriptionValue);
13721377
}
13731378

1379+
inline Symbol Symbol::New(napi_env env, std::string_view description) {
1380+
napi_value descriptionValue = String::New(env, description);
1381+
return Symbol::New(env, descriptionValue);
1382+
}
1383+
13741384
inline Symbol Symbol::New(napi_env env, String description) {
13751385
napi_value descriptionValue = description;
13761386
return Symbol::New(env, descriptionValue);

napi.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#endif // NAPI_HAS_THREADS
2020
#include <chrono>
2121
#include <string>
22+
#include <string_view>
2223
#include <vector>
2324

2425
// VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known
@@ -725,6 +726,11 @@ class String : public Name {
725726
const std::u16string& value ///< UTF-16 encoded C++ string
726727
);
727728

729+
/// Creates a new String value from a UTF-8 encoded C++ string view.
730+
static String New(napi_env env, ///< Node-API environment
731+
std::string_view value ///< UTF-8 encoded C++ string view
732+
);
733+
728734
/// Creates a new String value from a UTF-8 encoded C string.
729735
static String New(
730736
napi_env env, ///< Node-API environment
@@ -798,6 +804,13 @@ class Symbol : public Name {
798804
description ///< UTF-8 encoded C++ string describing the symbol
799805
);
800806

807+
/// Creates a new Symbol value with a description.
808+
static Symbol New(
809+
napi_env env, ///< Node-API environment
810+
std::string_view
811+
description ///< UTF-8 encoded C++ string view describing the symbol
812+
);
813+
801814
/// Creates a new Symbol value with a description.
802815
static Symbol New(napi_env env, ///< Node-API environment
803816
String description ///< String value describing the symbol

test/name.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "napi.h"
22

3+
#include <string_view>
4+
35
using namespace Napi;
46

57
const char* testValueUtf8 = "123456789";
@@ -43,6 +45,10 @@ Value CreateString(const CallbackInfo& info) {
4345
}
4446
}
4547

48+
Value CreateStringFromStringView(const CallbackInfo& info) {
49+
return String::New(info.Env(), std::string_view("hello1"));
50+
}
51+
4652
Value CheckString(const CallbackInfo& info) {
4753
String value = info[0].As<String>();
4854
String encoding = info[1].As<String>();
@@ -80,6 +86,10 @@ Value CreateSymbol(const CallbackInfo& info) {
8086
}
8187
}
8288

89+
Value CreateSymbolFromStringView(const CallbackInfo& info) {
90+
return Symbol::New(info.Env(), std::string_view("hello2"));
91+
}
92+
8393
Value CheckSymbol(const CallbackInfo& info) {
8494
return Boolean::New(info.Env(), info[0].Type() == napi_symbol);
8595
}
@@ -99,11 +109,15 @@ Object InitName(Env env) {
99109

100110
exports["echoString"] = Function::New(env, EchoString);
101111
exports["createString"] = Function::New(env, CreateString);
112+
exports["createStringFromStringView"] =
113+
Function::New(env, CreateStringFromStringView);
102114
exports["nullStringShouldThrow"] = Function::New(env, NullStringShouldThrow);
103115
exports["nullString16ShouldThrow"] =
104116
Function::New(env, NullString16ShouldThrow);
105117
exports["checkString"] = Function::New(env, CheckString);
106118
exports["createSymbol"] = Function::New(env, CreateSymbol);
119+
exports["createSymbolFromStringView"] =
120+
Function::New(env, CreateSymbolFromStringView);
107121
exports["checkSymbol"] = Function::New(env, CheckSymbol);
108122

109123
return exports;

test/name.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ function test (binding) {
5656
assert.strictEqual(binding.name.echoString(str, 'utf8'), str);
5757
assert.strictEqual(binding.name.echoString(str, 'utf16'), str);
5858
}
59+
60+
assert.strictEqual(binding.name.createStringFromStringView(), 'hello1');
61+
const symFromStringView = binding.name.createSymbolFromStringView();
62+
assert.strictEqual(typeof symFromStringView, 'symbol');
63+
assert.strictEqual(symFromStringView.description, 'hello2');
5964
}

0 commit comments

Comments
 (0)