Skip to content

Commit d181147

Browse files
sam-githubMylesBorins
authored andcommitted
url: const-ify APIs, and pass URL by ref
Fixes warnings by Coverity Scan of inefficiences when passing by value instead of passing by const reference. PR-URL: #15615 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent 4153938 commit d181147

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

src/module_wrap.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ struct file_check {
323323
bool failed = true;
324324
uv_file file = -1;
325325
};
326-
inline const struct file_check check_file(URL search,
326+
inline const struct file_check check_file(const URL& search,
327327
bool close = false,
328328
bool allow_dir = false) {
329329
struct file_check ret;
@@ -349,7 +349,7 @@ inline const struct file_check check_file(URL search,
349349
if (close) uv_fs_close(nullptr, &fs_req, fd, nullptr);
350350
return ret;
351351
}
352-
URL resolve_extensions(URL search, bool check_exact = true) {
352+
URL resolve_extensions(const URL& search, bool check_exact = true) {
353353
if (check_exact) {
354354
auto check = check_file(search, true);
355355
if (!check.failed) {
@@ -365,10 +365,10 @@ URL resolve_extensions(URL search, bool check_exact = true) {
365365
}
366366
return URL("");
367367
}
368-
inline URL resolve_index(URL search) {
368+
inline URL resolve_index(const URL& search) {
369369
return resolve_extensions(URL("index", &search), false);
370370
}
371-
URL resolve_main(URL search) {
371+
URL resolve_main(const URL& search) {
372372
URL pkg("package.json", &search);
373373
auto check = check_file(pkg);
374374
if (!check.failed) {
@@ -402,7 +402,7 @@ URL resolve_main(URL search) {
402402
}
403403
return URL("");
404404
}
405-
URL resolve_module(std::string specifier, URL* base) {
405+
URL resolve_module(std::string specifier, const URL* base) {
406406
URL parent(".", base);
407407
URL dir("");
408408
do {
@@ -427,7 +427,7 @@ URL resolve_module(std::string specifier, URL* base) {
427427
return URL("");
428428
}
429429

430-
URL resolve_directory(URL search, bool read_pkg_json) {
430+
URL resolve_directory(const URL& search, bool read_pkg_json) {
431431
if (read_pkg_json) {
432432
auto main = resolve_main(search);
433433
if (!(main.flags() & URL_FLAGS_FAILED)) return main;
@@ -438,7 +438,7 @@ URL resolve_directory(URL search, bool read_pkg_json) {
438438
} // anonymous namespace
439439

440440

441-
URL Resolve(std::string specifier, URL* base, bool read_pkg_json) {
441+
URL Resolve(std::string specifier, const URL* base, bool read_pkg_json) {
442442
URL pure_url(specifier);
443443
if (!(pure_url.flags() & URL_FLAGS_FAILED)) {
444444
return pure_url;

src/module_wrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace node {
1414
namespace loader {
1515

16-
node::url::URL Resolve(std::string specifier, node::url::URL* base,
16+
node::url::URL Resolve(std::string specifier, const node::url::URL* base,
1717
bool read_pkg_json = false);
1818

1919
class ModuleWrap : public BaseObject {

src/node_url.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,7 @@ static void DomainToUnicode(const FunctionCallbackInfo<Value>& args) {
20812081
v8::NewStringType::kNormal).ToLocalChecked());
20822082
}
20832083

2084-
std::string URL::ToFilePath() {
2084+
std::string URL::ToFilePath() const {
20852085
if (context_.scheme != "file:") {
20862086
return "";
20872087
}
@@ -2102,7 +2102,7 @@ std::string URL::ToFilePath() {
21022102
}
21032103
#endif
21042104
std::string decoded_path;
2105-
for (std::string& part : context_.path) {
2105+
for (const std::string& part : context_.path) {
21062106
std::string decoded;
21072107
PercentDecode(part.c_str(), part.length(), &decoded);
21082108
for (char& ch : decoded) {

src/node_url.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class URL {
154154
return context_.fragment;
155155
}
156156

157-
std::string path() {
157+
std::string path() const {
158158
std::string ret;
159159
for (auto i = context_.path.begin(); i != context_.path.end(); i++) {
160160
ret += '/';
@@ -165,7 +165,7 @@ class URL {
165165

166166
// Get the path of the file: URL in a format consumable by native file system
167167
// APIs. Returns an empty string if something went wrong.
168-
std::string ToFilePath();
168+
std::string ToFilePath() const;
169169

170170
const Local<Value> ToObject(Environment* env) const;
171171

0 commit comments

Comments
 (0)