From e74b8f593ecd412ee410f538ac88aa8b91d24b33 Mon Sep 17 00:00:00 2001 From: danielytics Date: Wed, 7 Mar 2018 12:21:33 +0000 Subject: [PATCH] adds variadic printing function, makes String.format custom placeholder work --- include/core/GodotGlobal.hpp | 6 ++++++ include/core/String.hpp | 2 ++ src/core/String.cpp | 11 ++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/core/GodotGlobal.hpp b/include/core/GodotGlobal.hpp index 7d28dba3..a9133593 100644 --- a/include/core/GodotGlobal.hpp +++ b/include/core/GodotGlobal.hpp @@ -3,6 +3,7 @@ #include #include "String.hpp" +#include "Array.hpp" namespace godot { @@ -20,6 +21,11 @@ public: static void gdnative_init(godot_gdnative_init_options *o); static void gdnative_terminate(godot_gdnative_terminate_options *o); static void nativescript_init(void *handle); + + template + static void print(const String& fmt, Args... values) { + print(fmt.format(Array::make(values...))); + } }; diff --git a/include/core/String.hpp b/include/core/String.hpp index de65f3cf..7826fcb6 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -81,6 +81,7 @@ public: int find(String what, int from = 0) const; int find_last(String what) const; int findn(String what, int from = 0) const; + String format(Variant values) const; String format(Variant values, String placeholder) const; String get_base_dir() const; String get_basename() const; @@ -128,6 +129,7 @@ public: String to_upper() const; String xml_escape() const; String xml_unescape() const; + }; String operator+(const char *a, const String &b); diff --git a/src/core/String.cpp b/src/core/String.cpp index 3d822b1c..737c0408 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -267,13 +267,22 @@ int String::findn(String what, int from) const { return godot::api->godot_string_findn(&_godot_string, what._godot_string); } -String String::format(Variant values, String placeholder) const { +String String::format(Variant values) const { String new_string; new_string._godot_string = godot::api->godot_string_format(&_godot_string, (godot_variant *)&values); return new_string; } +String String::format(Variant values, String placeholder) const { + String new_string; + godot_char_string contents = godot::api->godot_string_utf8(&placeholder._godot_string); + new_string._godot_string = godot::api->godot_string_format_with_custom_placeholder(&_godot_string, (godot_variant *)&values, godot::api->godot_char_string_get_data(&contents)); + godot::api->godot_char_string_destroy(&contents); + + return new_string; +} + String String::get_base_dir() const { String new_string; new_string._godot_string = godot::api->godot_string_get_base_dir(&_godot_string);