Skip to content

Commit 7a55485

Browse files
committed
src: optimize Dotenv::GetPathFromArgs
1 parent 3ed9f98 commit 7a55485

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/node_dotenv.cc

+12-19
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,22 @@ using v8::String;
1313

1414
std::vector<std::string> Dotenv::GetPathFromArgs(
1515
const std::vector<std::string>& args) {
16-
const auto find_match = [](const std::string& arg) {
17-
const std::string_view flag = "--env-file";
18-
return strncmp(arg.c_str(), flag.data(), flag.size()) == 0;
19-
};
16+
const std::string_view flag = "--env-file";
2017
std::vector<std::string> paths;
21-
auto path = std::find_if(args.begin(), args.end(), find_match);
2218

23-
while (path != args.end()) {
24-
auto equal_char = path->find('=');
25-
26-
if (equal_char != std::string::npos) {
27-
paths.push_back(path->substr(equal_char + 1));
28-
} else {
29-
auto next_path = std::next(path);
30-
31-
if (next_path == args.end()) {
32-
return paths;
19+
for (size_t i = 0; i < args.size(); ++i) {
20+
const std::string_view arg = args[i];
21+
if (arg == "--") {
22+
break;
23+
}
24+
if (arg.find(flag) == 0) {
25+
if (arg.size() > flag.size() && arg[flag.size()] == '=') {
26+
paths.push_back(std::string(arg.substr(flag.size() + 1)));
27+
} else if (i + 1 < args.size()) {
28+
paths.push_back(args[i + 1]);
29+
++i;
3330
}
34-
35-
paths.push_back(*next_path);
3631
}
37-
38-
path = std::find_if(++path, args.end(), find_match);
3932
}
4033

4134
return paths;

0 commit comments

Comments
 (0)