DynamicString.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <cstring>
  25. #include "Allocator.h"
  26. #include "StringUtils.h"
  27. #include "List.h"
  28. namespace crown
  29. {
  30. ///
  31. class DynamicString
  32. {
  33. public:
  34. ///
  35. DynamicString(Allocator& allocator);
  36. ///
  37. DynamicString(Allocator& allocator, const char* s);
  38. ///
  39. ~DynamicString();
  40. ///
  41. DynamicString& operator+=(const DynamicString& s);
  42. DynamicString& operator+=(const char* s);
  43. DynamicString& operator+=(const char c);
  44. ///
  45. DynamicString& operator=(const DynamicString& s);
  46. DynamicString& operator=(const char* s);
  47. DynamicString& operator=(const char c);
  48. ///
  49. const char* c_str();
  50. private:
  51. List<char> m_string;
  52. };
  53. //-----------------------------------------------------------------------------
  54. inline DynamicString::DynamicString(Allocator& allocator) :
  55. m_string(allocator)
  56. {
  57. }
  58. //-----------------------------------------------------------------------------
  59. inline DynamicString::DynamicString(Allocator& allocator, const char* s) :
  60. m_string(allocator)
  61. {
  62. m_string.push(s, string::strlen(s));
  63. }
  64. //-----------------------------------------------------------------------------
  65. inline DynamicString::~DynamicString()
  66. {
  67. }
  68. //-----------------------------------------------------------------------------
  69. inline DynamicString& DynamicString::operator+=(const DynamicString& s)
  70. {
  71. const char* tmp = s.m_string.begin();
  72. return *this += tmp;
  73. }
  74. //-----------------------------------------------------------------------------
  75. inline DynamicString& DynamicString::operator+=(const char* s)
  76. {
  77. m_string.push(s, string::strlen(s));
  78. return *this;
  79. }
  80. //-----------------------------------------------------------------------------
  81. inline DynamicString& DynamicString::operator+=(const char c)
  82. {
  83. m_string.push_back(c);
  84. return *this;
  85. }
  86. //-----------------------------------------------------------------------------
  87. inline DynamicString& DynamicString::operator=(const DynamicString& s)
  88. {
  89. const char* tmp = s.m_string.begin();
  90. return *this = tmp;
  91. }
  92. //-----------------------------------------------------------------------------
  93. inline DynamicString& DynamicString::operator=(const char* s)
  94. {
  95. m_string.clear();
  96. m_string.push(s, string::strlen(s));
  97. return *this;
  98. }
  99. //-----------------------------------------------------------------------------
  100. inline DynamicString& DynamicString::operator=(const char c)
  101. {
  102. m_string.clear();
  103. m_string.push_back(c);
  104. return *this;
  105. }
  106. //-----------------------------------------------------------------------------
  107. inline const char* DynamicString::c_str()
  108. {
  109. m_string.push_back('\0');
  110. m_string.pop_back();
  111. return m_string.begin();
  112. }
  113. } // namespace crown