as_string.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2009 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. #include "as_config.h"
  24. #include <stdarg.h> // va_list, va_start(), etc
  25. #include <stdlib.h> // strtod(), strtol()
  26. #include <string.h> // some compilers declare memcpy() here
  27. #if !defined(AS_NO_MEMORY_H)
  28. #include <memory.h>
  29. #endif
  30. #include "as_string.h"
  31. asCString::asCString()
  32. {
  33. length = 0;
  34. local[0] = 0;
  35. }
  36. // Copy constructor
  37. asCString::asCString(const asCString &str)
  38. {
  39. length = 0;
  40. local[0] = 0;
  41. Assign(str.AddressOf(), str.length);
  42. }
  43. asCString::asCString(const char *str, size_t len)
  44. {
  45. length = 0;
  46. local[0] = 0;
  47. Assign(str, len);
  48. }
  49. asCString::asCString(const char *str)
  50. {
  51. length = 0;
  52. local[0] = 0;
  53. size_t len = strlen(str);
  54. Assign(str, len);
  55. }
  56. asCString::asCString(char ch)
  57. {
  58. length = 0;
  59. local[0] = 0;
  60. Assign(&ch, 1);
  61. }
  62. asCString::~asCString()
  63. {
  64. if( length > 11 && dynamic )
  65. {
  66. asDELETEARRAY(dynamic);
  67. }
  68. }
  69. char *asCString::AddressOf()
  70. {
  71. if( length <= 11 )
  72. return local;
  73. else
  74. return dynamic;
  75. }
  76. const char *asCString::AddressOf() const
  77. {
  78. if( length <= 11 )
  79. return local;
  80. else
  81. return dynamic;
  82. }
  83. void asCString::SetLength(size_t len)
  84. {
  85. Allocate(len, true);
  86. }
  87. void asCString::Allocate(size_t len, bool keepData)
  88. {
  89. // If we stored the capacity of the dynamically allocated buffer it would be possible
  90. // to save some memory allocations if a string decreases in size then increases again,
  91. // but this would require extra bytes in the string object itself, or a decrease of
  92. // the static buffer, which in turn would mean extra memory is needed. I've tested each
  93. // of these options, and it turned out that the current choice is what best balanced
  94. // the number of allocations against the size of the allocations.
  95. if( len > 11 && len > length )
  96. {
  97. // Allocate a new dynamic buffer if the new one is larger than the old
  98. char *buf = asNEWARRAY(char,len+1);
  99. if( keepData )
  100. {
  101. int l = (int)len < (int)length ? (int)len : (int)length;
  102. memcpy(buf, AddressOf(), l);
  103. }
  104. if( length > 11 )
  105. {
  106. asDELETEARRAY(dynamic);
  107. }
  108. dynamic = buf;
  109. }
  110. else if( len <= 11 && length > 11 )
  111. {
  112. // Free the dynamic buffer, since it is no longer needed
  113. char *buf = dynamic;
  114. if( keepData )
  115. {
  116. memcpy(&local, buf, len);
  117. }
  118. asDELETEARRAY(buf);
  119. }
  120. length = (int)len;
  121. // Make sure the buffer is null terminated
  122. AddressOf()[length] = 0;
  123. }
  124. void asCString::Assign(const char *str, size_t len)
  125. {
  126. Allocate(len, false);
  127. // Copy the string
  128. memcpy(AddressOf(), str, length);
  129. AddressOf()[length] = 0;
  130. }
  131. asCString &asCString::operator =(const char *str)
  132. {
  133. size_t len = str ? strlen(str) : 0;
  134. Assign(str, len);
  135. return *this;
  136. }
  137. asCString &asCString::operator =(const asCString &str)
  138. {
  139. Assign(str.AddressOf(), str.length);
  140. return *this;
  141. }
  142. asCString &asCString::operator =(char ch)
  143. {
  144. Assign(&ch, 1);
  145. return *this;
  146. }
  147. void asCString::Concatenate(const char *str, size_t len)
  148. {
  149. asUINT oldLength = length;
  150. SetLength(length + len);
  151. memcpy(AddressOf() + oldLength, str, len);
  152. AddressOf()[length] = 0;
  153. }
  154. asCString &asCString::operator +=(const char *str)
  155. {
  156. size_t len = strlen(str);
  157. Concatenate(str, len);
  158. return *this;
  159. }
  160. asCString &asCString::operator +=(const asCString &str)
  161. {
  162. Concatenate(str.AddressOf(), str.length);
  163. return *this;
  164. }
  165. asCString &asCString::operator +=(char ch)
  166. {
  167. Concatenate(&ch, 1);
  168. return *this;
  169. }
  170. size_t asCString::GetLength() const
  171. {
  172. return length;
  173. }
  174. // Returns the length
  175. size_t asCString::Format(const char *format, ...)
  176. {
  177. va_list args;
  178. va_start(args, format);
  179. char tmp[256];
  180. int r = asVSNPRINTF(tmp, 255, format, args);
  181. if( r > 0 )
  182. {
  183. Assign(tmp, r);
  184. }
  185. else
  186. {
  187. size_t n = 512;
  188. asCString str; // Use temporary string in case the current buffer is a parameter
  189. str.Allocate(n, false);
  190. while( (r = asVSNPRINTF(str.AddressOf(), n, format, args)) < 0 )
  191. {
  192. n *= 2;
  193. str.Allocate(n, false);
  194. }
  195. Assign(str.AddressOf(), r);
  196. }
  197. va_end(args);
  198. return length;
  199. }
  200. char &asCString::operator [](size_t index)
  201. {
  202. asASSERT(index < length);
  203. return AddressOf()[index];
  204. }
  205. const char &asCString::operator [](size_t index) const
  206. {
  207. asASSERT(index < length);
  208. return AddressOf()[index];
  209. }
  210. asCString asCString::SubString(size_t start, size_t length) const
  211. {
  212. if( start >= GetLength() || length == 0 )
  213. return asCString("");
  214. if( length == (size_t)(-1) ) length = GetLength() - start;
  215. asCString tmp;
  216. tmp.Assign(AddressOf() + start, length);
  217. return tmp;
  218. }
  219. int asCString::Compare(const char *str) const
  220. {
  221. return Compare(str, strlen(str));
  222. }
  223. int asCString::Compare(const asCString &str) const
  224. {
  225. return Compare(str.AddressOf(), str.GetLength());
  226. }
  227. int asCString::Compare(const char *str, size_t len) const
  228. {
  229. if( length == 0 )
  230. {
  231. if( str == 0 || len == 0 ) return 0; // Equal
  232. return 1; // The other string is larger than this
  233. }
  234. if( str == 0 )
  235. {
  236. if( length == 0 )
  237. return 0; // Equal
  238. return -1; // The other string is smaller than this
  239. }
  240. if( len < length )
  241. {
  242. int result = memcmp(AddressOf(), str, len);
  243. if( result == 0 ) return -1; // The other string is smaller than this
  244. return result;
  245. }
  246. int result = memcmp(AddressOf(), str, length);
  247. if( result == 0 && length < len ) return 1; // The other string is larger than this
  248. return result;
  249. }
  250. size_t asCString::RecalculateLength()
  251. {
  252. SetLength(strlen(AddressOf()));
  253. return length;
  254. }
  255. //-----------------------------------------------------------------------------
  256. // Helper functions
  257. bool operator ==(const asCString &a, const char *b)
  258. {
  259. return a.Compare(b) == 0;
  260. }
  261. bool operator !=(const asCString &a, const char *b)
  262. {
  263. return a.Compare(b) != 0;
  264. }
  265. bool operator ==(const asCString &a, const asCString &b)
  266. {
  267. return a.Compare(b) == 0;
  268. }
  269. bool operator !=(const asCString &a, const asCString &b)
  270. {
  271. return a.Compare(b) != 0;
  272. }
  273. bool operator ==(const char *a, const asCString &b)
  274. {
  275. return b.Compare(a) == 0;
  276. }
  277. bool operator !=(const char *a, const asCString &b)
  278. {
  279. return b.Compare(a) != 0;
  280. }
  281. bool operator <(const asCString &a, const asCString &b)
  282. {
  283. return a.Compare(b) < 0;
  284. }
  285. asCString operator +(const asCString &a, const asCString &b)
  286. {
  287. asCString res = a;
  288. res += b;
  289. return res;
  290. }
  291. asCString operator +(const char *a, const asCString &b)
  292. {
  293. asCString res = a;
  294. res += b;
  295. return res;
  296. }
  297. asCString operator +(const asCString &a, const char *b)
  298. {
  299. asCString res = a;
  300. res += b;
  301. return res;
  302. }