Skip to content

Commit

Permalink
use string_view for convert to path
Browse files Browse the repository at this point in the history
  • Loading branch information
islandryu committed Nov 18, 2024
1 parent 2ad8502 commit c295818
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3137,15 +3137,15 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, src.ToStringView());

auto src_path = BufferValueToPath(src);
auto src_path = StringViewToPath(src.ToStringView());

BufferValue dest(isolate, args[1]);
CHECK_NOT_NULL(*dest);
ToNamespacedPath(env, &dest);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemWrite, dest.ToStringView());

auto dest_path = BufferValueToPath(dest);
auto dest_path = StringViewToPath(dest.ToStringView());
bool dereference = args[2]->IsTrue();
bool recursive = args[3]->IsTrue();

Expand Down
4 changes: 2 additions & 2 deletions src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void BindingData::GetNearestParentPackageJSON(
bool slashCheck = path_value.ToStringView().ends_with(kPathSeparator);

ToNamespacedPath(realm->env(), &path_value);
std::filesystem::path path = BufferValueToPath(path_value);
std::filesystem::path path = StringViewToPath(path_value.ToStringView());
if (slashCheck) {
path += kPathSeparator;
}
Expand All @@ -361,7 +361,7 @@ void BindingData::GetNearestParentPackageJSONType(
bool slashCheck = path_value.ToStringView().ends_with(kPathSeparator);

ToNamespacedPath(realm->env(), &path_value);
std::filesystem::path path = BufferValueToPath(path_value);
std::filesystem::path path = StringViewToPath(path_value.ToStringView());
if (slashCheck) {
path += kPathSeparator;
}
Expand Down
9 changes: 4 additions & 5 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -886,14 +886,14 @@ v8::Maybe<int> GetValidFileMode(Environment* env,
}

#ifdef _WIN32
std::wstring ConvertToWideString(const std::string& str) {
std::wstring ConvertToWideString(std::string_view strView) {
int size_needed = MultiByteToWideChar(
CP_UTF8, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
CP_UTF8, 0, strView.data(), static_cast<int>(strView.size()), nullptr, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8,
0,
&str[0],
static_cast<int>(str.size()),
strView.data(),
static_cast<int>(strView.size()),
&wstrTo[0],
size_needed);
return wstrTo;
Expand Down Expand Up @@ -921,7 +921,6 @@ std::string ConvertWideToUTF8(const std::wstring& wstr) {
nullptr);
return strTo;
}

#endif // _WIN32

} // namespace node
19 changes: 13 additions & 6 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1019,19 +1019,26 @@ v8::Maybe<int> GetValidFileMode(Environment* env,
inline bool IsWindowsBatchFile(const char* filename);

#ifdef _WIN32
std::wstring ConvertToWideString(const std::string& str);
std::wstring ConvertToWideString(const std::string_view str);

#define BufferValueToPath(str) \
std::filesystem::path(ConvertToWideString(str.ToString()))
inline std::filesystem::path StringViewToPath(std::string_view str) {
return std::filesystem::path(ConvertToWideString(str));
}

std::string ConvertWideToUTF8(const std::wstring& wstr);

#define PathToString(path) ConvertWideToUTF8(path.wstring());
inline std::string PathToString(std::filesystem::path path) {
return ConvertWideToUTF8(path.wstring());
}

#else // _WIN32

#define BufferValueToPath(str) std::filesystem::path(str.ToStringView());
#define PathToString(path) path.native();
inline std::filesystem::path StringViewToPath(std::string_view str) {
return std::filesystem::path(str);
}
inline std::string PathToString(std::filesystem::path path) {
return path.native();
}

#endif // _WIN32

Expand Down

0 comments on commit c295818

Please sign in to comment.