]> git.lizzy.rs Git - minetest.git/blob - src/util/enriched_string.cpp
Fix Enter key after creating a new world (#12997)
[minetest.git] / src / util / enriched_string.cpp
1 /*
2 Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
3 Copyright (C) 2016 Nore, NathanaĆ«lle 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(const std::wstring &string,
33                 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, SColor initial_color)
69 {
70         SColor color(initial_color);
71         bool use_default = (m_default_length == m_string.size() &&
72                 color == m_default_color);
73
74         m_colors.reserve(m_colors.size() + s.size());
75
76         size_t i = 0;
77         while (i < s.length()) {
78                 if (s[i] != L'\x1b') {
79                         m_string += s[i];
80                         m_colors.push_back(color);
81                         ++i;
82                         continue;
83                 }
84                 ++i;
85                 size_t start_index = i;
86                 size_t length;
87                 if (i == s.length()) {
88                         break;
89                 }
90                 if (s[i] == L'(') {
91                         ++i;
92                         ++start_index;
93                         while (i < s.length() && s[i] != L')') {
94                                 if (s[i] == L'\\') {
95                                         ++i;
96                                 }
97                                 ++i;
98                         }
99                         length = i - start_index;
100                         ++i;
101                 } else {
102                         ++i;
103                         length = 1;
104                 }
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) {
109                                 continue;
110                         }
111                         parseColorString(wide_to_utf8(parts[1]), color, true);
112
113                         // No longer use default color after first escape
114                         if (use_default) {
115                                 m_default_length = m_string.size();
116                                 use_default = false;
117                         }
118                 } else if (parts[0] == L"b") {
119                         if (parts.size() < 2) {
120                                 continue;
121                         }
122                         parseColorString(wide_to_utf8(parts[1]), m_background, true);
123                         m_has_background = true;
124                 }
125         }
126
127         // Update if no escape character was found
128         if (use_default)
129                 m_default_length = m_string.size();
130 }
131
132 void EnrichedString::addChar(const EnrichedString &source, size_t i)
133 {
134         m_string += source.m_string[i];
135         m_colors.push_back(source.m_colors[i]);
136 }
137
138 void EnrichedString::addCharNoColor(wchar_t c)
139 {
140         m_string += c;
141         if (m_colors.empty()) {
142                 m_colors.emplace_back(m_default_color);
143         } else {
144                 m_colors.push_back(m_colors[m_colors.size() - 1]);
145         }
146 }
147
148 EnrichedString EnrichedString::operator+(const EnrichedString &other) const
149 {
150         EnrichedString result = *this;
151         result += other;
152         return result;
153 }
154
155 void EnrichedString::operator+=(const EnrichedString &other)
156 {
157         bool update_default_color = m_default_length == m_string.size();
158
159         m_string += other.m_string;
160         m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
161
162         if (update_default_color) {
163                 m_default_length += other.m_default_length;
164                 updateDefaultColor();
165         }
166 }
167
168 EnrichedString EnrichedString::substr(size_t pos, size_t len) const
169 {
170         if (pos >= m_string.length())
171                 return EnrichedString();
172
173         if (len == std::string::npos || pos + len > m_string.length())
174                 len = m_string.length() - pos;
175
176         EnrichedString str(
177                 m_string.substr(pos, len),
178                 std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
179         );
180
181         str.m_has_background = m_has_background;
182         str.m_background = m_background;
183
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);
187         return str;
188 }
189
190 const wchar_t *EnrichedString::c_str() const
191 {
192         return m_string.c_str();
193 }
194
195 const std::vector<SColor> &EnrichedString::getColors() const
196 {
197         return m_colors;
198 }
199
200 const std::wstring &EnrichedString::getString() const
201 {
202         return m_string;
203 }
204
205 void EnrichedString::updateDefaultColor()
206 {
207         sanity_check(m_default_length <= m_colors.size());
208
209         for (size_t i = 0; i < m_default_length; ++i)
210                 m_colors[i] = m_default_color;
211 }