String.cpp 3.2 KB

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