Blender V5.3
BLI_ustring.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <atomic>
12
13#include "BLI_fixed_string.hh"
14#include "BLI_hash.hh"
15#include "BLI_string_ref.hh"
16
17namespace blender {
18
19namespace detail {
20
26 std::string str;
28
29 friend bool operator==(const UStringEntry &a, const UStringEntry &b)
30 {
31 return a.str == b.str;
32 }
33
34 friend bool operator==(const UStringEntry &a, const StringRef b)
35 {
36 return a.str == b;
37 }
38};
39
40const blender::detail::UStringEntry &ustring_ensure_entry(StringRef str);
41const blender::detail::UStringEntry &ustring_ensure_entry(const char *str);
42
43} // namespace detail
44
51class UString {
52 public:
53#if defined(_MSC_VER) && !defined(NDEBUG)
54 /* MSVC's extended STL checks do something to std::string where you can't have a global constexpr
55 * without it causing a build error this only happens in debug builds, for details see :
56 * https://github.com/microsoft/STL/issues/4427 */
57 static const inline blender::detail::UStringEntry EMPTY_ENTRY = {"", hash_string("")};
58#else
60#endif
61
62 private:
64
65 explicit UString(const blender::detail::UStringEntry *ptr) : ptr_(ptr) {}
66
67 public:
68 UString() = default;
69 explicit UString(const StringRef str) : ptr_(&blender::detail::ustring_ensure_entry(str)) {}
70
71 template<FixedString FStr> friend UString operator""_ustr();
72
79 {
80 return StringRefNull(ptr_->str);
81 }
82
83 const std::string &string() const
84 {
85 return ptr_->str;
86 }
87
88 const char *c_str() const
89 {
90 return ptr_->str.c_str();
91 }
92
93 friend bool operator==(const UString &a, const UString &b)
94 {
95 return a.ptr_ == b.ptr_;
96 }
97
98 friend bool operator==(const UString &a, const StringRef b)
99 {
100 return a.ref() == b;
101 }
102
104 {
105 return ptr_->hash;
106 }
107
108 int64_t size() const
109 {
110 return ptr_->str.size();
111 }
112
113 bool is_empty() const
114 {
115 return ptr_->str.empty();
116 }
117
118 char operator[](const int64_t i) const
119 {
120 /* Accessing null char at end is allowed too. */
121 BLI_assert(i >= 0 && i <= this->size());
122 return ptr_->str[i];
123 }
124};
125
134template<> struct DefaultHash<UString> {
135 uint64_t operator()(const UString &value) const
136 {
137 return value.hash();
138 }
139
140 constexpr uint64_t operator()(const StringRef value) const
141 {
142 return get_default_hash(value);
143 }
144};
145
153 {
154 return value.hash;
155 }
156
157 constexpr uint64_t operator()(const StringRef value) const
158 {
159 return get_default_hash(value);
160 }
161};
162
171template<FixedString FStr> inline UString operator""_ustr()
172{
173 /* This is a more optimized variant of just doing this:
174 * \code{.cc}
175 * static UString ustr(FStr.data);
176 * return ustr
177 * \endcode
178 *
179 * The goal of the actual implementation is to improve upon performance and binary size compared
180 * to the above. This is possible here we have two pieces of information the compiler can't have:
181 * - The cache is a raw #UStringEntry pointer, so the not-yet-cached check is a plain null
182 * comparison. It never has to dereference a pointer, unlike e.g. comparing hashes or calling
183 * #UString::is_empty.
184 * - It is valid to initialize the static variable more than once and the result will still be
185 * the same because the string does not change. So a double checked lock is not needed.
186 */
187 /* This is initialized to null by default. */
188 static std::atomic<const blender::detail::UStringEntry *> static_entry;
189 const blender::detail::UStringEntry *entry = static_entry.load(std::memory_order_acquire);
190 if (entry == nullptr) [[unlikely]] {
191 entry = &blender::detail::ustring_ensure_entry(FStr.data);
192 static_entry.store(entry, std::memory_order_release);
193 }
194 return UString(entry);
195}
196
200inline std::string_view format_as(UString str)
201{
202 return std::string_view(str.ref());
203}
204
205} // namespace blender
constexpr StringRefNull()
friend bool operator==(const UString &a, const StringRef b)
bool is_empty() const
friend bool operator==(const UString &a, const UString &b)
const std::string & string() const
int64_t size() const
const char * c_str() const
StringRefNull ref() const
uint64_t hash() const
UString()=default
char operator[](const int64_t i) const
UString(const StringRef str)
static constexpr blender::detail::UStringEntry EMPTY_ENTRY
#define str(s)
const blender::detail::UStringEntry & ustring_ensure_entry(StringRef str)
Definition ustring.cc:29
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
std::string_view format_as(StringRef str)
constexpr uint64_t hash_string(StringRef str)
Definition BLI_hash.hh:183
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
uint64_t operator()(const UString &value) const
constexpr uint64_t operator()(const StringRef value) const
constexpr uint64_t operator()(const StringRef value) const
uint64_t operator()(const blender::detail::UStringEntry &value) const
friend bool operator==(const UStringEntry &a, const UStringEntry &b)
friend bool operator==(const UStringEntry &a, const StringRef b)