Skip to content

Commit ff111e3

Browse files
committed
Merge pull request godotengine#103917 from Ivorforce/sprintf-span
Optimize `vformat` by using `Span` in `sprintf`
2 parents 6e75c28 + a916325 commit ff111e3

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

core/string/ustring.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,7 +5461,7 @@ String String::lpad(int min_length, const String &character) const {
54615461
// "fish %s pie" % "frog"
54625462
// "fish %s %d pie" % ["frog", 12]
54635463
// In case of an error, the string returned is the error description and "error" is true.
5464-
String String::sprintf(const Array &values, bool *error) const {
5464+
String String::sprintf(const Span<Variant> &values, bool *error) const {
54655465
static const String ZERO("0");
54665466
static const String SPACE(" ");
54675467
static const String MINUS("-");
@@ -5470,7 +5470,7 @@ String String::sprintf(const Array &values, bool *error) const {
54705470
String formatted;
54715471
char32_t *self = (char32_t *)get_data();
54725472
bool in_format = false;
5473-
int value_index = 0;
5473+
uint64_t value_index = 0;
54745474
int min_chars = 0;
54755475
int min_decimals = 0;
54765476
bool in_decimals = false;

core/string/ustring.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class [[nodiscard]] String {
438438
String trim_suffix(const char *p_suffix) const;
439439
String lpad(int min_length, const String &character = " ") const;
440440
String rpad(int min_length, const String &character = " ") const;
441-
String sprintf(const Array &values, bool *error) const;
441+
String sprintf(const Span<Variant> &values, bool *error) const;
442442
String quote(const String &quotechar = "\"") const;
443443
String unquote() const;
444444
static String num(double p_num, int p_decimals = -1);

core/variant/array.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,10 @@ bool Array::is_read_only() const {
936936
return _p->read_only != nullptr;
937937
}
938938

939+
Span<Variant> Array::span() const {
940+
return _p->array.span();
941+
}
942+
939943
Array::Array(const Array &p_from) {
940944
_p = nullptr;
941945
_ref(p_from);

core/variant/array.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#pragma once
3232

33+
#include "core/templates/span.h"
3334
#include "core/typedefs.h"
3435
#include "core/variant/variant_deep_duplicate.h"
3536

@@ -201,6 +202,11 @@ class Array {
201202
bool is_read_only() const;
202203
static Array create_read_only();
203204

205+
Span<Variant> span() const;
206+
operator Span<Variant>() const {
207+
return this->span();
208+
}
209+
204210
Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
205211
Array(const Array &p_from);
206212
Array(std::initializer_list<Variant> p_init);

core/variant/variant.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -925,14 +925,9 @@ const Variant::ObjData &Variant::_get_obj() const {
925925
template <typename... VarArgs>
926926
String vformat(const String &p_text, const VarArgs... p_args) {
927927
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
928-
Array args_array;
929-
args_array.resize(sizeof...(p_args));
930-
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
931-
args_array[i] = args[i];
932-
}
933928

934929
bool error = false;
935-
String fmt = p_text.sprintf(args_array, &error);
930+
String fmt = p_text.sprintf(Span(args, sizeof...(p_args)), &error);
936931

937932
ERR_FAIL_COND_V_MSG(error, String(), String("Formatting error in string \"") + p_text + "\": " + fmt + ".");
938933

0 commit comments

Comments
 (0)