Skip to content

Commit f4130a1

Browse files
author
Mariusz Pasinski
committed
fix: implement proper extension stripping
1 parent 3b30241 commit f4130a1

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

packages/host/cpp/AddonRegistry.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,28 @@ napi_status napi_emplace_named_property_object(napi_env env,
3030
return status;
3131
}
3232

33+
bool endsWith(const std::string &str, const std::string &suffix) {
34+
#if __cplusplus >= 202002L // __cpp_lib_starts_ends_with
35+
return str.ends_with(suffix);
36+
#else
37+
return str.size() >= suffix.size()
38+
&& std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
39+
#endif
40+
}
41+
42+
std::string_view stripSuffix(const std::string_view &str, const std::string_view &suffix) {
43+
if (endsWith(str, suffix)) {
44+
return str.substr(0, str.size() - suffix.size());
45+
} else {
46+
return str;
47+
}
48+
}
49+
3350
void sanitizeLibraryNameInplace(std::string &name) {
3451
#if USING_PATCHED_BABEL_PLUGIN
3552
// Strip the extension (if present)
3653
// NOTE: This is needed when working with updated Babel plugin
37-
if (auto pos = name.find(".node"); std::string::npos != pos) {
38-
name = name.substr(0, pos);
39-
}
54+
name = stripSuffix(name, ".node");
4055
#endif
4156

4257
for (char &c : name) {

0 commit comments

Comments
 (0)