String.cpp 3.2 KB

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