]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/enriched_string.cpp
Make Lint Happy
[dragonfireclient.git] / src / util / enriched_string.cpp
1 /*
2 Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
3 Copyright (C) 2016 Nore, NathanaĆ«l Courant <nore@mesecons.net>
4
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.
9
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.
14
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.
18 */
19
20 #include "enriched_string.h"
21 #include "util/string.h"
22 #include "debug.h"
23 #include "log.h"
24
25 using namespace irr::video;
26
27 EnrichedString::EnrichedString()
28 {
29         clear();
30 }
31
32 EnrichedString::EnrichedString(
33                 const std::wstring &string, const std::vector<SColor> &colors)
34 {
35         clear();
36         m_string = string;
37         m_colors = colors;
38 }
39
40 EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
41 {
42         clear();
43         addAtEnd(translate_string(s), color);
44 }
45
46 EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
47 {
48         clear();
49         addAtEnd(translate_string(std::wstring(str)), color);
50 }
51
52 void EnrichedString::clear()
53 {
54         m_string.clear();
55         m_colors.clear();
56         m_has_background = false;
57         m_default_length = 0;
58         m_default_color = irr::video::SColor(255, 255, 255, 255);
59         m_background = irr::video::SColor(0, 0, 0, 0);
60 }
61
62 void EnrichedString::operator=(const wchar_t *str)
63 {
64         clear();
65         addAtEnd(translate_string(std::wstring(str)), m_default_color);
66 }
67
68 void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color)
69 {
70         SColor color(initial_color);
71         bool use_default =
72                         (m_default_length == m_string.size() && color == m_default_color);
73
74         size_t i = 0;
75         while (i < s.length()) {
76                 if (s[i] != L'\x1b') {
77                         m_string += s[i];
78                         m_colors.push_back(color);
79                         ++i;
80                         continue;
81                 }
82                 ++i;
83                 size_t start_index = i;
84                 size_t length;
85                 if (i == s.length()) {
86                         break;
87                 }
88                 if (s[i] == L'(') {
89                         ++i;
90                         ++start_index;
91                         while (i < s.length() && s[i] != L')') {
92                                 if (s[i] == L'\\') {
93                                         ++i;
94                                 }
95                                 ++i;
96                         }
97                         length = i - start_index;
98                         ++i;
99                 } else {
100                         ++i;
101                         length = 1;
102                 }
103                 std::wstring escape_sequence(s, start_index, length);
104                 std::vector<std::wstring> parts = split(escape_sequence, L'@');
105                 if (parts[0] == L"c") {
106                         if (parts.size() < 2) {
107                                 continue;
108                         }
109                         parseColorString(wide_to_utf8(parts[1]), color, true);
110
111                         // No longer use default color after first escape
112                         if (use_default) {
113                                 m_default_length = m_string.size();
114                                 use_default = false;
115                         }
116                 } else if (parts[0] == L"b") {
117                         if (parts.size() < 2) {
118                                 continue;
119                         }
120                         parseColorString(wide_to_utf8(parts[1]), m_background, true);
121                         m_has_background = true;
122                 }
123         }
124
125         // Update if no escape character was found
126         if (use_default)
127                 m_default_length = m_string.size();
128 }
129
130 void EnrichedString::addChar(const EnrichedString &source, size_t i)
131 {
132         m_string += source.m_string[i];
133         m_colors.push_back(source.m_colors[i]);
134 }
135
136 void EnrichedString::addCharNoColor(wchar_t c)
137 {
138         m_string += c;
139         if (m_colors.empty()) {
140                 m_colors.emplace_back(m_default_color);
141         } else {
142                 m_colors.push_back(m_colors[m_colors.size() - 1]);
143         }
144 }
145
146 EnrichedString EnrichedString::operator+(const EnrichedString &other) const
147 {
148         EnrichedString result = *this;
149         result += other;
150         return result;
151 }
152
153 void EnrichedString::operator+=(const EnrichedString &other)
154 {
155         bool update_default_color = m_default_length == m_string.size();
156
157         m_string += other.m_string;
158         m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
159
160         if (update_default_color) {
161                 m_default_length += other.m_default_length;
162                 updateDefaultColor();
163         }
164 }
165
166 EnrichedString EnrichedString::substr(size_t pos, size_t len) const
167 {
168         if (pos >= m_string.length())
169                 return EnrichedString();
170
171         if (len == std::string::npos || pos + len > m_string.length())
172                 len = m_string.length() - pos;
173
174         EnrichedString str(m_string.substr(pos, len),
175                         std::vector<SColor>(m_colors.begin() + pos,
176                                         m_colors.begin() + pos + len));
177
178         str.m_has_background = m_has_background;
179         str.m_background = m_background;
180
181         if (pos < m_default_length)
182                 str.m_default_length = std::min(m_default_length - pos, str.size());
183         str.setDefaultColor(m_default_color);
184         return str;
185 }
186
187 const wchar_t *EnrichedString::c_str() const
188 {
189         return m_string.c_str();
190 }
191
192 const std::vector<SColor> &EnrichedString::getColors() const
193 {
194         return m_colors;
195 }
196
197 const std::wstring &EnrichedString::getString() const
198 {
199         return m_string;
200 }
201
202 void EnrichedString::setDefaultColor(const irr::video::SColor &color)
203 {
204         m_default_color = color;
205         updateDefaultColor();
206 }
207
208 void EnrichedString::updateDefaultColor()
209 {
210         sanity_check(m_default_length <= m_colors.size());
211
212         for (size_t i = 0; i < m_default_length; ++i)
213                 m_colors[i] = m_default_color;
214 }