Naohiro19

life_string_view (From YouTube Video「コンテナと文字列の中間インタフェースspanとstring_view - Akira Takahashi」)

May 9th, 2025
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. #pragma once
  2. #include <string_view>
  3. #include <memory>
  4. #include <string>
  5. #include <algorithm>
  6. #include <compare>
  7.  
  8. struct life_string_view {
  9.   std::string_view _data;
  10.   std::shared_ptr<void> _life;
  11.  
  12.   // Constructor
  13.   life_string_view(std::string_view data, std::shared_ptr<void> life)
  14.       : _data(data), _life(std::move(life)) {}
  15.  
  16.   // Factory method
  17.   template <class T>
  18.   static life_string_view allocate(T&& data) {
  19.     auto p = std::make_shared<std::decay_t<T>>(std::forward<T>(data));
  20.     return {std::string_view(*p), p};
  21.   }
  22.  
  23.   // Member types
  24.   using traits_type            = std::string_view::traits_type;
  25.   using value_type             = std::string_view::value_type;
  26.   using pointer                = std::string_view::pointer;
  27.   using const_pointer          = std::string_view::const_pointer;
  28.   using reference              = std::string_view::reference;
  29.   using const_reference        = std::string_view::const_reference;
  30.   using const_iterator         = std::string_view::const_iterator;
  31.   using iterator               = const_iterator;
  32.   using const_reverse_iterator = std::string_view::const_reverse_iterator;
  33.   using reverse_iterator       = const_reverse_iterator;
  34.   using size_type              = std::string_view::size_type;
  35.   using difference_type        = std::string_view::difference_type;
  36.   static constexpr size_type npos = std::string_view::npos;
  37.  
  38.   // Iterators
  39.   const_iterator begin() const noexcept { return _data.begin(); }
  40.   const_iterator end() const noexcept { return _data.end(); }
  41.   const_iterator cbegin() const noexcept { return _data.cbegin(); }
  42.   const_iterator cend() const noexcept { return _data.cend(); }
  43.   const_reverse_iterator rbegin() const noexcept { return _data.rbegin(); }
  44.   const_reverse_iterator rend() const noexcept { return _data.rend(); }
  45.   const_reverse_iterator crbegin() const noexcept { return _data.crbegin(); }
  46.   const_reverse_iterator crend() const noexcept { return _data.crend(); }
  47.  
  48.   // Capacity
  49.   size_type size() const noexcept { return _data.size(); }
  50.   size_type length() const noexcept { return _data.length(); }
  51.   size_type max_size() const noexcept { return _data.max_size(); }
  52.   bool empty() const noexcept { return _data.empty(); }
  53.  
  54.   // Element access
  55.   const_reference operator[](size_type pos) const { return _data[pos]; }
  56.   const_reference at(size_type pos) const { return _data.at(pos); }
  57.   const_reference front() const { return _data.front(); }
  58.   const_reference back() const { return _data.back(); }
  59.   const_pointer data() const noexcept { return _data.data(); }
  60.  
  61.   // Modifiers
  62.   void remove_prefix(size_type n) { _data.remove_prefix(n); }
  63.   void remove_suffix(size_type n) { _data.remove_suffix(n); }
  64.   void swap(life_string_view& other) noexcept {
  65.     std::swap(_data, other._data);
  66.     std::swap(_life, other._life);
  67.   }
  68.  
  69.   // Operations
  70.   size_type copy(char* dest, size_type count, size_type pos = 0) const {
  71.     return _data.copy(dest, count, pos);
  72.   }
  73.   life_string_view substr(size_type pos = 0, size_type count = npos) const {
  74.     return {_data.substr(pos, count), _life};
  75.   }
  76.   int compare(std::string_view sv) const noexcept { return _data.compare(sv); }
  77.  
  78.   // C++20
  79. #if __cpp_lib_starts_ends_with >= 201711L
  80.   bool starts_with(std::string_view sv) const noexcept { return _data.starts_with(sv); }
  81.   bool ends_with(std::string_view sv) const noexcept { return _data.ends_with(sv); }
  82. #endif
  83.  
  84.   // C++23
  85. #if __cpp_lib_string_contains >= 202011L
  86.   bool contains(std::string_view sv) const noexcept { return _data.contains(sv); }
  87. #endif
  88.  
  89.   // Search
  90.   size_type find(std::string_view sv, size_type pos = 0) const noexcept { return _data.find(sv, pos); }
  91.   size_type rfind(std::string_view sv, size_type pos = npos) const noexcept { return _data.rfind(sv, pos); }
  92.   size_type find_first_of(std::string_view sv, size_type pos = 0) const noexcept { return _data.find_first_of(sv, pos); }
  93.   size_type find_last_of(std::string_view sv, size_type pos = npos) const noexcept { return _data.find_last_of(sv, pos); }
  94.   size_type find_first_not_of(std::string_view sv, size_type pos = 0) const noexcept { return _data.find_first_not_of(sv, pos); }
  95.   size_type find_last_not_of(std::string_view sv, size_type pos = npos) const noexcept { return _data.find_last_not_of(sv, pos); }
  96.  
  97.   // Three-way comparison (C++20)
  98. #if __cpp_impl_three_way_comparison >= 201907L
  99.   auto operator<=>(const life_string_view& other) const noexcept = default;
  100. #endif
  101.  
  102.   // Comparison operators
  103.   bool operator==(const life_string_view& other) const noexcept { return _data == other._data; }
  104.   bool operator!=(const life_string_view& other) const noexcept { return _data != other._data; }
  105.   bool operator< (const life_string_view& other) const noexcept { return _data <  other._data; }
  106.   bool operator<=(const life_string_view& other) const noexcept { return _data <= other._data; }
  107.   bool operator> (const life_string_view& other) const noexcept { return _data >  other._data; }
  108.   bool operator>=(const life_string_view& other) const noexcept { return _data >= other._data; }
  109.  
  110.   // Stream output
  111.   friend std::ostream& operator<<(std::ostream& os, const life_string_view& lsv) {
  112.     return os << lsv._data;
  113.   }
  114. };
  115.  
Advertisement
Add Comment
Please, Sign In to add comment