Skip to content

Commit

Permalink
Change from to_utf8(wchar_t*, int) -> (wchar_t*, size_t)
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Feb 28, 2024
1 parent 8e11557 commit bf90f67
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
13 changes: 6 additions & 7 deletions base/dll_win32.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Base Library
// Copyright (c) 2020 Igara Studio S.A.
// Copyright (c) 2020-2024 Igara Studio S.A.
// Copyright (c) 2016 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -47,19 +47,18 @@ std::string get_dll_filename(dll lib)
{
std::vector<wchar_t> buf(MAX_PATH);
if (get_dll_filename_wchar(lib, buf) &&
buf.size() > 1) // One char for the null char
return to_utf8(&buf[0], (int)buf.size()-1);
else
return std::string();
buf.size() > 1) { // One char for the null char
return to_utf8(&buf[0], buf.size()-1);
}
return std::string();
}

Version get_dll_version(dll lib)
{
std::vector<wchar_t> buf(MAX_PATH);
if (get_dll_filename_wchar(lib, buf))
return get_file_version(&buf[0]);
else
return Version();
return Version();
}

} // namespace base
8 changes: 4 additions & 4 deletions base/string.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Base Library
// Copyright (c) 2020-2022 Igara Studio S.A.
// Copyright (c) 2020-2024 Igara Studio S.A.
// Copyright (c) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -71,7 +71,7 @@ std::string string_to_upper(const std::string& original)

#ifdef LAF_WINDOWS

std::string to_utf8(const wchar_t* src, const int n)
std::string to_utf8(const wchar_t* src, const size_t n)
{
int required_size =
::WideCharToMultiByte(CP_UTF8, 0,
Expand Down Expand Up @@ -152,13 +152,13 @@ static std::size_t insert_utf8_char(std::string* result, wchar_t chr)
return size;
}

std::string to_utf8(const wchar_t* src, const int n)
std::string to_utf8(const wchar_t* src, const size_t n)
{
// Get required size to reserve a string so string::push_back()
// doesn't need to reallocate its data.
std::size_t required_size = 0;
const auto* p = src;
for (int i=0; i<n; ++i, ++p)
for (size_t i=0; i<n; ++i, ++p)
required_size += insert_utf8_char(nullptr, *p);
if (!required_size)
return "";
Expand Down
6 changes: 3 additions & 3 deletions base/string.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Base Library
// Copyright (c) 2020-2022 Igara Studio S.A.
// Copyright (c) 2020-2024 Igara Studio S.A.
// Copyright (c) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
Expand All @@ -21,10 +21,10 @@ namespace base {
std::string string_to_lower(const std::string& original);
std::string string_to_upper(const std::string& original);

std::string to_utf8(const wchar_t* src, const int n);
std::string to_utf8(const wchar_t* src, size_t n);

inline std::string to_utf8(const std::wstring& widestring) {
return to_utf8(widestring.c_str(), (int)widestring.size());
return to_utf8(widestring.c_str(), widestring.size());
}

std::wstring from_utf8(const std::string& utf8string);
Expand Down

0 comments on commit bf90f67

Please sign in to comment.