as_string.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2017 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. #include "as_string_util.h"
  32. asCString::asCString()
  33. {
  34. length = 0;
  35. local[0] = 0;
  36. }
  37. // Copy constructor
  38. asCString::asCString(const asCString &str)
  39. {
  40. length = 0;
  41. local[0] = 0;
  42. Assign(str.AddressOf(), str.length);
  43. }
  44. #ifdef AS_CAN_USE_CPP11
  45. asCString::asCString(asCString &&str)
  46. {
  47. if( str.length <= 11 )
  48. {
  49. length = str.length;
  50. memcpy(local, str.local, length);
  51. local[length] = 0;
  52. }
  53. else
  54. {
  55. dynamic = str.dynamic;
  56. length = str.length;
  57. }
  58. str.dynamic = 0;
  59. str.length = 0;
  60. }
  61. #endif // c++11
  62. asCString::asCString(const char *str, size_t len)
  63. {
  64. length = 0;
  65. local[0] = 0;
  66. Assign(str, len);
  67. }
  68. asCString::asCString(const char *str)
  69. {
  70. length = 0;
  71. local[0] = 0;
  72. size_t len = strlen(str);
  73. Assign(str, len);
  74. }
  75. asCString::asCString(char ch)
  76. {
  77. length = 0;
  78. local[0] = 0;
  79. Assign(&ch, 1);
  80. }
  81. asCString::~asCString()
  82. {
  83. if( length > 11 && dynamic )
  84. {
  85. asDELETEARRAY(dynamic);
  86. }
  87. }
  88. char *asCString::AddressOf()
  89. {
  90. if( length <= 11 )
  91. return local;
  92. else
  93. return dynamic;
  94. }
  95. const char *asCString::AddressOf() const
  96. {
  97. if( length <= 11 )
  98. return local;
  99. else
  100. return dynamic;
  101. }
  102. void asCString::SetLength(size_t len)
  103. {
  104. Allocate(len, true);
  105. }
  106. void asCString::Allocate(size_t len, bool keepData)
  107. {
  108. // If we stored the capacity of the dynamically allocated buffer it would be possible
  109. // to save some memory allocations if a string decreases in size then increases again,
  110. // but this would require extra bytes in the string object itself, or a decrease of
  111. // the static buffer, which in turn would mean extra memory is needed. I've tested each
  112. // of these options, and it turned out that the current choice is what best balanced
  113. // the number of allocations against the size of the allocations.
  114. if( len > 11 && len > length )
  115. {
  116. // Allocate a new dynamic buffer if the new one is larger than the old
  117. char *buf = asNEWARRAY(char,len+1);
  118. if( buf == 0 )
  119. {
  120. // Out of memory. Return without modifying anything
  121. return;
  122. }
  123. if( keepData )
  124. {
  125. int l = (int)len < (int)length ? (int)len : (int)length;
  126. memcpy(buf, AddressOf(), l);
  127. }
  128. if( length > 11 )
  129. {
  130. asDELETEARRAY(dynamic);
  131. }
  132. dynamic = buf;
  133. }
  134. else if( len <= 11 && length > 11 )
  135. {
  136. // Free the dynamic buffer, since it is no longer needed
  137. char *buf = dynamic;
  138. if( keepData )
  139. {
  140. memcpy(&local, buf, len);
  141. }
  142. asDELETEARRAY(buf);
  143. }
  144. length = (int)len;
  145. // Make sure the buffer is null terminated
  146. AddressOf()[length] = 0;
  147. }
  148. void asCString::Assign(const char *str, size_t len)
  149. {
  150. Allocate(len, false);
  151. // Copy the string
  152. memcpy(AddressOf(), str, length);
  153. AddressOf()[length] = 0;
  154. }
  155. asCString &asCString::operator =(const char *str)
  156. {
  157. size_t len = str ? strlen(str) : 0;
  158. Assign(str, len);
  159. return *this;
  160. }
  161. asCString &asCString::operator =(const asCString &str)
  162. {
  163. Assign(str.AddressOf(), str.length);
  164. return *this;
  165. }
  166. #ifdef AS_CAN_USE_CPP11
  167. asCString &asCString::operator =(asCString &&str)
  168. {
  169. if( this != &str )
  170. {
  171. if( length > 11 && dynamic )
  172. {
  173. asDELETEARRAY(dynamic);
  174. }
  175. if ( str.length <= 11 )
  176. {
  177. length = str.length;
  178. memcpy(local, str.local, length);
  179. local[length] = 0;
  180. }
  181. else
  182. {
  183. dynamic = str.dynamic;
  184. length = str.length;
  185. }
  186. str.dynamic = 0;
  187. str.length = 0;
  188. }
  189. return *this;
  190. }
  191. #endif // c++11
  192. asCString &asCString::operator =(char ch)
  193. {
  194. Assign(&ch, 1);
  195. return *this;
  196. }
  197. void asCString::Concatenate(const char *str, size_t len)
  198. {
  199. asUINT oldLength = length;
  200. SetLength(length + len);
  201. memcpy(AddressOf() + oldLength, str, len);
  202. AddressOf()[length] = 0;
  203. }
  204. asCString &asCString::operator +=(const char *str)
  205. {
  206. size_t len = strlen(str);
  207. Concatenate(str, len);
  208. return *this;
  209. }
  210. asCString &asCString::operator +=(const asCString &str)
  211. {
  212. Concatenate(str.AddressOf(), str.length);
  213. return *this;
  214. }
  215. asCString &asCString::operator +=(char ch)
  216. {
  217. Concatenate(&ch, 1);
  218. return *this;
  219. }
  220. size_t asCString::GetLength() const
  221. {
  222. return length;
  223. }
  224. // Returns the length
  225. size_t asCString::Format(const char *format, ...)
  226. {
  227. va_list args;
  228. va_start(args, format);
  229. const size_t startSize = 1024;
  230. char tmp[startSize];
  231. int r = asVSNPRINTF(tmp, startSize-1, format, args);
  232. if( r > 0 && r < int(startSize) )
  233. {
  234. Assign(tmp, r);
  235. }
  236. else
  237. {
  238. // TODO: For some reason this doesn't work properly on Linux. Perhaps the
  239. // problem is related to vsnprintf not keeping the state of va_arg.
  240. // Perhaps I need to rewrite this in some way to keep the state
  241. size_t n = startSize*2;
  242. asCString str; // Use temporary string in case the current buffer is a parameter
  243. str.Allocate(n, false);
  244. while( (r = asVSNPRINTF(str.AddressOf(), n, format, args)) < 0 || r >= int(n) )
  245. {
  246. n *= 2;
  247. str.Allocate(n, false);
  248. }
  249. Assign(str.AddressOf(), r);
  250. }
  251. va_end(args);
  252. return length;
  253. }
  254. char &asCString::operator [](size_t index)
  255. {
  256. asASSERT(index < length);
  257. return AddressOf()[index];
  258. }
  259. const char &asCString::operator [](size_t index) const
  260. {
  261. asASSERT(index < length);
  262. return AddressOf()[index];
  263. }
  264. asCString asCString::SubString(size_t in_start, size_t in_length) const
  265. {
  266. if( in_start >= GetLength() || in_length == 0 )
  267. return asCString("");
  268. if( in_length == (size_t)(-1) ) in_length = GetLength() - in_start;
  269. asCString tmp;
  270. tmp.Assign(AddressOf() + in_start, in_length);
  271. return tmp;
  272. }
  273. int asCString::Compare(const char *str) const
  274. {
  275. return asCompareStrings(AddressOf(), length, str, strlen(str));
  276. }
  277. int asCString::Compare(const asCString &str) const
  278. {
  279. return asCompareStrings(AddressOf(), length, str.AddressOf(), str.GetLength());
  280. }
  281. int asCString::Compare(const char *str, size_t len) const
  282. {
  283. return asCompareStrings(AddressOf(), length, str, len);
  284. }
  285. size_t asCString::RecalculateLength()
  286. {
  287. SetLength(strlen(AddressOf()));
  288. return length;
  289. }
  290. int asCString::FindLast(const char *str, int *count) const
  291. {
  292. // There is no strstr that starts from the end, so
  293. // we'll iterate until we find the last occurrance.
  294. // This shouldn't cause a performance problem because
  295. // it is not expected that this will be done very often,
  296. // and then only on quite short strings anyway.
  297. if( count ) *count = 0;
  298. const char *last = 0;
  299. const char *curr = AddressOf()-1;
  300. while( (curr = strstr(curr+1, str)) != 0 )
  301. {
  302. if( count ) (*count)++;
  303. last = curr;
  304. }
  305. if( last )
  306. return int(last - AddressOf());
  307. return -1;
  308. }
  309. //-----------------------------------------------------------------------------
  310. // Helper functions
  311. bool operator ==(const asCString &a, const char *b)
  312. {
  313. return a.Compare(b) == 0;
  314. }
  315. bool operator !=(const asCString &a, const char *b)
  316. {
  317. return a.Compare(b) != 0;
  318. }
  319. bool operator ==(const asCString &a, const asCString &b)
  320. {
  321. return a.Compare(b) == 0;
  322. }
  323. bool operator !=(const asCString &a, const asCString &b)
  324. {
  325. return a.Compare(b) != 0;
  326. }
  327. bool operator ==(const char *a, const asCString &b)
  328. {
  329. return b.Compare(a) == 0;
  330. }
  331. bool operator !=(const char *a, const asCString &b)
  332. {
  333. return b.Compare(a) != 0;
  334. }
  335. bool operator <(const asCString &a, const asCString &b)
  336. {
  337. return a.Compare(b) < 0;
  338. }
  339. asCString operator +(const asCString &a, const asCString &b)
  340. {
  341. asCString res = a;
  342. res += b;
  343. return res;
  344. }
  345. asCString operator +(const char *a, const asCString &b)
  346. {
  347. asCString res = a;
  348. res += b;
  349. return res;
  350. }
  351. asCString operator +(const asCString &a, const char *b)
  352. {
  353. asCString res = a;
  354. res += b;
  355. return res;
  356. }
  357. // wrapper class
  358. asCStringPointer::asCStringPointer()
  359. : string(0), length(0), cstring(0)
  360. {
  361. }
  362. asCStringPointer::asCStringPointer(const char *str, size_t len)
  363. : string(str), length(len), cstring(0)
  364. {
  365. }
  366. asCStringPointer::asCStringPointer(asCString *cstr)
  367. : string(0), length(0), cstring(cstr)
  368. {
  369. }
  370. const char *asCStringPointer::AddressOf() const
  371. {
  372. return string ? string : cstring->AddressOf();
  373. }
  374. size_t asCStringPointer::GetLength() const
  375. {
  376. return string ? length : cstring->GetLength();
  377. }
  378. bool asCStringPointer::operator==(const asCStringPointer& other) const
  379. {
  380. return asCompareStrings(AddressOf(), GetLength(), other.AddressOf(), other.GetLength()) == 0;
  381. }
  382. bool asCStringPointer::operator<(const asCStringPointer& other) const
  383. {
  384. return asCompareStrings(AddressOf(), GetLength(), other.AddressOf(), other.GetLength()) < 0;
  385. }