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