Skip to content

Commit

Permalink
Fix and consolidate wstring conversion utils (flutter#16342)
Browse files Browse the repository at this point in the history
There were two variants of string/wstring conversion utils, one using
codecvt_utf8 and the other using codecvt_utf8_utf16. We want the latter,
since we want to be using UTF-16, not UCS2.
  • Loading branch information
stuartmorgan committed Feb 4, 2020
1 parent 73c5130 commit 1cd8f3b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
6 changes: 3 additions & 3 deletions fml/platform/win/file_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static DWORD GetShareFlags(FilePermission permission) {
}

static DWORD GetFileAttributesForUtf8Path(const char* absolute_path) {
return ::GetFileAttributes(ConvertToWString(absolute_path).c_str());
return ::GetFileAttributes(StringToWideString(absolute_path).c_str());
}

static DWORD GetFileAttributesForUtf8Path(const fml::UniqueFD& base_directory,
Expand Down Expand Up @@ -272,7 +272,7 @@ bool IsFile(const std::string& path) {
}

bool UnlinkDirectory(const char* path) {
if (!::RemoveDirectory(ConvertToWString(path).c_str())) {
if (!::RemoveDirectory(StringToWideString(path).c_str())) {
FML_DLOG(ERROR) << "Could not remove directory: '" << path << "'. "
<< GetLastErrorMessage();
return false;
Expand All @@ -291,7 +291,7 @@ bool UnlinkDirectory(const fml::UniqueFD& base_directory, const char* path) {
}

bool UnlinkFile(const char* path) {
if (!::DeleteFile(ConvertToWString(path).c_str())) {
if (!::DeleteFile(StringToWideString(path).c_str())) {
FML_DLOG(ERROR) << "Could not remove file: '" << path << "'. "
<< GetLastErrorMessage();
return false;
Expand Down
2 changes: 1 addition & 1 deletion fml/platform/win/native_library_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ NativeLibrary::NativeLibrary(const char* path)
return;
}

handle_ = ::LoadLibrary(ConvertToWString(path).c_str());
handle_ = ::LoadLibrary(StringToWideString(path).c_str());
}

NativeLibrary::NativeLibrary(Handle handle, bool close_handle)
Expand Down
11 changes: 1 addition & 10 deletions fml/platform/win/wstring_conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
namespace fml {

using WideStringConvertor =
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>;

inline std::wstring ConvertToWString(const char* path) {
if (path == nullptr) {
return {};
}
std::string path8(path);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> wchar_conv;
return wchar_conv.from_bytes(path8);
}
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>;

inline std::wstring StringToWideString(const std::string& str) {
WideStringConvertor converter;
Expand Down

0 comments on commit 1cd8f3b

Please sign in to comment.