Skip to content

Commit

Permalink
Fix sorting by translation on Windows
Browse files Browse the repository at this point in the history
Add ConvertToSortKey() overload that accepts rvalue references, so that
temporary wxString objects, such as GetTranslation() return value, have
their string data copied into the ICU buffer.
  • Loading branch information
vslavik committed Sep 6, 2024
1 parent 2928e38 commit 7f57b47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/cat_sorting.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ class CatalogItemsComparator
}
}

static str::UCharBuffer ConvertToSortKey(const wxString&& a)
{
return std::move(ConvertToSortKey(a).ensure_owned());
}

private:
const Catalog& m_catalog;
SortOrder m_order;
Expand Down
15 changes: 13 additions & 2 deletions src/str_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,17 @@ class UCharBuffer
int32_t capacity() { return m_capacity; }

/// Ensure the buffer has a deep copy of the data, if it's not already owned
void ensure_owned()
UCharBuffer& ensure_owned()
{
if (m_owned)
return;
return *this;
if (m_capacity == -1)
m_capacity = u_strlen(m_data) + 1;
auto copy = new UChar[m_capacity];
memcpy(copy, m_data, m_capacity * sizeof(UChar));
m_data = copy;
m_owned = true;
return *this;
}

private:
Expand Down Expand Up @@ -280,11 +281,21 @@ inline UCharBuffer to_icu(const wxString& str)
return to_icu(str.wx_str());
}

inline UCharBuffer to_icu(const wxString&& str)
{
return std::move(to_icu(str.wx_str()).ensure_owned());
}

inline UCharBuffer to_icu(const std::wstring& str)
{
return to_icu(str.c_str());
}

inline UCharBuffer to_icu(const std::wstring&& str)
{
return std::move(to_icu(str.c_str()).ensure_owned());
}

inline UCharBuffer to_icu(const std::string& str)
{
return to_icu(str.c_str());
Expand Down

0 comments on commit 7f57b47

Please sign in to comment.