String.cpp 3.5 KB

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