tinystr.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. www.sourceforge.net/projects/tinyxml
  3. Original file by Yves Berquin.
  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 must
  11. not claim that you wrote the original software. If you use this
  12. software in a product, an acknowledgment in the product documentation
  13. 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. */
  19. /*
  20. * THIS FILE WAS ALTERED BY Tyge Løvset, 7. April 2005.
  21. */
  22. #ifndef TIXML_USE_STL
  23. #include "tinystr.h"
  24. // Error value for find primitive
  25. const TiXmlString::size_type TiXmlString::npos = static_cast< TiXmlString::size_type >(-1);
  26. // Null rep.
  27. TiXmlString::Rep TiXmlString::nullrep_ = { 0, 0, '\0' };
  28. void TiXmlString::reserve (size_type cap)
  29. {
  30. if (cap > capacity())
  31. {
  32. TiXmlString tmp;
  33. tmp.init(length(), cap);
  34. memcpy(tmp.start(), data(), length());
  35. swap(tmp);
  36. }
  37. }
  38. TiXmlString& TiXmlString::assign(const char* str, size_type len)
  39. {
  40. size_type cap = capacity();
  41. if (len > cap || cap > 3*(len + 8))
  42. {
  43. TiXmlString tmp;
  44. tmp.init(len);
  45. memcpy(tmp.start(), str, len);
  46. swap(tmp);
  47. }
  48. else
  49. {
  50. memmove(start(), str, len);
  51. set_size(len);
  52. }
  53. return *this;
  54. }
  55. TiXmlString& TiXmlString::append(const char* str, size_type len)
  56. {
  57. size_type newsize = length() + len;
  58. if (newsize > capacity())
  59. {
  60. reserve (newsize + capacity());
  61. }
  62. memmove(finish(), str, len);
  63. set_size(newsize);
  64. return *this;
  65. }
  66. TiXmlString operator + (const TiXmlString & a, const TiXmlString & b)
  67. {
  68. TiXmlString tmp;
  69. tmp.reserve(a.length() + b.length());
  70. tmp += a;
  71. tmp += b;
  72. return tmp;
  73. }
  74. TiXmlString operator + (const TiXmlString & a, const char* b)
  75. {
  76. TiXmlString tmp;
  77. TiXmlString::size_type b_len = static_cast<TiXmlString::size_type>( strlen(b) );
  78. tmp.reserve(a.length() + b_len);
  79. tmp += a;
  80. tmp.append(b, b_len);
  81. return tmp;
  82. }
  83. TiXmlString operator + (const char* a, const TiXmlString & b)
  84. {
  85. TiXmlString tmp;
  86. TiXmlString::size_type a_len = static_cast<TiXmlString::size_type>( strlen(a) );
  87. tmp.reserve(a_len + b.length());
  88. tmp.append(a, a_len);
  89. tmp += b;
  90. return tmp;
  91. }
  92. #endif // TIXML_USE_STL