DynamicString.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <algorithm>
  25. #include <cstring>
  26. #include "Assert.h"
  27. #include "Allocator.h"
  28. #include "StringUtils.h"
  29. #include "List.h"
  30. namespace crown
  31. {
  32. ///
  33. class DynamicString
  34. {
  35. public:
  36. DynamicString(Allocator& allocator = default_allocator());
  37. DynamicString(const char* s, Allocator& allocator = default_allocator());
  38. ~DynamicString();
  39. DynamicString& operator+=(DynamicString& s);
  40. DynamicString& operator+=(const char* s);
  41. DynamicString& operator+=(const char c);
  42. ///
  43. DynamicString& operator=(DynamicString& s);
  44. DynamicString& operator=(const char* s);
  45. DynamicString& operator=(const char c);
  46. bool operator==(DynamicString& s);
  47. bool operator==(const char* s);
  48. /// Removes the leading string @a s.
  49. /// @note
  50. /// The string must start with @a s.
  51. void strip_leading(const char* s);
  52. /// Removes the trailing string @a s.
  53. /// @note
  54. /// The string must end with @a s.
  55. void strip_trailing(const char* s);
  56. /// Returns whether the string starts with the given @a s string.
  57. bool starts_with(const char* s);
  58. /// Returns wheterh the string ends with the given @æ s string.
  59. bool ends_with(const char* s);
  60. ///
  61. const char* c_str();
  62. private:
  63. List<char> m_string;
  64. };
  65. //-----------------------------------------------------------------------------
  66. inline DynamicString::DynamicString(Allocator& allocator)
  67. : m_string(allocator)
  68. {
  69. }
  70. //-----------------------------------------------------------------------------
  71. inline DynamicString::DynamicString(const char* s, Allocator& allocator)
  72. : m_string(allocator)
  73. {
  74. if (s != NULL)
  75. {
  76. m_string.push(s, string::strlen(s));
  77. }
  78. }
  79. //-----------------------------------------------------------------------------
  80. inline DynamicString::~DynamicString()
  81. {
  82. }
  83. //-----------------------------------------------------------------------------
  84. inline DynamicString& DynamicString::operator+=(DynamicString& s)
  85. {
  86. const char* tmp = s.c_str();
  87. return *this += tmp;
  88. }
  89. //-----------------------------------------------------------------------------
  90. inline DynamicString& DynamicString::operator+=(const char* s)
  91. {
  92. CE_ASSERT_NOT_NULL(s);
  93. m_string.push(s, string::strlen(s));
  94. return *this;
  95. }
  96. //-----------------------------------------------------------------------------
  97. inline DynamicString& DynamicString::operator+=(const char c)
  98. {
  99. m_string.push_back(c);
  100. return *this;
  101. }
  102. //-----------------------------------------------------------------------------
  103. inline DynamicString& DynamicString::operator=(DynamicString& s)
  104. {
  105. const char* tmp = s.c_str();
  106. return *this = tmp;
  107. }
  108. //-----------------------------------------------------------------------------
  109. inline DynamicString& DynamicString::operator=(const char* s)
  110. {
  111. CE_ASSERT_NOT_NULL(s);
  112. m_string.clear();
  113. m_string.push(s, string::strlen(s));
  114. return *this;
  115. }
  116. //-----------------------------------------------------------------------------
  117. inline DynamicString& DynamicString::operator=(const char c)
  118. {
  119. m_string.clear();
  120. m_string.push_back(c);
  121. return *this;
  122. }
  123. //-----------------------------------------------------------------------------
  124. inline bool DynamicString::operator==(DynamicString& s)
  125. {
  126. return string::strcmp(c_str(), s.c_str()) == 0;
  127. }
  128. //-----------------------------------------------------------------------------
  129. inline bool DynamicString::operator==(const char* s)
  130. {
  131. CE_ASSERT_NOT_NULL(s);
  132. return string::strcmp(c_str(), s) == 0;
  133. }
  134. //-----------------------------------------------------------------------------
  135. inline void DynamicString::strip_leading(const char* s)
  136. {
  137. CE_ASSERT_NOT_NULL(s);
  138. CE_ASSERT(starts_with(s), "String does not start with %s", s);
  139. const size_t my_len = string::strlen(c_str());
  140. const size_t s_len = string::strlen(s);
  141. memmove(m_string.begin(), m_string.begin() + s_len, (my_len - s_len));
  142. m_string.resize(my_len - s_len);
  143. }
  144. //-----------------------------------------------------------------------------
  145. inline void DynamicString::strip_trailing(const char* s)
  146. {
  147. CE_ASSERT_NOT_NULL(s);
  148. CE_ASSERT(ends_with(s), "String does not end with %s", s);
  149. const size_t my_len = string::strlen(c_str());
  150. const size_t s_len = string::strlen(s);
  151. m_string.resize(my_len - s_len);
  152. }
  153. //-----------------------------------------------------------------------------
  154. inline bool DynamicString::starts_with(const char* s)
  155. {
  156. CE_ASSERT_NOT_NULL(s);
  157. return string::strncmp(c_str(), s, string::strlen(s)) == 0;
  158. }
  159. //-----------------------------------------------------------------------------
  160. inline bool DynamicString::ends_with(const char* s)
  161. {
  162. CE_ASSERT_NOT_NULL(s);
  163. const size_t my_len = string::strlen(c_str());
  164. const size_t s_len = string::strlen(s);
  165. if (my_len >= s_len)
  166. {
  167. return string::strncmp(m_string.begin() + (my_len - s_len), s, s_len) == 0;
  168. }
  169. return false;
  170. }
  171. //-----------------------------------------------------------------------------
  172. inline const char* DynamicString::c_str()
  173. {
  174. m_string.push_back('\0');
  175. m_string.pop_back();
  176. return m_string.begin();
  177. }
  178. } // namespace crown