diff --git a/core/string/ustring.h b/core/string/ustring.h index c404fc5592..0d2f484c23 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -34,6 +34,7 @@ #include "core/containers/cowdata.h" #include "core/containers/vector.h" +#include "core/math/math_defs.h" #include "core/string/char_utils.h" #include "core/typedefs.h" #include "core/variant/array.h" @@ -372,6 +373,14 @@ class String { bool to_bool() const; uint32_t to_uint() const; + _FORCE_INLINE_ real_t to_real() const { +#ifdef REAL_T_IS_DOUBLE + return to_double(); +#else + return to_float(); +#endif + } + int hex_to_int(bool p_with_prefix = true) const; int64_t hex_to_int64(bool p_with_prefix = true) const; int64_t bin_to_int64(bool p_with_prefix = true) const; @@ -389,6 +398,28 @@ class String { static double to_double(const wchar_t *p_str, const wchar_t **r_end = nullptr); static double to_double(const CharType *p_str, const CharType **r_end = nullptr); + _FORCE_INLINE_ static real_t to_real(const char *p_str) { +#ifdef REAL_T_IS_DOUBLE + return to_double(p_str); +#else + return to_float(p_str); +#endif + } + _FORCE_INLINE_ static real_t to_real(const wchar_t *p_str, const wchar_t **r_end = nullptr) { +#ifdef REAL_T_IS_DOUBLE + return to_double(p_str, r_end); +#else + return to_float(p_str, r_end); +#endif + } + _FORCE_INLINE_ static real_t to_real(const CharType *p_str, const CharType **r_end = nullptr) { +#ifdef REAL_T_IS_DOUBLE + return to_double(p_str, r_end); +#else + return to_float(p_str, r_end); +#endif + } + static uint32_t num_characters(int64_t p_int); String capitalize() const; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 3b009edf0d..db6ba8ffa0 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -342,6 +342,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(String, is_zero); VCALL_LOCALMEM0R(String, to_double); VCALL_LOCALMEM0R(String, to_float); + VCALL_LOCALMEM0R(String, to_real); VCALL_LOCALMEM0R(String, to_int); VCALL_LOCALMEM0R(String, to_bool); VCALL_LOCALMEM0R(String, to_uint); @@ -2514,6 +2515,7 @@ void register_variant_methods() { ADDFUNC0R(STRING, REAL, String, to_double, varray()); ADDFUNC0R(STRING, REAL, String, to_float, varray()); + ADDFUNC0R(STRING, REAL, String, to_real, varray()); ADDFUNC0R(STRING, INT, String, to_int, varray()); ADDFUNC0R(STRING, INT, String, to_bool, varray()); ADDFUNC0R(STRING, INT, String, to_uint, varray());