WString.cpp 4.8 KB

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