Skip to content

Commit

Permalink
url: reduce unnecessary string copies
Browse files Browse the repository at this point in the history
PR-URL: nodejs#53628
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
anonrig authored and ehsankhfr committed Jul 18, 2024
1 parent ed62035 commit 78a5e6a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ void BindingData::Deserialize(v8::Local<v8::Context> context,

void BindingData::DomainToASCII(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_GE(args.Length(), 1);
CHECK_GE(args.Length(), 1); // input
CHECK(args[0]->IsString());

std::string input = Utf8Value(env->isolate(), args[0]).ToString();
if (input.empty()) {
return args.GetReturnValue().Set(String::Empty(env->isolate()));
Utf8Value input(env->isolate(), args[0]);
if (input.ToStringView().empty()) {
return args.GetReturnValue().SetEmptyString();
}

// It is important to have an initial value that contains a special scheme.
// Since it will change the implementation of `set_hostname` according to URL
// spec.
auto out = ada::parse<ada::url>("ws://x");
DCHECK(out);
if (!out->set_hostname(input)) {
if (!out->set_hostname(input.ToStringView())) {
return args.GetReturnValue().Set(String::Empty(env->isolate()));
}
std::string host = out->get_hostname();
Expand All @@ -99,20 +99,20 @@ void BindingData::DomainToASCII(const FunctionCallbackInfo<Value>& args) {

void BindingData::DomainToUnicode(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_GE(args.Length(), 1);
CHECK_GE(args.Length(), 1); // input
CHECK(args[0]->IsString());

std::string input = Utf8Value(env->isolate(), args[0]).ToString();
if (input.empty()) {
return args.GetReturnValue().Set(String::Empty(env->isolate()));
Utf8Value input(env->isolate(), args[0]);
if (input.ToStringView().empty()) {
return args.GetReturnValue().SetEmptyString();
}

// It is important to have an initial value that contains a special scheme.
// Since it will change the implementation of `set_hostname` according to URL
// spec.
auto out = ada::parse<ada::url>("ws://x");
DCHECK(out);
if (!out->set_hostname(input)) {
if (!out->set_hostname(input.ToStringView())) {
return args.GetReturnValue().Set(String::Empty(env->isolate()));
}
std::string result = ada::unicode::to_unicode(out->get_hostname());
Expand Down

0 comments on commit 78a5e6a

Please sign in to comment.