String.cpp 6.1 KB

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