enriched_string.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
  3. Copyright (C) 2016 Nore, Nathanaël Courant <nore@mesecons.net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "enriched_string.h"
  17. #include "util/string.h"
  18. #include "log.h"
  19. using namespace irr::video;
  20. EnrichedString::EnrichedString()
  21. {
  22. clear();
  23. }
  24. EnrichedString::EnrichedString(const std::wstring &string,
  25. const std::vector<SColor> &colors):
  26. m_string(string),
  27. m_colors(colors)
  28. {}
  29. EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
  30. {
  31. clear();
  32. addAtEnd(translate_string(s), color);
  33. }
  34. EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
  35. {
  36. clear();
  37. addAtEnd(translate_string(std::wstring(str)), color);
  38. }
  39. void EnrichedString::operator=(const wchar_t *str)
  40. {
  41. clear();
  42. addAtEnd(translate_string(std::wstring(str)), SColor(255, 255, 255, 255));
  43. }
  44. void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color)
  45. {
  46. SColor color(initial_color);
  47. size_t i = 0;
  48. while (i < s.length()) {
  49. if (s[i] != L'\x1b') {
  50. m_string += s[i];
  51. m_colors.push_back(color);
  52. ++i;
  53. continue;
  54. }
  55. ++i;
  56. size_t start_index = i;
  57. size_t length;
  58. if (i == s.length()) {
  59. break;
  60. }
  61. if (s[i] == L'(') {
  62. ++i;
  63. ++start_index;
  64. while (i < s.length() && s[i] != L')') {
  65. if (s[i] == L'\\') {
  66. ++i;
  67. }
  68. ++i;
  69. }
  70. length = i - start_index;
  71. ++i;
  72. } else {
  73. ++i;
  74. length = 1;
  75. }
  76. std::wstring escape_sequence(s, start_index, length);
  77. std::vector<std::wstring> parts = split(escape_sequence, L'@');
  78. if (parts[0] == L"c") {
  79. if (parts.size() < 2) {
  80. continue;
  81. }
  82. parseColorString(wide_to_utf8(parts[1]), color, true);
  83. } else if (parts[0] == L"b") {
  84. if (parts.size() < 2) {
  85. continue;
  86. }
  87. parseColorString(wide_to_utf8(parts[1]), m_background, true);
  88. m_has_background = true;
  89. }
  90. }
  91. }
  92. void EnrichedString::addChar(const EnrichedString &source, size_t i)
  93. {
  94. m_string += source.m_string[i];
  95. m_colors.push_back(source.m_colors[i]);
  96. }
  97. void EnrichedString::addCharNoColor(wchar_t c)
  98. {
  99. m_string += c;
  100. if (m_colors.empty()) {
  101. m_colors.emplace_back(255, 255, 255, 255);
  102. } else {
  103. m_colors.push_back(m_colors[m_colors.size() - 1]);
  104. }
  105. }
  106. EnrichedString EnrichedString::operator+(const EnrichedString &other) const
  107. {
  108. std::vector<SColor> result;
  109. result.insert(result.end(), m_colors.begin(), m_colors.end());
  110. result.insert(result.end(), other.m_colors.begin(), other.m_colors.end());
  111. return EnrichedString(m_string + other.m_string, result);
  112. }
  113. void EnrichedString::operator+=(const EnrichedString &other)
  114. {
  115. m_string += other.m_string;
  116. m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
  117. }
  118. EnrichedString EnrichedString::substr(size_t pos, size_t len) const
  119. {
  120. if (pos == m_string.length()) {
  121. return EnrichedString();
  122. }
  123. if (len == std::string::npos || pos + len > m_string.length()) {
  124. return EnrichedString(
  125. m_string.substr(pos, std::string::npos),
  126. std::vector<SColor>(m_colors.begin() + pos, m_colors.end())
  127. );
  128. }
  129. return EnrichedString(
  130. m_string.substr(pos, len),
  131. std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
  132. );
  133. }
  134. const wchar_t *EnrichedString::c_str() const
  135. {
  136. return m_string.c_str();
  137. }
  138. const std::vector<SColor> &EnrichedString::getColors() const
  139. {
  140. return m_colors;
  141. }
  142. const std::wstring &EnrichedString::getString() const
  143. {
  144. return m_string;
  145. }