string.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #include <kernel/stdio.h>
  3. #include <types/allocator.hpp>
  4. #include <types/types.h>
  5. #include <types/vector.hpp>
  6. #ifdef __cplusplus
  7. namespace types {
  8. template <template <typename _value_type> class Allocator = kernel_allocator>
  9. class string : public types::vector<char, Allocator> {
  10. public:
  11. using inner_vector_type = types::vector<char, Allocator>;
  12. using size_type = typename inner_vector_type::size_type;
  13. static inline constexpr size_type npos = (-1U);
  14. public:
  15. explicit string(size_type capacity = 8)
  16. : inner_vector_type(capacity)
  17. {
  18. this->push_back(0x00);
  19. }
  20. string(const char* str, size_type n = npos)
  21. : string()
  22. {
  23. this->append(str, n);
  24. }
  25. 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. string& append(const string& str)
  36. {
  37. return this->append(str.data());
  38. }
  39. string& append(string&& str)
  40. {
  41. return this->append(str.data());
  42. }
  43. 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. string& operator+=(const char* str)
  51. {
  52. return this->append(str);
  53. }
  54. string& operator+=(const string& str)
  55. {
  56. return this->append(str);
  57. }
  58. string& operator+=(string&& str)
  59. {
  60. return this->append(move(str));
  61. }
  62. bool operator==(const string& rhs) const
  63. {
  64. return strcmp(c_str(), rhs.c_str()) == 0;
  65. }
  66. string substr(size_type pos, size_type n = npos)
  67. {
  68. return string(this->m_arr + pos, n);
  69. }
  70. const char* c_str(void) const noexcept
  71. {
  72. return this->data();
  73. }
  74. void clear(void)
  75. {
  76. inner_vector_type::clear();
  77. this->push_back(0x00);
  78. }
  79. char pop(void)
  80. {
  81. this->pop_back();
  82. auto iter = inner_vector_type::back();
  83. char c = *iter;
  84. *iter = 0x00;
  85. return c;
  86. }
  87. typename inner_vector_type::iterator_type back(void)
  88. {
  89. // TODO: assert
  90. if (this->empty())
  91. return typename inner_vector_type::iterator_type((void*)0xffffffff);
  92. return --inner_vector_type::back();
  93. }
  94. typename inner_vector_type::const_iterator_type back(void) const
  95. {
  96. // TODO: assert
  97. if (this->empty())
  98. return typename inner_vector_type::iterator_type((void*)0xffffffff);
  99. return --inner_vector_type::back();
  100. }
  101. typename inner_vector_type::const_iterator_type cback(void) const
  102. {
  103. // TODO: assert
  104. if (this->empty())
  105. return typename inner_vector_type::iterator_type((void*)0xffffffff);
  106. return --inner_vector_type::cback();
  107. }
  108. };
  109. } // namespace types
  110. #endif