mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-02 16:38:41 +00:00
Avoid copies when working with EnrichedString
This commit is contained in:
parent
fa4529b4f1
commit
19a58745c9
6 changed files with 53 additions and 54 deletions
|
@ -29,7 +29,7 @@ EnrichedString::EnrichedString()
|
|||
clear();
|
||||
}
|
||||
|
||||
EnrichedString::EnrichedString(const std::wstring &string,
|
||||
EnrichedString::EnrichedString(std::wstring_view string,
|
||||
const std::vector<SColor> &colors)
|
||||
{
|
||||
clear();
|
||||
|
@ -37,18 +37,12 @@ EnrichedString::EnrichedString(const std::wstring &string,
|
|||
m_colors = colors;
|
||||
}
|
||||
|
||||
EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
|
||||
EnrichedString::EnrichedString(std::wstring_view s, const SColor &color)
|
||||
{
|
||||
clear();
|
||||
addAtEnd(translate_string(s), color);
|
||||
}
|
||||
|
||||
EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
|
||||
{
|
||||
clear();
|
||||
addAtEnd(translate_string(std::wstring(str)), color);
|
||||
}
|
||||
|
||||
void EnrichedString::clear()
|
||||
{
|
||||
m_string.clear();
|
||||
|
@ -59,19 +53,20 @@ void EnrichedString::clear()
|
|||
m_background = irr::video::SColor(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
EnrichedString &EnrichedString::operator=(const wchar_t *str)
|
||||
EnrichedString &EnrichedString::operator=(std::wstring_view str)
|
||||
{
|
||||
clear();
|
||||
addAtEnd(translate_string(std::wstring(str)), m_default_color);
|
||||
addAtEnd(translate_string(str), m_default_color);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void EnrichedString::addAtEnd(const std::wstring &s, SColor initial_color)
|
||||
void EnrichedString::addAtEnd(std::wstring_view s, SColor initial_color)
|
||||
{
|
||||
SColor color(initial_color);
|
||||
bool use_default = (m_default_length == m_string.size() &&
|
||||
color == m_default_color);
|
||||
|
||||
m_string.reserve(m_string.size() + s.size());
|
||||
m_colors.reserve(m_colors.size() + s.size());
|
||||
|
||||
size_t i = 0;
|
||||
|
@ -142,7 +137,7 @@ void EnrichedString::addCharNoColor(wchar_t c)
|
|||
if (m_colors.empty()) {
|
||||
m_colors.emplace_back(m_default_color);
|
||||
} else {
|
||||
m_colors.push_back(m_colors[m_colors.size() - 1]);
|
||||
m_colors.push_back(m_colors.back());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -203,11 +198,6 @@ EnrichedString EnrichedString::substr(size_t pos, size_t len) const
|
|||
return str;
|
||||
}
|
||||
|
||||
const wchar_t *EnrichedString::c_str() const
|
||||
{
|
||||
return m_string.c_str();
|
||||
}
|
||||
|
||||
const std::vector<SColor> &EnrichedString::getColors() const
|
||||
{
|
||||
return m_colors;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue