Browse Source

adds variadic printing function, makes String.format custom placeholder work

danielytics 7 years ago
parent
commit
e74b8f593e
3 changed files with 18 additions and 1 deletions
  1. 6 0
      include/core/GodotGlobal.hpp
  2. 2 0
      include/core/String.hpp
  3. 10 1
      src/core/String.cpp

+ 6 - 0
include/core/GodotGlobal.hpp

@@ -3,6 +3,7 @@
 
 #include <gdnative_api_struct.gen.h>
 #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 <class... Args>
+	static void print(const String& fmt, Args... values) {
+		print(fmt.format(Array::make(values...)));
+	}
 };
 
 

+ 2 - 0
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);

+ 10 - 1
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);