]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/enriched_string.cpp
Modernize source code: last part (#6285)
[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 "log.h"
23 using namespace irr::video;
24
25 EnrichedString::EnrichedString()
26 {
27         clear();
28 }
29
30 EnrichedString::EnrichedString(const std::wstring &string,
31                 const std::vector<SColor> &colors):
32         m_string(string),
33         m_colors(colors)
34 {}
35
36 EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
37 {
38         clear();
39         addAtEnd(s, color);
40 }
41
42 EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
43 {
44         clear();
45         addAtEnd(std::wstring(str), color);
46 }
47
48 void EnrichedString::operator=(const wchar_t *str)
49 {
50         clear();
51         addAtEnd(std::wstring(str), SColor(255, 255, 255, 255));
52 }
53
54 void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color)
55 {
56         SColor color(initial_color);
57         size_t i = 0;
58         while (i < s.length()) {
59                 if (s[i] != L'\x1b') {
60                         m_string += s[i];
61                         m_colors.push_back(color);
62                         ++i;
63                         continue;
64                 }
65                 ++i;
66                 size_t start_index = i;
67                 size_t length;
68                 if (i == s.length()) {
69                         break;
70                 }
71                 if (s[i] == L'(') {
72                         ++i;
73                         ++start_index;
74                         while (i < s.length() && s[i] != L')') {
75                                 if (s[i] == L'\\') {
76                                         ++i;
77                                 }
78                                 ++i;
79                         }
80                         length = i - start_index;
81                         ++i;
82                 } else {
83                         ++i;
84                         length = 1;
85                 }
86                 std::wstring escape_sequence(s, start_index, length);
87                 std::vector<std::wstring> parts = split(escape_sequence, L'@');
88                 if (parts[0] == L"c") {
89                         if (parts.size() < 2) {
90                                 continue;
91                         }
92                         parseColorString(wide_to_utf8(parts[1]), color, true);
93                 } else if (parts[0] == L"b") {
94                         if (parts.size() < 2) {
95                                 continue;
96                         }
97                         parseColorString(wide_to_utf8(parts[1]), m_background, true);
98                         m_has_background = true;
99                 }
100         }
101 }
102
103 void EnrichedString::addChar(const EnrichedString &source, size_t i)
104 {
105         m_string += source.m_string[i];
106         m_colors.push_back(source.m_colors[i]);
107 }
108
109 void EnrichedString::addCharNoColor(wchar_t c)
110 {
111         m_string += c;
112         if (m_colors.empty()) {
113                 m_colors.emplace_back(255, 255, 255, 255);
114         } else {
115                 m_colors.push_back(m_colors[m_colors.size() - 1]);
116         }
117 }
118
119 EnrichedString EnrichedString::operator+(const EnrichedString &other) const
120 {
121         std::vector<SColor> result;
122         result.insert(result.end(), m_colors.begin(), m_colors.end());
123         result.insert(result.end(), other.m_colors.begin(), other.m_colors.end());
124         return EnrichedString(m_string + other.m_string, result);
125 }
126
127 void EnrichedString::operator+=(const EnrichedString &other)
128 {
129         m_string += other.m_string;
130         m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
131 }
132
133 EnrichedString EnrichedString::substr(size_t pos, size_t len) const
134 {
135         if (pos == m_string.length()) {
136                 return EnrichedString();
137         }
138         if (len == std::string::npos || pos + len > m_string.length()) {
139                 return EnrichedString(
140                         m_string.substr(pos, std::string::npos),
141                         std::vector<SColor>(m_colors.begin() + pos, m_colors.end())
142                 );
143         }
144
145         return EnrichedString(
146                 m_string.substr(pos, len),
147                 std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
148         );
149
150 }
151
152 const wchar_t *EnrichedString::c_str() const
153 {
154         return m_string.c_str();
155 }
156
157 const std::vector<SColor> &EnrichedString::getColors() const
158 {
159         return m_colors;
160 }
161
162 const std::wstring &EnrichedString::getString() const
163 {
164         return m_string;
165 }