Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Add `String::EMPTY` and `StringName::EMPTY` to save on binary size.

Lukas Tenbrinka3df794427dc
diff --git a/core/object/gdtype.h b/core/object/gdtype.h
index 91806f139e3..1a85764a7b0 100644
--- a/core/object/gdtype.h
+++ b/core/object/gdtype.h
@@ -150,8 +150,7 @@ public:
const GDType *get_super_type() const { return super_type; }
const StringName &get_name() const { return name; }
const StringName &get_super_type_name() const {
- static const StringName EMPTY;
- return super_type ? super_type->name : EMPTY;
+ return super_type ? super_type->name : StringName::EMPTY;
}
const Vector<StringName> &get_name_hierarchy() const { return name_hierarchy; }
diff --git a/core/string/string_name.cpp b/core/string/string_name.cpp
index fb11f514d83..53b847d74d7 100644
--- a/core/string/string_name.cpp
+++ b/core/string/string_name.cpp
@@ -35,6 +35,8 @@
#include "core/string/print_string.h"
#include "core/templates/paged_allocator.h"
+const StringName StringName::EMPTY;
+
struct StringName::Table {
constexpr static uint32_t TABLE_BITS = 16;
constexpr static uint32_t TABLE_LEN = 1 << TABLE_BITS;
diff --git a/core/string/string_name.h b/core/string/string_name.h
index 1f86ee8ab2e..1f9abfece28 100644
--- a/core/string/string_name.h
+++ b/core/string/string_name.h
@@ -84,6 +84,8 @@ class [[nodiscard]] _WARN_UNUSED_ StringName {
StringName(_Data *p_data) { _data = p_data; }
public:
+ static const StringName EMPTY;
+
_FORCE_INLINE_ explicit operator bool() const { return _data; }
bool operator==(const String &p_name) const;
@@ -134,8 +136,7 @@ public:
}
_FORCE_INLINE_ const String &string() const _LIFETIME_BOUND_ {
- static const String EMPTY;
- return _data ? _data->name : EMPTY;
+ return _data ? _data->name : String::EMPTY;
}
_FORCE_INLINE_ operator const String &() const _LIFETIME_BOUND_ {
@@ -179,7 +180,7 @@ public:
p_name._data = nullptr;
}
StringName(const String &p_name, bool p_static = false);
- StringName() {}
+ constexpr StringName() {}
#ifdef SIZE_EXTRA
_NO_INLINE_
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp
index 94d9a06b07f..cedd66ff82a 100644
--- a/core/string/ustring.cpp
+++ b/core/string/ustring.cpp
@@ -60,6 +60,7 @@ STATIC_ASSERT_INCOMPLETE_TYPE(class, Object);
#endif
static const int MAX_DECIMALS = 32;
+const String String::EMPTY;
static _FORCE_INLINE_ char32_t lower_case(char32_t p_char) {
return (is_ascii_upper_case(p_char) ? (p_char + ('a' - 'A')) : p_char);
diff --git a/core/string/ustring.h b/core/string/ustring.h
index 552b648eb4f..4c3bbf49902 100644
--- a/core/string/ustring.h
+++ b/core/string/ustring.h
@@ -305,6 +305,8 @@ class [[nodiscard]] _WARN_UNUSED_ String {
String _separate_compound_words() const;
public:
+ static const String EMPTY;
+
enum {
npos = -1 ///<for "some" compatibility with std::string (npos is a huge value in std::string)
};
@@ -673,7 +675,7 @@ public:
* The constructors must not depend on other overloads
*/
- _FORCE_INLINE_ String() {}
+ _FORCE_INLINE_ constexpr String() {}
_FORCE_INLINE_ String(const String &p_str) = default;
_FORCE_INLINE_ String(String &&p_str) = default;
#ifdef SIZE_EXTRA
diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h
index 20f1b8d86f3..09c4105e6dd 100644
--- a/core/templates/cowdata.h
+++ b/core/templates/cowdata.h
@@ -230,7 +230,7 @@ public:
_FORCE_INLINE_ operator Span<T>() const _LIFETIME_BOUND_ { return Span<T>(ptr(), size()); }
_FORCE_INLINE_ Span<T> span() const _LIFETIME_BOUND_ { return operator Span<T>(); }
- _FORCE_INLINE_ CowData() {}
+ _FORCE_INLINE_ constexpr CowData() {}
_FORCE_INLINE_ ~CowData() { _unref(); }
_FORCE_INLINE_ CowData(std::initializer_list<T> p_init);
_FORCE_INLINE_ explicit CowData(Span<T> p_span);
diff --git a/core/variant/type_info.h b/core/variant/type_info.h
index a580856b0f8..b4b322efc00 100644
--- a/core/variant/type_info.h
+++ b/core/variant/type_info.h
@@ -306,8 +306,7 @@ const StringName &get_object_class_name_or_empty() {
if constexpr (std::is_base_of_v<Object, T>) {
return T::get_class_static();
} else {
- static const StringName EMPTY = "";
- return EMPTY;
+ return StringName::EMPTY;
}
}