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):
36 EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
42 EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
45 addAtEnd(std::wstring(str), color);
48 void EnrichedString::operator=(const wchar_t *str)
51 addAtEnd(std::wstring(str), SColor(255, 255, 255, 255));
54 void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color)
56 SColor color(initial_color);
58 while (i < s.length()) {
59 if (s[i] != L'\x1b') {
61 m_colors.push_back(color);
66 size_t start_index = i;
68 if (i == s.length()) {
74 while (i < s.length() && s[i] != L')') {
80 length = i - start_index;
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) {
92 parseColorString(wide_to_utf8(parts[1]), color, true);
93 } else if (parts[0] == L"b") {
94 if (parts.size() < 2) {
97 parseColorString(wide_to_utf8(parts[1]), m_background, true);
98 m_has_background = true;
104 void EnrichedString::addChar(const EnrichedString &source, size_t i)
106 m_string += source.m_string[i];
107 m_colors.push_back(source.m_colors[i]);
110 void EnrichedString::addCharNoColor(wchar_t c)
113 if (m_colors.empty()) {
114 m_colors.push_back(SColor(255, 255, 255, 255));
116 m_colors.push_back(m_colors[m_colors.size() - 1]);
120 EnrichedString EnrichedString::operator+(const EnrichedString &other) const
122 std::vector<SColor> result;
123 result.insert(result.end(), m_colors.begin(), m_colors.end());
124 result.insert(result.end(), other.m_colors.begin(), other.m_colors.end());
125 return EnrichedString(m_string + other.m_string, result);
128 void EnrichedString::operator+=(const EnrichedString &other)
130 m_string += other.m_string;
131 m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
134 EnrichedString EnrichedString::substr(size_t pos, size_t len) const
136 if (pos == m_string.length()) {
137 return EnrichedString();
139 if (len == std::string::npos || pos + len > m_string.length()) {
140 return EnrichedString(
141 m_string.substr(pos, std::string::npos),
142 std::vector<SColor>(m_colors.begin() + pos, m_colors.end())
145 return EnrichedString(
146 m_string.substr(pos, len),
147 std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
152 const wchar_t *EnrichedString::c_str() const
154 return m_string.c_str();
157 const std::vector<SColor> &EnrichedString::getColors() const
162 const std::wstring &EnrichedString::getString() const