CommonLibSSE (Parapets fork)
BSFixedString.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "RE/B/BSStringPool.h"
4 #include "RE/C/CRC.h"
5 
6 namespace RE
7 {
8  namespace detail
9  {
10  template <class CharT>
12  {
13  public:
14  using size_type = std::uint32_t;
15  using value_type = CharT;
16  using pointer = value_type*;
17  using const_pointer = const value_type*;
19  using const_reference = const value_type&;
20 
21  constexpr BSFixedString() noexcept = default;
22 
24  BSFixedString(BSFixedString&& a_rhs) noexcept;
26  BSFixedString(std::basic_string_view<value_type> a_view);
27 
28  template <std::convertible_to<std::basic_string_view<value_type>> T>
29  inline BSFixedString(T&& a_string) :
30  BSFixedString(static_cast<std::basic_string_view<value_type>>(std::forward<T>(a_string)))
31  {
32  }
33 
35 
39  BSFixedString& operator=(std::basic_string_view<value_type> a_view);
40 
41  template <std::convertible_to<std::basic_string_view<value_type>> T>
42  inline BSFixedString& operator=(T&& a_string)
43  {
44  return *this = static_cast<std::basic_string_view<value_type>>(std::forward<T>(a_string));
45  }
46 
47  [[nodiscard]] inline const_reference operator[](size_type a_pos) const noexcept
48  {
49  assert(a_pos < size());
50  return _data[a_pos];
51  }
52 
53  [[nodiscard]] inline const_reference front() const noexcept { return _data[0]; }
54  [[nodiscard]] inline const_reference back() const noexcept { return _data[size() - 1]; }
55 
56  [[nodiscard]] inline const_pointer data() const noexcept
57  {
58  const auto proxy = get_proxy();
59  const auto cstr = proxy ? proxy->template data<value_type>() : nullptr;
60  return cstr ? cstr : EMPTY;
61  }
62 
63  [[nodiscard]] inline const_pointer c_str() const noexcept { return data(); }
64 
65  [[nodiscard]] constexpr operator std::basic_string_view<value_type>() const { return { c_str(), length() }; }
66 
67  [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
68 
69  [[nodiscard]] constexpr size_type size() const noexcept
70  {
71  const auto proxy = get_proxy();
72  return proxy ? proxy->size() : 0;
73  }
74 
75  [[nodiscard]] constexpr size_type length() const noexcept { return size(); }
76 
77  [[nodiscard]] inline friend bool operator==(const BSFixedString& a_lhs, const BSFixedString& a_rhs) noexcept
78  {
79  return a_lhs._data == a_rhs._data || a_lhs.empty() && a_rhs.empty();
80  }
81 
82  [[nodiscard]] inline friend bool operator!=(const BSFixedString& a_lhs, const BSFixedString& a_rhs) noexcept { return !(a_lhs == a_rhs); }
83 
84  [[nodiscard]] inline friend bool operator==(const BSFixedString& a_lhs, std::basic_string_view<value_type> a_rhs)
85  {
86  if (a_lhs.empty() && a_rhs.empty()) {
87  return true;
88  } else if (const auto length = a_lhs.length(); length != a_rhs.length()) {
89  return false;
90  } else {
91  return strncmp(a_lhs.c_str(), a_rhs.data(), length) == 0;
92  }
93  }
94 
95  [[nodiscard]] inline friend bool operator!=(const BSFixedString& a_lhs, std::basic_string_view<value_type> a_rhs) { return !(a_lhs == a_rhs); }
96  [[nodiscard]] inline friend bool operator==(std::basic_string_view<value_type> a_lhs, const BSFixedString& a_rhs) { return a_rhs == a_lhs; }
97  [[nodiscard]] inline friend bool operator!=(std::basic_string_view<value_type> a_lhs, const BSFixedString& a_rhs) { return !(a_lhs == a_rhs); }
98 
99  [[nodiscard]] inline friend bool operator==(const BSFixedString& a_lhs, const_pointer a_rhs) { return a_lhs == std::basic_string_view<value_type>(a_rhs ? a_rhs : EMPTY); }
100  [[nodiscard]] inline friend bool operator!=(const BSFixedString& a_lhs, const_pointer a_rhs) { return !(a_lhs == a_rhs); }
101  [[nodiscard]] inline friend bool operator==(const_pointer a_lhs, const BSFixedString& a_rhs) { return a_rhs == a_lhs; }
102  [[nodiscard]] inline friend bool operator!=(const_pointer a_lhs, const BSFixedString& a_rhs) { return !(a_lhs == a_rhs); }
103 
104  private:
105  [[nodiscard]] static inline int strncmp(const char* a_lhs, const char* a_rhs, std::size_t a_length)
106  {
107  return _strnicmp(a_lhs, a_rhs, a_length);
108  }
109 
110  [[nodiscard]] static inline int strncmp(const wchar_t* a_lhs, const wchar_t* a_rhs, std::size_t a_length)
111  {
112  return _wcsnicmp(a_lhs, a_rhs, a_length);
113  }
114 
115  BSFixedString* ctor(const char* a_data);
116  BSFixedString* ctor(const wchar_t* a_data);
117  BSFixedString* ctor8(const char* a_data);
118  BSFixedString* ctor16(const wchar_t* a_data);
119 
120  [[nodiscard]] inline BSStringPool::Entry* get_proxy() noexcept
121  {
122  return _data ?
123  reinterpret_cast<BSStringPool::Entry*>(const_cast<pointer>(_data)) - 1 :
124  nullptr;
125  }
126 
127  [[nodiscard]] inline const BSStringPool::Entry* get_proxy() const noexcept
128  {
129  return _data ?
130  reinterpret_cast<const BSStringPool::Entry*>(_data) - 1 :
131  nullptr;
132  }
133 
134  void try_acquire();
135  void try_release();
136 
137  static constexpr const value_type EMPTY[]{ 0 };
138 
139  // members
140  const_pointer _data{ nullptr }; // 0
141  };
142 
143  extern template class BSFixedString<char>;
144  extern template class BSFixedString<wchar_t>;
145  }
146 
150 
151  template <class CharT>
153  {
154  public:
155  [[nodiscard]] inline std::uint32_t operator()(const detail::BSFixedString<CharT>& a_key) const noexcept
156  {
157  return BSCRC32_<const void*>()(a_key.data());
158  }
159  };
160 }
Definition: BSFixedString.h:12
constexpr size_type length() const noexcept
Definition: BSFixedString.h:75
friend bool operator==(std::basic_string_view< value_type > a_lhs, const BSFixedString &a_rhs)
Definition: BSFixedString.h:96
value_type & reference
Definition: BSFixedString.h:18
constexpr BSFixedString() noexcept=default
CharT value_type
Definition: BSFixedString.h:15
BSFixedString & operator=(BSFixedString &&a_rhs) noexcept
friend bool operator!=(const BSFixedString &a_lhs, std::basic_string_view< value_type > a_rhs)
Definition: BSFixedString.h:95
const_reference front() const noexcept
Definition: BSFixedString.h:53
friend bool operator!=(const BSFixedString &a_lhs, const BSFixedString &a_rhs) noexcept
Definition: BSFixedString.h:82
friend bool operator!=(std::basic_string_view< value_type > a_lhs, const BSFixedString &a_rhs)
Definition: BSFixedString.h:97
value_type * pointer
Definition: BSFixedString.h:16
friend bool operator==(const BSFixedString &a_lhs, const BSFixedString &a_rhs) noexcept
Definition: BSFixedString.h:77
friend bool operator==(const BSFixedString &a_lhs, const_pointer a_rhs)
Definition: BSFixedString.h:99
friend bool operator==(const_pointer a_lhs, const BSFixedString &a_rhs)
Definition: BSFixedString.h:101
friend bool operator!=(const_pointer a_lhs, const BSFixedString &a_rhs)
Definition: BSFixedString.h:102
BSFixedString & operator=(std::basic_string_view< value_type > a_view)
const_reference back() const noexcept
Definition: BSFixedString.h:54
const_pointer c_str() const noexcept
Definition: BSFixedString.h:63
BSFixedString & operator=(T &&a_string)
Definition: BSFixedString.h:42
BSFixedString & operator=(const_pointer a_string)
const value_type & const_reference
Definition: BSFixedString.h:19
friend bool operator==(const BSFixedString &a_lhs, std::basic_string_view< value_type > a_rhs)
Definition: BSFixedString.h:84
const_reference operator[](size_type a_pos) const noexcept
Definition: BSFixedString.h:47
const_pointer data() const noexcept
Definition: BSFixedString.h:56
const value_type * const_pointer
Definition: BSFixedString.h:17
constexpr bool empty() const noexcept
Definition: BSFixedString.h:67
friend bool operator!=(const BSFixedString &a_lhs, const_pointer a_rhs)
Definition: BSFixedString.h:100
constexpr size_type size() const noexcept
Definition: BSFixedString.h:69
std::uint32_t size_type
Definition: BSFixedString.h:14
BSFixedString & operator=(const BSFixedString &a_rhs)
Definition: AbsorbEffect.h:6
detail::BSFixedString< char > BSFixedString
Definition: BSFixedString.h:147
Definition: NiBinaryStream.h:94
std::uint32_t operator()(const detail::BSFixedString< CharT > &a_key) const noexcept
Definition: BSFixedString.h:155
Definition: CRC.h:72