2 Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
3 Copyright (C) 2016 Nore, Nathanaƫl 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
, const SColor
&initial_color
)
70 SColor
color(initial_color
);
71 bool use_default
= (m_default_length
== m_string
.size() &&
72 color
== m_default_color
);
75 while (i
< s
.length()) {
76 if (s
[i
] != L
'\x1b') {
78 m_colors
.push_back(color
);
83 size_t start_index
= i
;
85 if (i
== s
.length()) {
91 while (i
< s
.length() && s
[i
] != L
')') {
97 length
= i
- start_index
;
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) {
109 parseColorString(wide_to_utf8(parts
[1]), color
, true);
111 // No longer use default color after first escape
113 m_default_length
= m_string
.size();
116 } else if (parts
[0] == L
"b") {
117 if (parts
.size() < 2) {
120 parseColorString(wide_to_utf8(parts
[1]), m_background
, true);
121 m_has_background
= true;
125 // Update if no escape character was found
127 m_default_length
= m_string
.size();
130 void EnrichedString::addChar(const EnrichedString
&source
, size_t i
)
132 m_string
+= source
.m_string
[i
];
133 m_colors
.push_back(source
.m_colors
[i
]);
136 void EnrichedString::addCharNoColor(wchar_t c
)
139 if (m_colors
.empty()) {
140 m_colors
.emplace_back(m_default_color
);
142 m_colors
.push_back(m_colors
[m_colors
.size() - 1]);
146 EnrichedString
EnrichedString::operator+(const EnrichedString
&other
) const
148 EnrichedString result
= *this;
153 void EnrichedString::operator+=(const EnrichedString
&other
)
155 bool update_default_color
= m_default_length
== m_string
.size();
157 m_string
+= other
.m_string
;
158 m_colors
.insert(m_colors
.end(), other
.m_colors
.begin(), other
.m_colors
.end());
160 if (update_default_color
) {
161 m_default_length
+= other
.m_default_length
;
162 updateDefaultColor();
166 EnrichedString
EnrichedString::substr(size_t pos
, size_t len
) const
168 if (pos
>= m_string
.length())
169 return EnrichedString();
171 if (len
== std::string::npos
|| pos
+ len
> m_string
.length())
172 len
= m_string
.length() - pos
;
175 m_string
.substr(pos
, len
),
176 std::vector
<SColor
>(m_colors
.begin() + pos
, m_colors
.begin() + pos
+ len
)
179 str
.m_has_background
= m_has_background
;
180 str
.m_background
= m_background
;
182 if (pos
< m_default_length
)
183 str
.m_default_length
= std::min(m_default_length
- pos
, str
.size());
184 str
.setDefaultColor(m_default_color
);
188 const wchar_t *EnrichedString::c_str() const
190 return m_string
.c_str();
193 const std::vector
<SColor
> &EnrichedString::getColors() const
198 const std::wstring
&EnrichedString::getString() const
203 void EnrichedString::setDefaultColor(const irr::video::SColor
&color
)
205 m_default_color
= color
;
206 updateDefaultColor();
209 void EnrichedString::updateDefaultColor()
211 sanity_check(m_default_length
<= m_colors
.size());
213 for (size_t i
= 0; i
< m_default_length
; ++i
)
214 m_colors
[i
] = m_default_color
;