String.cpp 6.1 KB

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