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"
23 using namespace irr::video;
25 EnrichedString::EnrichedString()
30 EnrichedString::EnrichedString(const std::wstring &string,
31 const std::vector<SColor> &colors):
34 m_has_background(false)
37 EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
43 EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
46 addAtEnd(std::wstring(str), color);
49 void EnrichedString::operator=(const wchar_t *str)
52 addAtEnd(std::wstring(str), SColor(255, 255, 255, 255));
55 void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color)
57 SColor color(initial_color);
59 while (i < s.length()) {
60 if (s[i] != L'\x1b') {
62 m_colors.push_back(color);
67 size_t start_index = i;
69 if (i == s.length()) {
75 while (i < s.length() && s[i] != L')') {
81 length = i - start_index;
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) {
93 parseColorString(wide_to_utf8(parts[1]), color, true);
94 } else if (parts[0] == L"b") {
95 if (parts.size() < 2) {
98 parseColorString(wide_to_utf8(parts[1]), m_background, true);
99 m_has_background = true;
105 void EnrichedString::addChar(const EnrichedString &source, size_t i)
107 m_string += source.m_string[i];
108 m_colors.push_back(source.m_colors[i]);
111 void EnrichedString::addCharNoColor(wchar_t c)
114 if (m_colors.empty()) {
115 m_colors.push_back(SColor(255, 255, 255, 255));
117 m_colors.push_back(m_colors[m_colors.size() - 1]);
121 EnrichedString EnrichedString::operator+(const EnrichedString &other) const
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);
129 void EnrichedString::operator+=(const EnrichedString &other)
131 m_string += other.m_string;
132 m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
135 EnrichedString EnrichedString::substr(size_t pos, size_t len) const
137 if (pos == m_string.length()) {
138 return EnrichedString();
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())
146 return EnrichedString(
147 m_string.substr(pos, len),
148 std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
153 const wchar_t *EnrichedString::c_str() const
155 return m_string.c_str();
158 const std::vector<SColor> &EnrichedString::getColors() const
163 const std::wstring &EnrichedString::getString() const