Skip to content

Commit

Permalink
tools: skip common extensions on js2c
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Mar 25, 2024
1 parent fc02918 commit bae8e4f
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions tools/js2c.cc
Expand Up @@ -7,6 +7,7 @@
#include <functional>
#include <iostream>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -54,7 +55,20 @@ int GetStats(const char* path, std::function<void(const uv_stat_t*)> func) {
return r;
}

bool EndsWith(const std::string& str, std::string_view suffix) noexcept {
size_t suffix_len = suffix.length();
size_t str_len = str.length();
if (str_len < suffix_len) {
return false;
}
return str.compare(str_len - suffix_len, suffix_len, suffix) == 0;
}

bool IsDirectory(const std::string& filename, int* error) {
// Skip .js or .mjs files.
if (EndsWith(filename, ".js") || EndsWith(filename, ".mjs")) {
return false;
}
bool result = false;
*error = GetStats(filename.c_str(), [&](const uv_stat_t* stats) {
result = !!(stats->st_mode & S_IFDIR);
Expand All @@ -72,15 +86,6 @@ size_t GetFileSize(const std::string& filename, int* error) {
return result;
}

bool EndsWith(const std::string& str, std::string_view suffix) {
size_t suffix_len = suffix.length();
size_t str_len = str.length();
if (str_len < suffix_len) {
return false;
}
return str.compare(str_len - suffix_len, suffix_len, suffix) == 0;
}

bool StartsWith(const std::string& str, std::string_view prefix) {
size_t prefix_len = prefix.length();
size_t str_len = str.length();
Expand Down Expand Up @@ -156,13 +161,14 @@ constexpr std::string_view libPrefix = "lib/";
std::set<std::string_view> kAllowedExtensions{
kGypiSuffix, kJsSuffix, kMjsSuffix};

std::string_view HasAllowedExtensions(const std::string& filename) {
std::optional<std::string_view> HasAllowedExtensions(
const std::string& filename) {
for (const auto& ext : kAllowedExtensions) {
if (EndsWith(filename, ext)) {
return ext;
}
}
return {};
return std::nullopt;
}

using Fragment = std::vector<char>;
Expand Down Expand Up @@ -902,9 +908,8 @@ int Main(int argc, char* argv[]) {
} else if (error != 0) {
return 1;
} else { // It's a file.
std::string_view extension = HasAllowedExtensions(file);
if (extension.size() != 0) {
auto it = file_map.insert({std::string(extension), FileList()}).first;
if (auto extension = HasAllowedExtensions(file); extension.has_value()) {
auto it = file_map.insert({std::string(*extension), FileList()}).first;
it->second.push_back(file);
} else {
fprintf(stderr, "Unsupported file: %s\n", file.c_str());
Expand Down

0 comments on commit bae8e4f

Please sign in to comment.