tinystr.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. www.sourceforge.net/projects/tinyxml
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any
  5. damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any
  7. purpose, including commercial applications, and to alter it and
  8. redistribute it freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must
  10. not claim that you wrote the original software. If you use this
  11. software in a product, an acknowledgment in the product documentation
  12. would be appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and
  14. must not be misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #ifndef TIXML_USE_STL
  19. #include "tinystr.h"
  20. // Error value for find primitive
  21. const TiXmlString::size_type TiXmlString::npos = static_cast< TiXmlString::size_type >(-1);
  22. // Null rep.
  23. TiXmlString::Rep TiXmlString::nullrep_ = { 0, 0, { '\0' } };
  24. void TiXmlString::reserve (size_type cap)
  25. {
  26. if (cap > capacity())
  27. {
  28. TiXmlString tmp;
  29. tmp.init(length(), cap);
  30. memcpy(tmp.start(), data(), length());
  31. swap(tmp);
  32. }
  33. }
  34. TiXmlString& TiXmlString::assign(const char* str, size_type len)
  35. {
  36. size_type cap = capacity();
  37. if (len > cap || cap > 3*(len + 8))
  38. {
  39. TiXmlString tmp;
  40. tmp.init(len);
  41. memcpy(tmp.start(), str, len);
  42. swap(tmp);
  43. }
  44. else
  45. {
  46. memmove(start(), str, len);
  47. set_size(len);
  48. }
  49. return *this;
  50. }
  51. TiXmlString& TiXmlString::append(const char* str, size_type len)
  52. {
  53. size_type newsize = length() + len;
  54. if (newsize > capacity())
  55. {
  56. reserve (newsize + capacity());
  57. }
  58. memmove(finish(), str, len);
  59. set_size(newsize);
  60. return *this;
  61. }
  62. TiXmlString operator + (const TiXmlString & a, const TiXmlString & b)
  63. {
  64. TiXmlString tmp;
  65. tmp.reserve(a.length() + b.length());
  66. tmp += a;
  67. tmp += b;
  68. return tmp;
  69. }
  70. TiXmlString operator + (const TiXmlString & a, const char* b)
  71. {
  72. TiXmlString tmp;
  73. TiXmlString::size_type b_len = static_cast<TiXmlString::size_type>( strlen(b) );
  74. tmp.reserve(a.length() + b_len);
  75. tmp += a;
  76. tmp.append(b, b_len);
  77. return tmp;
  78. }
  79. TiXmlString operator + (const char* a, const TiXmlString & b)
  80. {
  81. TiXmlString tmp;
  82. TiXmlString::size_type a_len = static_cast<TiXmlString::size_type>( strlen(a) );
  83. tmp.reserve(a_len + b.length());
  84. tmp.append(a, a_len);
  85. tmp += b;
  86. return tmp;
  87. }
  88. #endif // TIXML_USE_STL