string.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #pragma once
  2. #include <string.h>
  3. #include <types/allocator.hpp>
  4. #include <types/types.h>
  5. #include <types/vector.hpp>
  6. namespace types {
  7. template <template <typename _value_type> class Allocator = kernel_allocator>
  8. class string : public types::vector<char, Allocator> {
  9. public:
  10. using inner_vector_type = types::vector<char, Allocator>;
  11. using size_type = typename inner_vector_type::size_type;
  12. static inline constexpr size_type npos = (-1U);
  13. public:
  14. explicit constexpr string(size_type capacity = 8)
  15. : inner_vector_type(capacity)
  16. {
  17. this->push_back(0x00);
  18. }
  19. constexpr string(const char* str, size_type n = npos)
  20. : string()
  21. {
  22. this->append(str, n);
  23. }
  24. constexpr string& append(const char* str, size_type n = npos)
  25. {
  26. this->pop_back();
  27. while (n-- && *str != 0x00) {
  28. this->push_back(*str);
  29. ++str;
  30. }
  31. this->push_back(0x00);
  32. return *this;
  33. }
  34. constexpr string& append(const string& str)
  35. {
  36. return this->append(str.data());
  37. }
  38. constexpr string& append(string&& str)
  39. {
  40. return this->append(str.data());
  41. }
  42. constexpr string& operator+=(const char c)
  43. {
  44. this->pop_back();
  45. this->push_back(c);
  46. this->push_back(0x00);
  47. return *this;
  48. }
  49. constexpr string& operator+=(const char* str)
  50. {
  51. return this->append(str);
  52. }
  53. constexpr string& operator+=(const string& str)
  54. {
  55. return this->append(str);
  56. }
  57. constexpr string& operator+=(string&& str)
  58. {
  59. return this->append(move(str));
  60. }
  61. constexpr bool operator==(const string& rhs) const
  62. {
  63. return strcmp(c_str(), rhs.c_str()) == 0;
  64. }
  65. constexpr string substr(size_type pos, size_type n = npos)
  66. {
  67. return string(this->m_arr + pos, n);
  68. }
  69. constexpr const char* c_str(void) const noexcept
  70. {
  71. return this->data();
  72. }
  73. constexpr void clear(void)
  74. {
  75. inner_vector_type::clear();
  76. this->push_back(0x00);
  77. }
  78. constexpr char pop(void)
  79. {
  80. this->pop_back();
  81. auto iter = inner_vector_type::back();
  82. char c = *iter;
  83. *iter = 0x00;
  84. return c;
  85. }
  86. constexpr typename inner_vector_type::iterator_type back(void)
  87. {
  88. assert(!this->empty());
  89. return --inner_vector_type::back();
  90. }
  91. constexpr typename inner_vector_type::const_iterator_type back(void) const
  92. {
  93. assert(!this->empty());
  94. return --inner_vector_type::back();
  95. }
  96. constexpr typename inner_vector_type::const_iterator_type cback(void) const
  97. {
  98. assert(!this->empty());
  99. return --inner_vector_type::cback();
  100. }
  101. };
  102. } // namespace types