buffer.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include "base/define.h"
  5. namespace ylib
  6. {
  7. class mem_pool
  8. {
  9. public:
  10. mem_pool(size_t initial_length = 1024);
  11. mem_pool(const mem_pool&) = delete;
  12. mem_pool& operator=(const mem_pool&) = delete;
  13. mem_pool(mem_pool&& other) noexcept;
  14. mem_pool& operator=(mem_pool&& other) noexcept;
  15. ~mem_pool();
  16. void append(const uchar* data,size_t length);
  17. void clear();
  18. size_t length() const;
  19. uchar* data() const;
  20. void resize(size_t length);
  21. void set(const uchar* data,size_t length);
  22. bool empty() const;
  23. private:
  24. void renew(size_t length);
  25. public:
  26. // 内存数据指针
  27. uchar* m_data = nullptr;
  28. // 内存长度
  29. size_t m_length = 0;
  30. // 使用长度
  31. size_t m_use_length = 0;
  32. };
  33. /**
  34. * @brief BUFF字节流
  35. */
  36. class buffer
  37. {
  38. public:
  39. buffer(size_t initial_length = 1024);
  40. buffer(char data, size_t len);
  41. buffer(const char* data,size_t len);
  42. buffer(const ylib::buffer& data);
  43. buffer(ylib::buffer&& data) noexcept;
  44. buffer(const std::string& data);
  45. buffer(std::initializer_list<uchar> char_list);
  46. ~buffer();
  47. void set(const char* data, size_t length);
  48. void set(const ylib::buffer& data);
  49. void append(const std::string& data);
  50. void append(const char* data, size_t length);
  51. void append(const ylib::buffer& data);
  52. void append(std::initializer_list<uchar> char_list);
  53. template<typename T>
  54. void append(const T& value) {
  55. append((const char*)&value, sizeof(T));
  56. }
  57. void clear();
  58. void resize(size_t length);
  59. size_t find(const char* data, size_t len, size_t start_pos = 0) const;
  60. size_t find(const ylib::buffer& data, size_t start_pos = 0) const;
  61. std::vector<size_t> find_list(const ylib::buffer& value, size_t start = 0) const;
  62. std::vector<ylib::buffer> split(const ylib::buffer& value, size_t start = 0) const;
  63. std::string to_string() const;
  64. bool empty() const;
  65. ylib::buffer left(size_t len) const;
  66. ylib::buffer right(size_t len) const;
  67. ylib::buffer sub(size_t start,size_t len) const;
  68. ylib::buffer sub(size_t start) const;
  69. operator std::string() const;
  70. ylib::buffer operator+(const ylib::buffer& other) const;
  71. ylib::buffer& operator=(const std::string& data);
  72. ylib::buffer& operator=(const ylib::buffer& data);
  73. ylib::buffer& operator=(ylib::buffer&& data) noexcept;
  74. unsigned char& operator[](size_t index) const;
  75. const char* data() const;
  76. const size_t length() const;
  77. const size_t size() const { return length(); };
  78. ylib::buffer trim_end(char value);
  79. ylib::buffer trim_begin(char value);
  80. ylib::buffer trim(char value);
  81. std::string to_hex();
  82. size_t& cursor(){ return m_cursor; }
  83. private:
  84. ylib::mem_pool m_data;
  85. size_t m_cursor = 0;
  86. };
  87. }