Added to_real helper methods to String.

This commit is contained in:
Relintai 2023-12-27 13:02:57 +01:00
parent 3f2149212a
commit 43b14a071c
2 changed files with 33 additions and 0 deletions

View File

@ -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 @@ public:
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 @@ public:
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;

View File

@ -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());