diff options
Diffstat (limited to 'core/string')
-rw-r--r-- | core/string/node_path.cpp | 8 | ||||
-rw-r--r-- | core/string/print_string.cpp | 2 | ||||
-rw-r--r-- | core/string/string_name.cpp | 4 | ||||
-rw-r--r-- | core/string/translation.h | 1 | ||||
-rw-r--r-- | core/string/ustring.cpp | 30 |
5 files changed, 26 insertions, 19 deletions
diff --git a/core/string/node_path.cpp b/core/string/node_path.cpp index af7c18741d..32e4564c5e 100644 --- a/core/string/node_path.cpp +++ b/core/string/node_path.cpp @@ -73,7 +73,7 @@ int NodePath::get_name_count() const { } StringName NodePath::get_name(int p_idx) const { - ERR_FAIL_COND_V(!data, StringName()); + ERR_FAIL_NULL_V(data, StringName()); ERR_FAIL_INDEX_V(p_idx, data->path.size(), StringName()); return data->path[p_idx]; } @@ -87,7 +87,7 @@ int NodePath::get_subname_count() const { } StringName NodePath::get_subname(int p_idx) const { - ERR_FAIL_COND_V(!data, StringName()); + ERR_FAIL_NULL_V(data, StringName()); ERR_FAIL_INDEX_V(p_idx, data->subpath.size(), StringName()); return data->subpath[p_idx]; } @@ -200,7 +200,7 @@ Vector<StringName> NodePath::get_subnames() const { } StringName NodePath::get_concatenated_names() const { - ERR_FAIL_COND_V(!data, StringName()); + ERR_FAIL_NULL_V(data, StringName()); if (!data->concatenated_path) { int pc = data->path.size(); @@ -215,7 +215,7 @@ StringName NodePath::get_concatenated_names() const { } StringName NodePath::get_concatenated_subnames() const { - ERR_FAIL_COND_V(!data, StringName()); + ERR_FAIL_NULL_V(data, StringName()); if (!data->concatenated_subpath) { int spc = data->subpath.size(); diff --git a/core/string/print_string.cpp b/core/string/print_string.cpp index dcdde3c175..e3614be359 100644 --- a/core/string/print_string.cpp +++ b/core/string/print_string.cpp @@ -65,7 +65,7 @@ void remove_print_handler(const PrintHandlerList *p_handler) { //OS::get_singleton()->print("print handler list is %p\n",print_handler_list); _global_unlock(); - ERR_FAIL_COND(l == nullptr); + ERR_FAIL_NULL(l); } void __print_line(String p_string) { diff --git a/core/string/string_name.cpp b/core/string/string_name.cpp index 6099fea13f..4402e44ad4 100644 --- a/core/string/string_name.cpp +++ b/core/string/string_name.cpp @@ -390,7 +390,7 @@ StringName::StringName(const String &p_name, bool p_static) { StringName StringName::search(const char *p_name) { ERR_FAIL_COND_V(!configured, StringName()); - ERR_FAIL_COND_V(!p_name, StringName()); + ERR_FAIL_NULL_V(p_name, StringName()); if (!p_name[0]) { return StringName(); } @@ -426,7 +426,7 @@ StringName StringName::search(const char *p_name) { StringName StringName::search(const char32_t *p_name) { ERR_FAIL_COND_V(!configured, StringName()); - ERR_FAIL_COND_V(!p_name, StringName()); + ERR_FAIL_NULL_V(p_name, StringName()); if (!p_name[0]) { return StringName(); } diff --git a/core/string/translation.h b/core/string/translation.h index ca8b460312..3f9dbcc476 100644 --- a/core/string/translation.h +++ b/core/string/translation.h @@ -33,7 +33,6 @@ #include "core/io/resource.h" #include "core/object/gdvirtual.gen.inc" -#include "core/object/script_language.h" class Translation : public Resource { GDCLASS(Translation, Resource); diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 3f11459a1e..5a42a3af12 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -1493,9 +1493,9 @@ String String::num(double p_num, int p_decimals) { if (p_decimals < 0) { p_decimals = 14; - const double abs_num = ABS(p_num); + const double abs_num = Math::abs(p_num); if (abs_num > 10) { - // We want to align the digits to the above sane default, so we only + // We want to align the digits to the above reasonable default, so we only // need to subtract log10 for numbers with a positive power of ten. p_decimals -= (int)floor(log10(abs_num)); } @@ -2763,12 +2763,13 @@ double String::to_float() const { } uint32_t String::hash(const char *p_cstr) { + // static_cast: avoid negative values on platforms where char is signed. uint32_t hashv = 5381; - uint32_t c = *p_cstr++; + uint32_t c = static_cast<uint8_t>(*p_cstr++); while (c) { hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */ - c = *p_cstr++; + c = static_cast<uint8_t>(*p_cstr++); } return hashv; @@ -2777,28 +2778,35 @@ uint32_t String::hash(const char *p_cstr) { uint32_t String::hash(const char *p_cstr, int p_len) { uint32_t hashv = 5381; for (int i = 0; i < p_len; i++) { - hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */ + // static_cast: avoid negative values on platforms where char is signed. + hashv = ((hashv << 5) + hashv) + static_cast<uint8_t>(p_cstr[i]); /* hash * 33 + c */ } return hashv; } uint32_t String::hash(const wchar_t *p_cstr, int p_len) { + // Avoid negative values on platforms where wchar_t is signed. Account for different sizes. + using wide_unsigned = std::conditional<sizeof(wchar_t) == 2, uint16_t, uint32_t>::type; + uint32_t hashv = 5381; for (int i = 0; i < p_len; i++) { - hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */ + hashv = ((hashv << 5) + hashv) + static_cast<wide_unsigned>(p_cstr[i]); /* hash * 33 + c */ } return hashv; } uint32_t String::hash(const wchar_t *p_cstr) { + // Avoid negative values on platforms where wchar_t is signed. Account for different sizes. + using wide_unsigned = std::conditional<sizeof(wchar_t) == 2, uint16_t, uint32_t>::type; + uint32_t hashv = 5381; - uint32_t c = *p_cstr++; + uint32_t c = static_cast<wide_unsigned>(*p_cstr++); while (c) { hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */ - c = *p_cstr++; + c = static_cast<wide_unsigned>(*p_cstr++); } return hashv; @@ -4890,8 +4898,8 @@ String String::sprintf(const Array &values, bool *error) const { } double value = values[value_index]; - bool is_negative = (value < 0); - String str = String::num(ABS(value), min_decimals); + bool is_negative = signbit(value); + String str = String::num(Math::abs(value), min_decimals); const bool is_finite = Math::is_finite(value); // Pad decimals out. @@ -4953,7 +4961,7 @@ String String::sprintf(const Array &values, bool *error) const { String str = "("; for (int i = 0; i < count; i++) { double val = vec[i]; - String number_str = String::num(ABS(val), min_decimals); + String number_str = String::num(Math::abs(val), min_decimals); const bool is_finite = Math::is_finite(val); // Pad decimals out. |