Skip to content

Commit

Permalink
src: optimize Dotenv::GetPathFromArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
RedYetiDev committed Aug 7, 2024
1 parent 3ed9f98 commit 7a55485
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,22 @@ using v8::String;

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

while (path != args.end()) {
auto equal_char = path->find('=');

if (equal_char != std::string::npos) {
paths.push_back(path->substr(equal_char + 1));
} else {
auto next_path = std::next(path);

if (next_path == args.end()) {
return paths;
for (size_t i = 0; i < args.size(); ++i) {
const std::string_view arg = args[i];
if (arg == "--") {
break;
}
if (arg.find(flag) == 0) {
if (arg.size() > flag.size() && arg[flag.size()] == '=') {
paths.push_back(std::string(arg.substr(flag.size() + 1)));
} else if (i + 1 < args.size()) {
paths.push_back(args[i + 1]);
++i;
}

paths.push_back(*next_path);
}

path = std::find_if(++path, args.end(), find_match);
}

return paths;
Expand Down

0 comments on commit 7a55485

Please sign in to comment.