dynamic_string.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "assert.h"
  7. #include "memory.h"
  8. #include "string_utils.h"
  9. #include "array.h"
  10. #include "string_utils.h"
  11. #include "murmur.h"
  12. #include <algorithm>
  13. #include <cstring>
  14. namespace crown
  15. {
  16. ///
  17. class DynamicString
  18. {
  19. public:
  20. DynamicString(Allocator& allocator = default_allocator());
  21. DynamicString(const char* s, Allocator& allocator = default_allocator());
  22. ~DynamicString();
  23. DynamicString& operator+=(const DynamicString& s);
  24. DynamicString& operator+=(const char* s);
  25. DynamicString& operator+=(const char c);
  26. ///
  27. DynamicString& operator=(const DynamicString& s);
  28. DynamicString& operator=(const char* s);
  29. DynamicString& operator=(const char c);
  30. bool operator<(const DynamicString& s) const;
  31. bool operator==(const DynamicString& s) const;
  32. bool operator==(const char* s) const;
  33. // Returns the length of the string.
  34. uint32_t length() const;
  35. /// Removes the leading string @a s.
  36. /// @note
  37. /// The string must start with @a s.
  38. void strip_leading(const char* s);
  39. /// Removes the trailing string @a s.
  40. /// @note
  41. /// The string must end with @a s.
  42. void strip_trailing(const char* s);
  43. /// Returns whether the string starts with the given @a s string.
  44. bool starts_with(const char* s) const;
  45. /// Returns wheterh the string ends with the given @a s string.
  46. bool ends_with(const char* s) const;
  47. /// Returns the string hashed to murmur2_32.
  48. StringId32 to_string_id() const;
  49. ///
  50. const char* c_str() const;
  51. private:
  52. Array<char> _data;
  53. };
  54. inline DynamicString::DynamicString(Allocator& allocator)
  55. : _data(allocator)
  56. {
  57. array::push_back(_data, '\0');
  58. }
  59. inline DynamicString::DynamicString(const char* s, Allocator& allocator)
  60. : _data(allocator)
  61. {
  62. if (s != NULL)
  63. {
  64. array::push(_data, s, strlen(s));
  65. }
  66. array::push_back(_data, '\0');
  67. }
  68. inline DynamicString::~DynamicString()
  69. {
  70. }
  71. inline DynamicString& DynamicString::operator+=(const DynamicString& s)
  72. {
  73. return *this += s.c_str();
  74. }
  75. inline DynamicString& DynamicString::operator+=(const char* s)
  76. {
  77. CE_ASSERT_NOT_NULL(s);
  78. array::pop_back(_data);
  79. array::push(_data, s, strlen(s));
  80. array::push_back(_data, '\0');
  81. return *this;
  82. }
  83. inline DynamicString& DynamicString::operator+=(const char c)
  84. {
  85. array::pop_back(_data);
  86. array::push_back(_data, c);
  87. array::push_back(_data, '\0');
  88. return *this;
  89. }
  90. inline DynamicString& DynamicString::operator=(const DynamicString& s)
  91. {
  92. _data = s._data;
  93. return *this;
  94. }
  95. inline DynamicString& DynamicString::operator=(const char* s)
  96. {
  97. CE_ASSERT_NOT_NULL(s);
  98. array::clear(_data);
  99. array::push(_data, s, strlen(s));
  100. array::push_back(_data, '\0');
  101. return *this;
  102. }
  103. inline DynamicString& DynamicString::operator=(const char c)
  104. {
  105. array::clear(_data);
  106. array::push_back(_data, c);
  107. return *this;
  108. }
  109. inline bool DynamicString::operator<(const DynamicString& s) const
  110. {
  111. return strcmp(c_str(), s.c_str()) < 0;
  112. }
  113. inline bool DynamicString::operator==(const DynamicString& s) const
  114. {
  115. return strcmp(c_str(), s.c_str()) == 0;
  116. }
  117. inline bool DynamicString::operator==(const char* s) const
  118. {
  119. CE_ASSERT_NOT_NULL(s);
  120. return strcmp(c_str(), s) == 0;
  121. }
  122. inline uint32_t DynamicString::length() const
  123. {
  124. return strlen(this->c_str());
  125. }
  126. inline void DynamicString::strip_leading(const char* s)
  127. {
  128. CE_ASSERT_NOT_NULL(s);
  129. CE_ASSERT(starts_with(s), "String does not start with %s", s);
  130. const size_t my_len = strlen(c_str());
  131. const size_t s_len = strlen(s);
  132. memmove(array::begin(_data), array::begin(_data) + s_len, (my_len - s_len));
  133. array::resize(_data, my_len - s_len);
  134. array::push_back(_data, '\0');
  135. }
  136. inline void DynamicString::strip_trailing(const char* s)
  137. {
  138. CE_ASSERT_NOT_NULL(s);
  139. CE_ASSERT(ends_with(s), "String does not end with %s", s);
  140. const size_t my_len = strlen(c_str());
  141. const size_t s_len = strlen(s);
  142. array::resize(_data, my_len - s_len);
  143. array::push_back(_data, '\0');
  144. }
  145. inline bool DynamicString::starts_with(const char* s) const
  146. {
  147. CE_ASSERT_NOT_NULL(s);
  148. return strncmp(c_str(), s, strlen(s)) == 0;
  149. }
  150. inline bool DynamicString::ends_with(const char* s) const
  151. {
  152. CE_ASSERT_NOT_NULL(s);
  153. const size_t my_len = strlen(c_str());
  154. const size_t s_len = strlen(s);
  155. if (my_len >= s_len)
  156. {
  157. return strncmp(array::begin(_data) + (my_len - s_len), s, s_len) == 0;
  158. }
  159. return false;
  160. }
  161. inline StringId32 DynamicString::to_string_id() const
  162. {
  163. return murmur2_32(c_str(), length());
  164. }
  165. inline const char* DynamicString::c_str() const
  166. {
  167. return array::begin(_data);
  168. }
  169. } // namespace crown