2 Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
3 Copyright (C) 2016 Nore, Nathanaƫlle Courant <nore@mesecons.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "enriched_string.h"
21 #include "util/string.h"
25 using namespace irr::video;
27 EnrichedString::EnrichedString()
32 EnrichedString::EnrichedString(const std::wstring &string,
33 const std::vector<SColor> &colors)
40 EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
43 addAtEnd(translate_string(s), color);
46 EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
49 addAtEnd(translate_string(std::wstring(str)), color);
52 void EnrichedString::clear()
56 m_has_background = false;
58 m_default_color = irr::video::SColor(255, 255, 255, 255);
59 m_background = irr::video::SColor(0, 0, 0, 0);
62 void EnrichedString::operator=(const wchar_t *str)
65 addAtEnd(translate_string(std::wstring(str)), m_default_color);
68 void EnrichedString::addAtEnd(const std::wstring &s, SColor initial_color)
70 SColor color(initial_color);
71 bool use_default = (m_default_length == m_string.size() &&
72 color == m_default_color);
74 m_colors.reserve(m_colors.size() + s.size());
77 while (i < s.length()) {
78 if (s[i] != L'\x1b') {
80 m_colors.push_back(color);
85 size_t start_index = i;
87 if (i == s.length()) {
93 while (i < s.length() && s[i] != L')') {
99 length = i - start_index;
105 std::wstring escape_sequence(s, start_index, length);
106 std::vector<std::wstring> parts = split(escape_sequence, L'@');
107 if (parts[0] == L"c") {
108 if (parts.size() < 2) {
111 parseColorString(wide_to_utf8(parts[1]), color, true);
113 // No longer use default color after first escape
115 m_default_length = m_string.size();
118 } else if (parts[0] == L"b") {
119 if (parts.size() < 2) {
122 parseColorString(wide_to_utf8(parts[1]), m_background, true);
123 m_has_background = true;
127 // Update if no escape character was found
129 m_default_length = m_string.size();
132 void EnrichedString::addChar(const EnrichedString &source, size_t i)
134 m_string += source.m_string[i];
135 m_colors.push_back(source.m_colors[i]);
138 void EnrichedString::addCharNoColor(wchar_t c)
141 if (m_colors.empty()) {
142 m_colors.emplace_back(m_default_color);
144 m_colors.push_back(m_colors[m_colors.size() - 1]);
148 EnrichedString EnrichedString::operator+(const EnrichedString &other) const
150 EnrichedString result = *this;
155 void EnrichedString::operator+=(const EnrichedString &other)
157 bool update_default_color = m_default_length == m_string.size();
159 m_string += other.m_string;
160 m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
162 if (update_default_color) {
163 m_default_length += other.m_default_length;
164 updateDefaultColor();
168 EnrichedString EnrichedString::substr(size_t pos, size_t len) const
170 if (pos >= m_string.length())
171 return EnrichedString();
173 if (len == std::string::npos || pos + len > m_string.length())
174 len = m_string.length() - pos;
177 m_string.substr(pos, len),
178 std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
181 str.m_has_background = m_has_background;
182 str.m_background = m_background;
184 if (pos < m_default_length)
185 str.m_default_length = std::min(m_default_length - pos, str.size());
186 str.setDefaultColor(m_default_color);
190 const wchar_t *EnrichedString::c_str() const
192 return m_string.c_str();
195 const std::vector<SColor> &EnrichedString::getColors() const
200 const std::wstring &EnrichedString::getString() const
205 void EnrichedString::updateDefaultColor()
207 sanity_check(m_default_length <= m_colors.size());
209 for (size_t i = 0; i < m_default_length; ++i)
210 m_colors[i] = m_default_color;