WString.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "../../Include/RmlUi/Core/WString.h"
  30. namespace Rml {
  31. namespace Core {
  32. // Constructs an empty string.
  33. WString::WString()
  34. {
  35. }
  36. // Constructs a string as a copy of another UCS-2 string.
  37. WString::WString(const WStringBase& ucs2_string) : WStringBase(ucs2_string)
  38. {
  39. }
  40. // Constructs a string from a sequence of UCS-2 encoded characters.
  41. WString::WString(const word* ucs2_string_begin, const word* ucs2_string_end) : WStringBase(ucs2_string_begin, ucs2_string_end)
  42. {
  43. }
  44. // Constructs a string from multiples of a UCS-2 encoded character.
  45. WString::WString(WStringBase::size_type count, word ucs2_char) : WStringBase(count, ucs2_char)
  46. {
  47. }
  48. // Constructs a string from a sequence of UTF-8 encoded characters.
  49. WString::WString(const char* utf8_string)
  50. {
  51. std::vector< word > ucs2_string;
  52. StringUtilities::UTF8toUCS2(utf8_string, ucs2_string);
  53. if (ucs2_string.size() > 1)
  54. Assign(&ucs2_string[0], &ucs2_string[ucs2_string.size() - 1]);
  55. }
  56. // Constructs a string from a sequence of UTF-8 encoded characters.
  57. WString::WString(const char* utf8_string_begin, const char* utf8_string_end)
  58. {
  59. std::vector< word > ucs2_string;
  60. StringUtilities::UTF8toUCS2(String(utf8_string_begin, utf8_string_end), ucs2_string);
  61. if (ucs2_string.size() > 1)
  62. Assign(&ucs2_string[0], &ucs2_string[ucs2_string.size() - 1]);
  63. }
  64. // Constructs a string from a sequence of UTF-8 encoded characters.
  65. WString::WString(const String& utf8_string)
  66. {
  67. std::vector< word > ucs2_string;
  68. StringUtilities::UTF8toUCS2(utf8_string, ucs2_string);
  69. if (ucs2_string.size() > 1)
  70. Assign(&ucs2_string[0], &ucs2_string[ucs2_string.size() - 1]);
  71. }
  72. // Destroys the string.
  73. WString::~WString()
  74. {
  75. }
  76. // Converts the string to UTF-8 encoding.
  77. String& WString::ToUTF8(String& utf8_string, bool append) const
  78. {
  79. if (!append)
  80. {
  81. utf8_string.Clear();
  82. }
  83. StringUtilities::UCS2toUTF8(CString(), Length(), utf8_string);
  84. return utf8_string;
  85. }
  86. /// Assigns a UCS-2 string to this string.
  87. WString& WString::operator=(const WStringBase& string)
  88. {
  89. WStringBase::operator=(string);
  90. return *this;
  91. }
  92. // Assigns a UCS-2 string to this string.
  93. WString& WString::operator=(const WString& string)
  94. {
  95. WStringBase::operator=(string);
  96. return *this;
  97. }
  98. // Assigns a null-terminated UCS-2 string to this string.
  99. WString& WString::operator=(const word* string)
  100. {
  101. WStringBase::operator=(string);
  102. return *this;
  103. }
  104. // Converts a UTF-8 encoded string into UCS-2 and assigns it to this string.
  105. WString& WString::operator=(const char* string)
  106. {
  107. std::vector< word > ucs2_string;
  108. StringUtilities::UTF8toUCS2(string, ucs2_string);
  109. if (ucs2_string.size() > 1)
  110. Assign(&ucs2_string[0], &ucs2_string[ucs2_string.size() - 1]);
  111. else
  112. Clear();
  113. return *this;
  114. }
  115. // Checks equality between two UCS-2 encoded strings.
  116. bool WString::operator==(const WString& string) const
  117. {
  118. return WStringBase::operator==(string);
  119. }
  120. // Checks equality between this string and another string in UTF-8 encoded.
  121. bool WString::operator==(const char* _string) const
  122. {
  123. return WStringBase::operator==(WString(_string));
  124. }
  125. WStringBase::size_type WString::Find(const WString& s, size_type pos) const
  126. {
  127. return WStringBase::Find(s, pos);
  128. }
  129. WStringBase::size_type WString::Find(const word* s, size_type pos) const
  130. {
  131. return WStringBase::Find(s, pos);
  132. }
  133. WStringBase::size_type WString::Find(const word& s, size_type pos) const
  134. {
  135. word buffer[2] = { s, 0 };
  136. return WStringBase::Find(buffer, pos);
  137. }
  138. word& WString::operator[](size_type offset)
  139. {
  140. return WStringBase::operator[](offset);
  141. }
  142. word WString::operator[](size_type offset) const
  143. {
  144. return WStringBase::operator[](offset);
  145. }
  146. }
  147. }