Skip to content

Commit

Permalink
Added to_real helper methods to String.
Browse files Browse the repository at this point in the history
  • Loading branch information
Relintai committed Dec 27, 2023
1 parent 3f21492 commit 43b14a0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions core/string/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 43b14a0

Please sign in to comment.