String.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "../../Include/RmlUi/Core/StringBase.h"
  31. namespace Rml {
  32. namespace Core {
  33. int RMLUICORE_API RmlUiStringFormatString(StringBase<char>& string, int 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. template <>
  54. StringBase<char>::StringBase(StringBase<char>::size_type max_size, const char* fmt, ...) : value(local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
  55. {
  56. va_list argument_list;
  57. va_start(argument_list, fmt);
  58. RmlUiStringFormatString(*this, (int)max_size, fmt, argument_list);
  59. va_end(argument_list);
  60. }
  61. template <>
  62. int StringBase<char>::FormatString(StringBase<char>::size_type max_size, const char* fmt, ...)
  63. {
  64. va_list argument_list;
  65. va_start(argument_list, fmt);
  66. int length = RmlUiStringFormatString(*this, (int)max_size, fmt, argument_list);
  67. va_end(argument_list);
  68. return length;
  69. }
  70. String operator+(const char* cstring, const String& string)
  71. {
  72. return String(cstring) + string;
  73. }
  74. //#define ENABLE_STRING_TESTS
  75. #ifdef ENABLE_STRING_TESTS
  76. #include <string>
  77. #include "RmlUi/Core/SystemInterface.h"
  78. RMLUICORE_API void StringTests()
  79. {
  80. SystemInterface* sys = Rml::Core::GetSystemInterface();
  81. std::string ss = "test";
  82. String es = "test";
  83. es = "hello";
  84. es.Resize(100);
  85. es.Erase(4);
  86. es.Erase(2,100);
  87. es += "y";
  88. String sub1 = es.Replace("lo", "l");
  89. sub1 = sub1.Replace("h", "!");
  90. RMLUI_ASSERT(sub1 == "!el");
  91. Time start;
  92. {
  93. // Create a few free buffers
  94. String tempstring("buffer");
  95. String tempstring1("buffer1");
  96. String tempstring2("buffer2");
  97. }
  98. start = sys->GetElapsedTime();
  99. for (int i = 0; i < 100000; i++)
  100. {
  101. std::string str("test");
  102. }
  103. printf( "SS Assign Short: %f\n", sys->GetElapsedTime() - start);
  104. start = sys->GetElapsedTime();
  105. for (int i = 0; i < 100000; i++)
  106. {
  107. String str("test");
  108. }
  109. printf( "ES Assign Short: %f\n", sys->GetElapsedTime() - start);
  110. start = sys->GetElapsedTime();
  111. for (int i = 0; i < 100000; i++)
  112. {
  113. std::string str("test this really long string that won't fit in a local buffer");
  114. }
  115. printf( "SS Assign Long: %f\n", sys->GetElapsedTime() - start);
  116. start = sys->GetElapsedTime();
  117. for (int i = 0; i < 100000; i++)
  118. {
  119. String str("test this really long string that won't fit in a local buffer");
  120. }
  121. printf( "ES Assign Long: %f\n", sys->GetElapsedTime() - start);
  122. start = sys->GetElapsedTime();
  123. for (int i = 0; i < 100000; i++)
  124. {
  125. if (ss == "hello")
  126. {
  127. int bob = 10;
  128. }
  129. }
  130. printf( "SS Compare: %f (char*)\n", sys->GetElapsedTime() - start);
  131. ss = "bo1";
  132. std::string oss = ss;
  133. std::string nss = "bob";
  134. start = sys->GetElapsedTime();
  135. for (int i = 0; i < 100000; i++)
  136. {
  137. //if (ss == oss)
  138. {
  139. int bob = 10;
  140. }
  141. if (ss == nss)
  142. {
  143. int bob = 10;
  144. }
  145. }
  146. printf( "SS Compare: %f (std::string)\n", sys->GetElapsedTime() - start);
  147. start = sys->GetElapsedTime();
  148. for (int i = 0; i < 100000; i++)
  149. {
  150. if (es == "hello")
  151. {
  152. int bob = 10;
  153. }
  154. }
  155. printf( "ES Compare: %f (char*)\n", sys->GetElapsedTime() - start);
  156. es = "bo1";
  157. String oes = es;
  158. String nes = "bob";
  159. start = sys->GetElapsedTime();
  160. for (int i = 0; i < 100000; i++)
  161. {
  162. //if (es == oes)
  163. {
  164. int bob = 10;
  165. }
  166. if (nes == oes)
  167. {
  168. int bob = 10;
  169. }
  170. }
  171. printf( "ES Compare: %f (String)\n", sys->GetElapsedTime() - start);
  172. start = sys->GetElapsedTime();
  173. std::string ss_concat = "hello";
  174. for (int i = 0; i < 100000; i++)
  175. {
  176. ss_concat += "y";
  177. }
  178. printf( "SS +=: %f\n", sys->GetElapsedTime() - start);
  179. String es_concat = "hello";
  180. start = sys->GetElapsedTime();
  181. for (int i = 0; i < 100000; i++)
  182. {
  183. if (i == 42)
  184. {
  185. int bob = 10;
  186. }
  187. es_concat += "y";
  188. }
  189. printf( "ES +=: %f\n", sys->GetElapsedTime() - start);
  190. const char* x1 = "bob";
  191. String s;
  192. String t;
  193. String u;
  194. s = "hello";
  195. t = "hell";
  196. u = "hello";
  197. if (s == t)
  198. {
  199. int bob = 10;
  200. }
  201. if (s == u)
  202. {
  203. int bob = 10;
  204. }
  205. t = s + u;
  206. if (t == "hellohello")
  207. {
  208. int bob = 10;
  209. }
  210. if (t == "x")
  211. {
  212. int bob = 10;
  213. }
  214. t += u;
  215. size_t x = s.Find("e");
  216. size_t y = s.Find("z");
  217. String sub = t.Replace("lo", "l");
  218. sub = sub.Replace("h", "!");
  219. sub.FormatString(128, "%s", "hello");
  220. int bob = 10;
  221. }
  222. #endif
  223. }
  224. }
  225. /*namespace std {
  226. RMLUICORE_API size_t hash< String >::operator()(const String& string) const
  227. {
  228. return StringUtilities::FNVHash(string.CString());
  229. }
  230. }*/