Skip to content

Commit

Permalink
Optimize String::get_data / CharString::get_data / `Char16String:…
Browse files Browse the repository at this point in the history
…:get_data` by avoiding a dereference of ptr, and inlining the function. Optimize `String::is_empty` by avoiding fetching the size.
  • Loading branch information
Ivorforce committed Feb 3, 2025
1 parent 1586c56 commit 19be5aa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
21 changes: 0 additions & 21 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ void Char16String::operator=(const char16_t *p_cstr) {
copy_from(p_cstr);
}

const char16_t *Char16String::get_data() const {
if (size()) {
return &operator[](0);
} else {
return u"";
}
}

void Char16String::copy_from(const char16_t *p_cstr) {
if (!p_cstr) {
resize(0);
Expand Down Expand Up @@ -185,14 +177,6 @@ void CharString::operator=(const char *p_cstr) {
copy_from(p_cstr);
}

const char *CharString::get_data() const {
if (size()) {
return &operator[](0);
} else {
return "";
}
}

void CharString::copy_from(const char *p_cstr) {
if (!p_cstr) {
resize(0);
Expand Down Expand Up @@ -934,11 +918,6 @@ signed char String::filenocasecmp_to(const String &p_str) const {
return naturalnocasecmp_to_base(this_str, that_str);
}

const char32_t *String::get_data() const {
static const char32_t zero = 0;
return size() ? &operator[](0) : &zero;
}

String String::_camelcase_to_underscore() const {
const char32_t *cstr = get_data();
String new_string;
Expand Down
8 changes: 4 additions & 4 deletions core/string/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class Char16String {
bool operator<(const Char16String &p_right) const;
Char16String &operator+=(char16_t p_char);
int length() const { return size() ? size() - 1 : 0; }
const char16_t *get_data() const;
const char16_t *get_data() const { return ptr() ? ptr() : &_null; }
operator const char16_t *() const { return get_data(); }
explicit operator StrRange<char16_t>() const { return StrRange(get_data(), length()); }

Expand Down Expand Up @@ -249,7 +249,7 @@ class CharString {
bool operator==(const CharString &p_right) const;
CharString &operator+=(char p_char);
int length() const { return size() ? size() - 1 : 0; }
const char *get_data() const;
const char *get_data() const { return ptr() ? ptr() : &_null; }
operator const char *() const { return get_data(); }
explicit operator StrRange<char>() const { return StrRange(get_data(), length()); }

Expand Down Expand Up @@ -385,7 +385,7 @@ class String {
signed char filecasecmp_to(const String &p_str) const;
signed char filenocasecmp_to(const String &p_str) const;

const char32_t *get_data() const;
const char32_t *get_data() const { return ptr() ? ptr() : &_null; }
/* standard size stuff */

_FORCE_INLINE_ int length() const {
Expand Down Expand Up @@ -544,7 +544,7 @@ class String {
Vector<uint8_t> sha1_buffer() const;
Vector<uint8_t> sha256_buffer() const;

_FORCE_INLINE_ bool is_empty() const { return length() == 0; }
_FORCE_INLINE_ bool is_empty() const { return ptr() == nullptr; }
_FORCE_INLINE_ bool contains(const char *p_str) const { return find(p_str) != -1; }
_FORCE_INLINE_ bool contains(const String &p_str) const { return find(p_str) != -1; }
_FORCE_INLINE_ bool contains_char(char32_t p_chr) const { return find_char(p_chr) != -1; }
Expand Down

0 comments on commit 19be5aa

Please sign in to comment.