string.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. constexpr string()
  15. : inner_vector_type()
  16. {
  17. this->reserve(8);
  18. this->push_back(0x00);
  19. }
  20. constexpr string(const char* str, size_type n = npos)
  21. : string()
  22. {
  23. this->append(str, n);
  24. }
  25. constexpr string& append(const char* str, size_type n = npos)
  26. {
  27. this->pop_back();
  28. while (n-- && *str != 0x00) {
  29. this->push_back(*str);
  30. ++str;
  31. }
  32. this->push_back(0x00);
  33. return *this;
  34. }
  35. constexpr string& append(const string& str)
  36. {
  37. return this->append(str.data());
  38. }
  39. constexpr string& append(string&& str)
  40. {
  41. return this->append(str.data());
  42. }
  43. constexpr string& operator+=(const char c)
  44. {
  45. this->pop_back();
  46. this->push_back(c);
  47. this->push_back(0x00);
  48. return *this;
  49. }
  50. constexpr string& operator+=(const char* str)
  51. {
  52. return this->append(str);
  53. }
  54. constexpr string& operator+=(const string& str)
  55. {
  56. return this->append(str);
  57. }
  58. constexpr string& operator+=(string&& str)
  59. {
  60. return this->append(move(str));
  61. }
  62. constexpr bool operator==(const string& rhs) const
  63. {
  64. return strcmp(c_str(), rhs.c_str()) == 0;
  65. }
  66. constexpr string substr(size_type pos, size_type n = npos)
  67. {
  68. return string(this->m_arr + pos, n);
  69. }
  70. constexpr const char* c_str(void) const noexcept
  71. {
  72. return this->data();
  73. }
  74. constexpr void clear(void)
  75. {
  76. inner_vector_type::clear();
  77. this->push_back(0x00);
  78. }
  79. constexpr char pop(void)
  80. {
  81. this->pop_back();
  82. auto& ref = inner_vector_type::back();
  83. char c = ref;
  84. ref = 0x00;
  85. return c;
  86. }
  87. constexpr typename inner_vector_type::iterator_type back(void)
  88. {
  89. assert(!this->empty());
  90. return --inner_vector_type::back();
  91. }
  92. constexpr typename inner_vector_type::const_iterator_type back(void) const
  93. {
  94. assert(!this->empty());
  95. return --inner_vector_type::back();
  96. }
  97. constexpr typename inner_vector_type::const_iterator_type cback(void) const
  98. {
  99. assert(!this->empty());
  100. return --inner_vector_type::cback();
  101. }
  102. };
  103. } // namespace types