String.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/String.h"
  29. #include "../../Include/Rocket/Core/StringBase.h"
  30. #include <codecvt>
  31. #include <algorithm>
  32. namespace Rocket {
  33. namespace Core {
  34. int FormatString(String& string, size_t max_size, const char* format, va_list argument_list)
  35. {
  36. const int INTERNAL_BUFFER_SIZE = 1024;
  37. static char buffer[INTERNAL_BUFFER_SIZE];
  38. char* buffer_ptr = buffer;
  39. if (max_size + 1 > INTERNAL_BUFFER_SIZE)
  40. buffer_ptr = new char[max_size + 1];
  41. int length = vsnprintf(buffer_ptr, max_size, format, argument_list);
  42. buffer_ptr[length >= 0 ? length : max_size] = '\0';
  43. #ifdef ROCKET_DEBUG
  44. if (length == -1)
  45. {
  46. Log::Message(Log::LT_WARNING, "String::sprintf: String truncated to %d bytes when processing %s", max_size, format);
  47. }
  48. #endif
  49. string = buffer_ptr;
  50. if (buffer_ptr != buffer)
  51. delete[] buffer_ptr;
  52. return length;
  53. }
  54. int FormatString(String& string, size_t max_size, const char* format, ...)
  55. {
  56. va_list argument_list;
  57. va_start(argument_list, format);
  58. int result = FormatString(string, (int)max_size, format, argument_list);
  59. va_end(argument_list);
  60. return result;
  61. }
  62. String CreateString(size_t max_size, const char* format, ...)
  63. {
  64. String result;
  65. result.reserve(max_size);
  66. va_list argument_list;
  67. va_start(argument_list, format);
  68. FormatString(result, max_size, format, argument_list);
  69. va_end(argument_list);
  70. return result;
  71. }
  72. String ToLower(const String& string) {
  73. std::string str_lower = string;
  74. std::transform(str_lower.begin(), str_lower.end(), str_lower.begin(), ::tolower);
  75. return str_lower;
  76. }
  77. std::wstring ToWideString(const std::string& str)
  78. {
  79. std::vector< word > result;
  80. StringUtilities::UTF8toUCS2(str, result);
  81. return std::wstring(result.data(), result.size());
  82. //using convert_typeX = std::codecvt_utf8<wchar_t>;
  83. //std::wstring_convert<convert_typeX, wchar_t> converterX;
  84. //return converterX.from_bytes(str);
  85. }
  86. std::string ToUTF8(const std::wstring& wstr)
  87. {
  88. std::string result;
  89. StringUtilities::UCS2toUTF8(wstr.c_str(), wstr.size(), result);
  90. return result;
  91. //using convert_typeX = std::codecvt_utf8<wchar_t>;
  92. //std::wstring_convert<convert_typeX, wchar_t> converterX;
  93. //return converterX.to_bytes(wstr);
  94. }
  95. std::string Replace(std::string subject, const std::string& search, const std::string& replace)
  96. {
  97. size_t pos = 0;
  98. while ((pos = subject.find(search, pos)) != std::string::npos) {
  99. subject.replace(pos, search.length(), replace);
  100. pos += replace.length();
  101. }
  102. return subject;
  103. }
  104. }
  105. }
  106. /*namespace std {
  107. ROCKETCORE_API size_t hash< String >::operator()(const String& string) const
  108. {
  109. return StringUtilities::FNVHash(string.c_str());
  110. }
  111. }*/